2022-09-18 11:57:22 +01:00
|
|
|
import { Client, REST, Routes, SlashCommandBuilder } from "discord.js";
|
2022-04-24 14:46:37 +01:00
|
|
|
import IEventItem from "../contracts/IEventItem";
|
2022-09-18 11:57:22 +01:00
|
|
|
import { CoreClient } from "./client";
|
2022-04-24 14:46:37 +01:00
|
|
|
|
|
|
|
export class Util {
|
2022-09-18 11:57:22 +01:00
|
|
|
public loadSlashCommands(client: Client) {
|
|
|
|
const registeredCommands = CoreClient.commandItems;
|
2022-04-24 14:46:37 +01:00
|
|
|
|
2022-09-18 11:57:22 +01:00
|
|
|
const globalCommands = registeredCommands.filter(x => !x.ServerId);
|
|
|
|
const guildCommands = registeredCommands.filter(x => x.ServerId);
|
2022-04-24 14:46:37 +01:00
|
|
|
|
2022-09-18 11:57:22 +01:00
|
|
|
const globalCommandData: SlashCommandBuilder[] = globalCommands
|
|
|
|
.filter(x => x.Command.CommandBuilder)
|
|
|
|
.flatMap(x => x.Command.CommandBuilder);
|
2022-04-24 14:46:37 +01:00
|
|
|
|
2022-09-18 11:57:22 +01:00
|
|
|
const guildIds: string[] = [];
|
2022-04-24 14:46:37 +01:00
|
|
|
|
2022-09-18 11:57:22 +01:00
|
|
|
for (let command of guildCommands) {
|
|
|
|
if (!guildIds.find(x => x == command.ServerId)) {
|
|
|
|
guildIds.push(command.ServerId!);
|
2022-04-24 14:46:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-18 11:57:22 +01:00
|
|
|
const rest = new REST({ version: '10' }).setToken(process.env.BOT_TOKEN!);
|
2022-04-24 14:46:37 +01:00
|
|
|
|
2022-09-18 11:57:22 +01:00
|
|
|
rest.put(
|
|
|
|
Routes.applicationCommands(process.env.BOT_CLIENTID!),
|
|
|
|
{
|
|
|
|
body: globalCommandData
|
2022-04-24 14:46:37 +01:00
|
|
|
}
|
2022-09-18 11:57:22 +01:00
|
|
|
);
|
2022-04-24 14:46:37 +01:00
|
|
|
|
2022-09-18 11:57:22 +01:00
|
|
|
for (let guild of guildIds) {
|
|
|
|
const guildCommandData = guildCommands.filter(x => x.ServerId == guild)
|
|
|
|
.filter(x => x.Command.CommandBuilder)
|
|
|
|
.flatMap(x => x.Command.CommandBuilder);
|
2022-04-24 14:46:37 +01:00
|
|
|
|
2022-09-18 11:57:22 +01:00
|
|
|
if (!client.guilds.cache.has(guild)) continue;
|
|
|
|
|
|
|
|
rest.put(
|
|
|
|
Routes.applicationGuildCommands(process.env.BOT_CLIENTID!, guild),
|
|
|
|
{
|
|
|
|
body: guildCommandData
|
|
|
|
}
|
|
|
|
)
|
2022-04-24 14:46:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load the events
|
|
|
|
loadEvents(client: Client, events: IEventItem[]) {
|
|
|
|
events.forEach((e) => {
|
|
|
|
client.on('channelCreate', e.Event.channelCreate);
|
|
|
|
client.on('channelDelete', e.Event.channelDelete);
|
|
|
|
client.on('channelUpdate', e.Event.channelUpdate);
|
|
|
|
client.on('guildBanAdd', e.Event.guildBanAdd);
|
|
|
|
client.on('guildBanRemove', e.Event.guildBanRemove);
|
|
|
|
client.on('guildCreate', e.Event.guildCreate);
|
|
|
|
client.on('guildMemberAdd', e.Event.guildMemberAdd);
|
|
|
|
client.on('guildMemberRemove', e.Event.guildMemberRemove);
|
|
|
|
client.on('guildMemberUpdate', e.Event.guildMemberUpdate);
|
|
|
|
client.on('messageCreate', e.Event.messageCreate);
|
|
|
|
client.on('messageDelete', e.Event.messageDelete);
|
|
|
|
client.on('messageUpdate', e.Event.messageUpdate);
|
|
|
|
client.on('ready', e.Event.ready);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|