2023-01-17 23:11:22 +01:00
|
|
|
import { REST } from "@discordjs/rest";
|
|
|
|
import { Routes } from "discord-api-types/v9";
|
|
|
|
import { Client } from "discord.js";
|
|
|
|
import { readdir } from "fs/promises";
|
|
|
|
import { removeExtension } from "../utils/misc";
|
2022-07-03 21:09:57 +02:00
|
|
|
|
2022-07-22 11:46:47 +02:00
|
|
|
/** Load all the commands. */
|
2022-07-20 23:01:47 +02:00
|
|
|
export default async (client: Client) => {
|
2023-01-17 23:11:22 +01:00
|
|
|
const rest = new REST({ version: "10" }).setToken(client.token ?? "");
|
2022-07-03 21:09:57 +02:00
|
|
|
|
2023-01-17 23:11:22 +01:00
|
|
|
const command_categories = (await readdir(__dirname)).filter(
|
|
|
|
(element) => !element.endsWith(".js") && !element.endsWith(".ts")
|
|
|
|
);
|
2022-07-03 21:09:57 +02:00
|
|
|
|
2023-01-17 23:11:22 +01:00
|
|
|
const commands = (
|
|
|
|
await Promise.all(
|
|
|
|
// For each categorie
|
|
|
|
command_categories.map(async (command_category) => {
|
|
|
|
// Retrieve all the commands
|
|
|
|
const command_files = await readdir(`${__dirname}/${command_category}`);
|
2022-07-03 21:09:57 +02:00
|
|
|
|
2023-01-17 23:11:22 +01:00
|
|
|
// Add the category to the collection for the help command
|
|
|
|
client.commands.categories.set(command_category, command_files.map(removeExtension));
|
2022-07-25 00:54:19 +02:00
|
|
|
|
2023-01-17 23:11:22 +01:00
|
|
|
// Add the command
|
|
|
|
return Promise.all(
|
|
|
|
command_files.map(async (command_file) => {
|
|
|
|
const command = (await import(`../commands/${command_category}/${command_file}`))
|
|
|
|
.default;
|
2022-07-03 21:09:57 +02:00
|
|
|
|
2023-01-17 23:11:22 +01:00
|
|
|
// Add it to the collection so the interaction will work
|
|
|
|
command.data = command.data(client);
|
|
|
|
client.commands.list.set(command.data.name, command);
|
2022-07-03 21:09:57 +02:00
|
|
|
|
2023-01-17 23:11:22 +01:00
|
|
|
return command;
|
|
|
|
})
|
|
|
|
);
|
|
|
|
})
|
|
|
|
)
|
|
|
|
).flat(2);
|
2022-07-03 21:09:57 +02:00
|
|
|
|
2023-01-17 23:11:22 +01:00
|
|
|
// Send guilds commands to Discord
|
|
|
|
const scopedCommands = new Map<string, unknown[]>();
|
2023-01-17 22:05:38 +01:00
|
|
|
|
2023-01-17 23:11:22 +01:00
|
|
|
// Add commands to guild where the bot is
|
|
|
|
const allowedGuilds = client.guilds.cache;
|
2023-01-17 22:05:38 +01:00
|
|
|
|
2023-01-17 23:11:22 +01:00
|
|
|
// Assign each commands to the guilds
|
|
|
|
commands
|
|
|
|
.filter((c) => c.scope().length > 0)
|
|
|
|
.forEach((c) => {
|
|
|
|
c.scope().forEach((guild: string) => {
|
|
|
|
if (allowedGuilds.get(guild) !== undefined) {
|
|
|
|
const guildCommands = scopedCommands.get(guild);
|
|
|
|
if (guildCommands === undefined) {
|
|
|
|
scopedCommands.set(guild, [c.data.toJSON()]);
|
|
|
|
} else {
|
|
|
|
guildCommands.push(c.data.toJSON());
|
|
|
|
scopedCommands.set(guild, guildCommands);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2023-01-17 22:05:38 +01:00
|
|
|
|
2023-01-17 23:11:22 +01:00
|
|
|
scopedCommands.forEach(
|
|
|
|
async (command, guild) =>
|
|
|
|
await rest.put(Routes.applicationGuildCommands(client.user?.id as string, guild), {
|
|
|
|
body: command,
|
|
|
|
})
|
|
|
|
);
|
2023-01-17 22:05:38 +01:00
|
|
|
|
2023-01-17 23:11:22 +01:00
|
|
|
// Send global commands to Discord
|
|
|
|
const globalCommands = commands.filter((c) => c.scope().length == 0);
|
|
|
|
return await rest.put(Routes.applicationCommands(client.user?.id as string), {
|
|
|
|
body: globalCommands.map((c) => c.data.toJSON()),
|
|
|
|
});
|
2022-07-03 21:09:57 +02:00
|
|
|
};
|