39 lines
1.3 KiB
TypeScript
39 lines
1.3 KiB
TypeScript
import { join } from "path";
|
|
import { Collection, REST, Routes } from "discord.js";
|
|
import getAllFiles from "@bot/utils/getAllFiles";
|
|
import Interaction from "@bot/components/interaction";
|
|
import DiscordBot from "@bot/bot";
|
|
|
|
export default class InteractionHandler extends Collection<string, Interaction> {
|
|
readonly client: DiscordBot;
|
|
|
|
constructor(client: DiscordBot) {
|
|
super();
|
|
this.client = client;
|
|
this.init();
|
|
}
|
|
|
|
private async init() {
|
|
const interactionsDirectory = join(__dirname, "../interactions");
|
|
const modules = getAllFiles(interactionsDirectory);
|
|
|
|
for (const module of modules) {
|
|
const interactionClass = ((r) => r.default || r)(require(module))
|
|
const interaction: Interaction = new interactionClass(this.client);
|
|
this.set(interaction.name, interaction);
|
|
}
|
|
}
|
|
|
|
async deploy() {
|
|
const interactions = this.map(inter => inter.toJSON())
|
|
const rest = new REST({ version: "10" }).setToken(process.env.BOT_TOKEN!);
|
|
|
|
for (const [_, guild] of this.client.guilds.cache) {
|
|
rest.put(
|
|
Routes.applicationGuildCommands(process.env.CLIENT_ID!, guild.id),
|
|
{ body: interactions }
|
|
)
|
|
}
|
|
}
|
|
}
|