-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeployCommands.js
94 lines (84 loc) · 2.71 KB
/
deployCommands.js
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import { REST, Routes } from 'discord.js';
import { clientId, token } from './auth.json';
import { getFiles } from './utils.js';
import { startDatabase } from './database/db';
import path from 'node:path';
const arkchatGuildId = '383889230704803851';
// does clientId need to be dynamic with sharding?
const cliArgs = process.argv.slice(2);
const getCommandDetails = async () => {
const commands = [];
const baseCommandPath = path.join(__dirname, 'commands');
const commandFiles = getFiles(baseCommandPath);
let guildId = '';
const deployGlobally = cliArgs.includes('-g') || cliArgs.includes('--global');
if (cliArgs.includes('-r') || cliArgs.includes('--reset')) {
guildId = arkchatGuildId;
} else if (cliArgs.includes('-G') || cliArgs.includes('--guild')) {
const db = await startDatabase('./database/db.json');
let flagIndex = cliArgs.indexOf('-G');
if (flagIndex < 0) {
flagIndex = cliArgs.indexOf('--guild');
}
guildId = cliArgs[flagIndex + 1];
const guildConfig = db.data.guilds[guildId];
if (!guildConfig) {
console.log('That server does not have a configuration set up. You must initialize the configuration and choose which commands to use in that server.');
return ({ route: null, commands: null });
}
commandFiles.filter(cf => !cf.global)
.reduce((acc, command) => {
if (guildConfig[command.name].enabled) {
acc.push(command.data.toJSON());
}
return acc;
}, commands);
} else {
commandFiles.reduce((acc, command) => {
if (command.global === deployGlobally) {
acc.push(command.data.toJSON());
}
return acc;
}, commands);
guildId = arkchatGuildId;
}
const route = guildId ?
Routes.applicationGuildCommands(clientId, guildId) :
Routes.applicationCommands(clientId);
return {
commands,
route
};
};
/**
* Example commands:
* 1) command: node deployCommands.js
* effect: deploys all commands to the Arkchat guild, for testing
* 2) command: node deployCommands.js -g
* effect: deploys all global commands
* 3) command: node deployCommands.js -G 383889230704803851
* effect: deploys commands to the guild based on their configuration
* 4) command: node deployCommands.js -r
* effect: resets Arkchat guild-specific commands
*/
const rest = new REST({ version: '10' }).setToken(token);
const loadCommands = (async () => {
const { route, commands } = await getCommandDetails();
if (!route || !commands) {
return;
}
try {
console.log(`Started refreshing ${commands.length} application (/) commands.`);
const data = await rest.put(
route,
{ body: commands }
);
console.log(`Successfully reloaded ${data.length} application (/) commands.`);
} catch (error) {
console.error(error);
}
});
loadCommands();
export {
loadCommands
};