-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
77 lines (68 loc) · 3.18 KB
/
Copy pathindex.js
File metadata and controls
77 lines (68 loc) · 3.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
'use strict';
const { Client, GatewayIntentBits, SlashCommandBuilder, REST, Routes } = require('discord.js');
const AGENT_BASE = (process.env.UCASH_AGENT_BASE || 'https://agents.u.cash').replace(/\/+$/, '');
const UXC_API_KEY = process.env.UXC_API_KEY || '';
const BASE = (process.env.UCASH_BASE_URL || 'https://pay.u.cash').replace(/\/+$/, '');
// Credentials can come from env vars OR from pay.u.cash Settings (read via /v1/integrations).
let CLOUD = process.env.UCASH_CLOUD_TOKEN || '';
let SERVER_ID = process.env.DISCORD_SERVER_ID || '';
let PAID_ROLE_ID = process.env.DISCORD_PAID_ROLE_ID || '';
async function loadCreds() {
let discordToken = process.env.DISCORD_TOKEN || '';
if (UXC_API_KEY) {
try {
const res = await fetch(`${AGENT_BASE}/v1/integrations`, { headers: { 'X-Api-Key': UXC_API_KEY } });
const j = await res.json();
if (j && j.discord) {
discordToken = j.discord.bot_token || discordToken;
SERVER_ID = j.discord.server_id || SERVER_ID;
PAID_ROLE_ID = j.discord.paid_role_id || PAID_ROLE_ID;
}
} catch (e) {
console.error('Could not read Discord creds from pay.u.cash /v1/integrations:', e.message);
}
}
return discordToken;
}
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
const commands = [
new SlashCommandBuilder()
.setName('pay')
.setDescription('Create a U.CASH Pay checkout link (crypto + cards)')
.addStringOption((o) => o.setName('amount').setDescription('Amount to charge').setRequired(true))
.addStringOption((o) => o.setName('currency').setDescription('Currency code (default USD)').setRequired(false))
.addStringOption((o) => o.setName('title').setDescription('Title shown on checkout').setRequired(false))
.addStringOption((o) => o.setName('external_reference').setDescription('Your reference id').setRequired(false)),
].map((c) => c.toJSON());
client.once('ready', async () => {
try {
const rest = new REST().setToken(client.token);
await rest.put(Routes.applicationCommands(client.user.id), { body: commands });
console.log(`U.CASH Pay bot online as ${client.user.tag}`);
} catch (e) {
console.error('Failed to register slash commands:', e);
}
});
client.on('interactionCreate', async (interaction) => {
if (!interaction.isChatInputCommand() || interaction.commandName !== 'pay') return;
if (!CLOUD) {
await interaction.reply('Not configured: set the store Cloud Token (UCASH_CLOUD_TOKEN) or configure it in pay.u.cash.');
return;
}
const amount = interaction.options.getString('amount');
const currency = interaction.options.getString('currency') || 'USD';
const title = interaction.options.getString('title') || '';
const external = interaction.options.getString('external_reference') || '';
const params = new URLSearchParams({ cloud: CLOUD, amount, currency });
if (title) params.set('title', title);
if (external) params.set('external_reference', external);
await interaction.reply(`Pay with U.CASH (${amount} ${currency}): ${BASE}/embed.php?${params.toString()}`);
});
(async () => {
const token = await loadCreds();
if (!token) {
console.error('No Discord token. Set DISCORD_TOKEN (env) or store it in pay.u.cash Settings and set UXC_API_KEY.');
process.exit(1);
}
await client.login(token);
})();