forked from dev-737/InterChat.ts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy-commands.js
More file actions
77 lines (61 loc) · 2.32 KB
/
deploy-commands.js
File metadata and controls
77 lines (61 loc) · 2.32 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
// @ts-check
import { REST, Routes } from 'discord.js';
import 'dotenv/config';
const redText = (text) => `\x1b[0;31m${text}\x1b[0m`;
const greenText = (text) => `\x1b[38;5;78m${text}\x1b[0m`;
const greyText = (text) => `\x1b[38;5;246m${text}\x1b[0m`;
const TOKEN = process.env.DISCORD_TOKEN;
const CLIENT_ID = process.env.CLIENT_ID;
const SUPPORT_SERVER_ID = '770256165300338709';
if (!TOKEN || !CLIENT_ID || !SUPPORT_SERVER_ID)
throw new Error('Missing TOKEN, CLIENT_ID or SUPPORT_SERVER_ID.');
const { loadCommandFiles } = await import('../build/utils/LoadCommands.js').catch(() => {
console.error(`${redText('✘')} Code is not build yet. Use \`pnpm build\` first.`);
process.exit();
});
const registerAllCommands = async (staffOnly = false) => {
// make sure CommandsMap is not empty
const commandsMap = await loadCommandFiles();
const rest = new REST().setToken(TOKEN);
const commands = commandsMap
.filter((command) => (staffOnly ? command.staffOnly : !command.staffOnly))
.map((command) => command.data);
const route = staffOnly
? Routes.applicationGuildCommands(CLIENT_ID, SUPPORT_SERVER_ID)
: Routes.applicationCommands(CLIENT_ID);
// register all other commands to the global application;
/** @type {any} */
const registerRes = await rest.put(route, { body: commands });
const type = staffOnly ? 'private' : 'public';
const totalRegistered =
registerRes.length === commands.length
? greenText(registerRes.length)
: redText(registerRes.length);
console.log(
`${greenText('✓')} Registered ${totalRegistered}${greyText('/')}${greenText(commands.length)} ${type} application commands.`,
);
};
const logHelp = () =>
console.log(`${greenText('Usage')}: node scripts/deploy-commands.js {--public|--private|--help}`);
/**
* @param {string[]} args
*/
const parseAndRun = async (args) => {
for (const arg of process.argv.slice(2)) {
if (!args.includes(arg)) continue;
if (arg === '--help') {
logHelp();
break;
}
await registerAllCommands(arg === '--private').catch((e) => {
console.error(`${redText('✘ Error: ')}`, e);
});
}
};
if (process.argv) {
const allArgs = ['--help', '--public', '--private'];
const slicedArgs = process.argv.slice(2);
if (slicedArgs.length === 0) logHelp();
else await parseAndRun(allArgs);
process.exit();
}