-
-
Notifications
You must be signed in to change notification settings - Fork 98
/
deploySlash.js
181 lines (159 loc) · 6.29 KB
/
deploySlash.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
const { plsParseArgs } = require('plsargs');
const args = plsParseArgs(process.argv.slice(2));
const chillout = require("chillout");
const { makeSureFolderExists } = require("stuffs");
const path = require("path");
const readdirRecursive = require("recursive-readdir");
const { TOKEN } = require("./settings/config.js");
const { ApplicationCommandOptionType, REST, Routes, ApplicationCommandManager } = require('discord.js');
(async () => {
let command = [];
let cleared = args.get(0) == "guild" ? args.get(2) == "clear" : (args.get(0) == "global" ? args.get(1) == "clear" : false);
let deployed = args.get(0) == "guild" ? "guild" : args.get(0) == "global" ? "global" : null;
if (!deployed) {
console.error(`Invalid sharing mode! Valid modes: guild, global`);
console.error(`Usage example: node deploySlash.js guild <guildId> [clear]`);
console.error(`Usage example: node deploySlash.js global [clear]`);
return process.exit(1);
}
if (!cleared) {
let interactionsFolder = path.resolve("./commands");
await makeSureFolderExists(interactionsFolder);
let store = [];
console.log("Reading interaction files..")
let interactionFilePaths = await readdirRecursive(interactionsFolder);
interactionFilePaths = interactionFilePaths.filter(i => {
let state = path.basename(i).startsWith("-");
return !state;
});
await chillout.forEach(interactionFilePaths, (interactionFilePath) => {
const cmd = require(interactionFilePath);
console.log(`Interaction "${cmd.type == "CHAT_INPUT" ? `/${cmd.name.join(" ")}` : `${cmd.name[0]}`}" ${cmd.name[1] || ""} ${cmd.name[2] || ""} added to the transform list!`);
store.push(cmd);
});
store = store.sort((a, b) => a.name.length - b.name.length)
command = store.reduce((all, current) => {
switch (current.name.length) {
case 1: {
all.push({
type: current.type,
name: current.name[0],
description: current.description,
defaultPermission: current.defaultPermission,
options: current.options
});
break;
}
case 2: {
let baseItem = all.find((i) => {
return i.name == current.name[0] && i.type == current.type
});
if (!baseItem) {
all.push({
type: current.type,
name: current.name[0],
description: `${current.name[0]} commands.`,
defaultPermission: current.defaultPermission,
options: [
{
type: ApplicationCommandOptionType.Subcommand,
description: current.description,
name: current.name[1],
options: current.options
}
]
});
} else {
baseItem.options.push({
type: ApplicationCommandOptionType.Subcommand,
description: current.description,
name: current.name[1],
options: current.options
})
}
break;
}
case 3: {
let SubItem = all.find((i) => {
return i.name == current.name[0] && i.type == current.type
});
if (!SubItem) {
all.push({
type: current.type,
name: current.name[0],
description: `${current.name[0]} commands.`,
defaultPermission: current.defaultPermission,
options: [
{
type: ApplicationCommandOptionType.SubcommandGroup,
description: `${current.name[1]} commands.`,
name: current.name[1],
options: [
{
type: ApplicationCommandOptionType.Subcommand,
description: current.description,
name: current.name[2],
options: current.options
}
]
}
]
});
} else {
let GroupItem = SubItem.options.find(i => {
return i.name == current.name[1] && i.type == ApplicationCommandOptionType.SubcommandGroup
});
if (!GroupItem) {
SubItem.options.push({
type: ApplicationCommandOptionType.SubcommandGroup,
description: `${current.name[1]} commands.`,
name: current.name[1],
options: [
{
type: ApplicationCommandOptionType.Subcommand,
description: current.description,
name: current.name[2],
options: current.options
}
]
})
} else {
GroupItem.options.push({
type: ApplicationCommandOptionType.Subcommand,
description: current.description,
name: current.name[2],
options: current.options
})
}
}
}
break;
}
return all;
}, []);
command = command.map(i => ApplicationCommandManager.transformCommand(i));
} else {
console.info("No interactions read, all existing ones will be cleared...");
}
const rest = new REST({ version: "9" }).setToken(TOKEN);
const client = await rest.get(Routes.user());
console.info(`Account information received! ${client.username}#${client.discriminator} (${client.id})`);
console.info(`Interactions are posted on discord!`);
switch (deployed) {
case "guild": {
let guildId = args.get(1);
console.info(`Deploy mode: guild (${guildId})`);
await rest.put(Routes.applicationGuildCommands(client.id, guildId), { body: command });
console.info(`Shared commands may take 3-5 seconds to arrive.`);
break;
}
case "global": {
console.info(`Deploy mode: global`);
await rest.put(Routes.applicationCommands(client.id), { body: command });
console.info(`Shared commands can take up to 1 hour to arrive. If you want it to come immediately, you can throw your bot from your server and get it back.`);
break;
}
}
console.info(`Interactions shared!`);
})();
/// Credit https://github.com/akanora/Youtube-Together (Handler) || Edit by: https://github.com/Adivise