-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
deploy-commands.js
executable file
·218 lines (191 loc) · 7.18 KB
/
deploy-commands.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// // const Config = require("./config.json");
// // const fs = require("node:fs");
// // const path = require("node:path");
// // const { Routes, REST } = require("discord.js");
// // const rest = new REST({ version: "9" }).setToken(Config.TOKEN);
// // const registerCommands = () => {
// // const commands = [];
// // const globalCommands = [];
// // const localCommands = [];
// // const commandsPath = path.join(__dirname, "commands");
// // const commandFiles = fs
// // .readdirSync(commandsPath)
// // .filter((file) => file.endsWith(".js"));
// // for (const file of commandFiles) {
// // const filePath = path.join(commandsPath, file);
// // const command = require(filePath);
// // if (command.data == undefined) { return }
// // if (command.global) {
// // globalCommands.push(JSON.parse(JSON.stringify(command.data)));
// // commands.push(JSON.parse(JSON.stringify(command.data)));
// // } else {
// // console.log("test "+command.data)
// // localCommands.push(JSON.parse(JSON.stringify(command.data)));
// // commands.push(JSON.parse(JSON.stringify(command.data)));
// // }
// // }
// // const contextCommands = [];
// // const contextPath = path.join(__dirname, "contextMenu");
// // const contextFiles = fs
// // .readdirSync(contextPath)
// // .filter((f) => f.endsWith(".js"));
// // for (const file of contextFiles) {
// // const filePath = path.join(contextPath, file);
// // const command = require(filePath);
// // contextCommands.pushJSON.parse(JSON.stringify(command.data));;
// // }
// // const commandsAllGlobal = [...contextCommands, ...globalCommands];
// // const commandsAllLocal = [...contextCommands, ...localCommands];
// // const commandsAllDEV = [
// // ...contextCommands,
// // ...globalCommands,
// // ...localCommands,
// // ];
// // if (Config.ENV == "prod") {
// // // global commands
// // rest.put(Routes.applicationCommands(Config.APP_ID), {
// // body: commandsAllGlobal,
// // }).catch(console.error);
// // // local commands
// // rest.put(Routes.applicationGuildCommands(Config.APP_ID, Config.GUILD_ID), {
// // body: commandsAllLocal,
// // }).catch(console.error);
// // console.log("Successfully registered global and local commands");
// // return true;
// // } else {
// // // local commands
// // rest.put(Routes.applicationGuildCommands(Config.APP_ID, Config.GUILD_ID), {
// // body: commandsAllDEV,
// // }).catch(console.error);
// // console.log("Successfully registered commands locally");
// // return true;
// // }
// // };
// // const deleteOld = () => {
// // rest
// // .put(Routes.applicationCommands(Config.CLIENT_ID), { body: [] })
// // .then(() => {
// // console.log("Successfully deleted all application commands.");
// // })
// // .catch(console.error);
// // };
// // module.exports = {
// // registerCommands,
// // deleteOld,
// // };
// const { REST, Routes } = require('discord.js');
// const { CLIENT_ID, GUILD_ID, TOKEN } = require('./config.json');
// const fs = require('node:fs');
// const path = require('node:path');
// const commands = [];
// console.log("Starting command registry...")
// // Grab all the command files from the commands directory you created earlier
// const commandsPath = path.join(__dirname, 'commands');
// const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));
// // Grab the SlashCommandBuilder#toJSON() output of each command's data for deployment
// var i = 0;
// const commandFilesLength = commandFiles.length
// for (const file of commandFiles) {
// i++;
// const filePath = path.join(commandsPath, file);
// const command = require(filePath);
// if ('data' in command && 'execute' in command) {
// commands.push(command.data.toJSON());
// // console.log(`[SUCCESS] Registered command ${i}/${commandFilesLength}`);
// process.stdout.write(`\r[SUCCESS] Registered command ${i}/${commandFiles.length}: `);
// } else {
// console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`);
// }
// console.log()
// }
// // Construct and prepare an instance of the REST module
// const rest = new REST().setToken(TOKEN);
// // and deploy your commands!
// (async () => {
// try {
// console.log(`Started refreshing ${commands.length} application (/) commands.`);
// // The put method is used to fully refresh all commands in the guild with the current set
// const data = await rest.put(
// Routes.applicationGuildCommands(CLIENT_ID, GUILD_ID),
// { body: commands },
// );
// console.log(`Successfully reloaded ${data.length} application (/) commands.`);
// } catch (error) {
// // And of course, make sure you catch and log any errors!
// console.error(error);
// }
// })();
const Config = require("./config.json");
const fs = require("node:fs");
const path = require("node:path");
const { Routes, REST } = require("discord.js");
const rest = new REST({ version: "9" }).setToken(Config.TOKEN);
const registerCommands = () => {
console.log("Registering commands");
const commands = [];
const globalCommands = [];
const localCommands = [];
const commandsPath = path.join(__dirname, "commands");
const commandFiles = fs
.readdirSync(commandsPath)
.filter((file) => file.endsWith(".js"));
for (const file of commandFiles) {
const filePath = path.join(commandsPath, file);
const command = require(filePath);
if (command.global) {
globalCommands.push(command.data.toJSON());
commands.push(command.data.toJSON());
} else {
localCommands.push(command.data.toJSON());
commands.push(command.data.toJSON());
}
}
const contextCommands = [];
const contextPath = path.join(__dirname, "contextMenu");
const contextFiles = fs
.readdirSync(contextPath)
.filter((f) => f.endsWith(".js"));
for (const file of contextFiles) {
const filePath = path.join(contextPath, file);
const command = require(filePath);
contextCommands.push(command.data.toJSON());
}
const commandsAllGlobal = [...contextCommands, ...globalCommands];
const commandsAllLocal = [...contextCommands, ...localCommands];
const commandsAllDEV = [
...contextCommands,
...globalCommands,
...localCommands,
];
if (Config.ENV == "prod") {
// global commands
rest.put(Routes.applicationCommands(Config.APP_ID), {
body: commandsAllGlobal,
});
// local commands
rest.put(Routes.applicationGuildCommands(Config.APP_ID, Config.GUILD_ID), {
body: commandsAllLocal,
});
console.log("Successfully registered global and local commands");
return true;
} else {
// local commands
rest.put(Routes.applicationGuildCommands(Config.APP_ID, Config.GUILD_ID), {
body: commandsAllDEV,
});
console.log("Successfully registered commands locally");
return true;
}
};
const deleteOld = () => {
rest
.put(Routes.applicationCommands(Config.CLIENT_ID), { body: [] })
.then(() => {
console.log("Successfully deleted all application commands.");
})
.catch(console.error);
};
module.exports = {
registerCommands,
deleteOld,
};