-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdeploy-commands.js
More file actions
117 lines (95 loc) · 3.8 KB
/
deploy-commands.js
File metadata and controls
117 lines (95 loc) · 3.8 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
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
const { REST, Routes } = require("discord.js");
const fs = require("fs");
const path = require("path");
require("dotenv").config();
const axios = require("axios");
const logger = require("./logger.js");
//
async function deployCommands() {
const commands = [];
const globalCommands = [];
const guildCommands = [];
const publicCommands = [];
async function readCommandFiles(dir) {
const files = await fs.promises.readdir(dir);
for (const file of files) {
const filepath = path.join(dir, file);
const stats = await fs.promises.stat(filepath);
if (stats.isDirectory()) {
await readCommandFiles(filepath);
} else if (stats.isFile() && file.endsWith(".js")) {
try {
const command = require(path.resolve(filepath));
const commandJSON = command.data.toJSON();
if (command.integration_types) {
commandJSON.integration_types = command.integration_types;
} else {
commandJSON.integration_types = [0];
}
if (command.contexts) {
commandJSON.contexts = command.contexts;
} else {
commandJSON.contexts = [0, 1, 2];
}
if (command.explicit === true) {
guildCommands.push(commandJSON);
} else {
globalCommands.push(commandJSON);
}
if (!command.dev && commandJSON.type === 1) {
publicCommands.push({
name: commandJSON.name,
description: commandJSON.description,
type: 1
});
}
} catch (err) {
logger.error(`Error loading command ${filepath}:`, err);
}
}
}
}
await readCommandFiles("./slashCommands");
await readCommandFiles("./contextCommands");
const rest = new REST({ version: "10" }).setToken(process.env.token);
try {
logger.info("Started refreshing application commands.");
await rest.put(
Routes.applicationCommands(process.env.CLIENT_ID),
{ body: globalCommands },
);
logger.neon("Successfully reloaded global application commands.");
await rest.put(
Routes.applicationGuildCommands(process.env.CLIENT_ID, "1440117235401363508"),
{ body: guildCommands },
);
logger.neon("Successfully registered explicit commands in guild 1440117235401363508.");
if (process.env.CANARY !== "true") {
const dblResponse = await axios.post(
`https://discordbotlist.com/api/v1/bots/${process.env.CLIENT_ID}/commands`,
publicCommands,
{
headers: {
Authorization: `Bot ${process.env.DBL_TOKEN}`,
'Content-Type': 'application/json'
}
}
);
logger.neon('Successfully posted commands to discordbotlist.com:');
logger.neon(dblResponse.data)
}
} catch (error) {
if (error.response) {
logger.error(`Error in deployCommands: ${error.message}`);
logger.error('Response data:', JSON.stringify(error.response.data, null, 2));
logger.error('Response status:', error.response.status);
} else {
logger.error('Error in deployCommands:', error);
}
}
}
if (require.main === module) {
deployCommands();
}
module.exports = { deployCommands };
// contributors: @relentiousdragon