2022-09-18 11:57:22 +01:00
|
|
|
import { Client, REST, Routes, SlashCommandBuilder } from "discord.js";
|
2022-10-09 15:23:25 +01:00
|
|
|
import { EventType } from "../constants/EventType";
|
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;
|
2023-12-16 16:50:26 +00:00
|
|
|
|
2022-09-18 11:57:22 +01:00
|
|
|
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) => {
|
2022-10-09 15:23:25 +01:00
|
|
|
switch(e.EventType) {
|
|
|
|
case EventType.ChannelCreate:
|
|
|
|
client.on('channelCreate', (channel) => e.ExecutionFunction(channel));
|
|
|
|
break;
|
|
|
|
case EventType.ChannelDelete:
|
|
|
|
client.on('channelDelete', (channel) => e.ExecutionFunction(channel));
|
|
|
|
break;
|
|
|
|
case EventType.ChannelUpdate:
|
|
|
|
client.on('channelUpdate', (channel) => e.ExecutionFunction(channel));
|
|
|
|
break;
|
|
|
|
case EventType.GuildBanAdd:
|
|
|
|
client.on('guildBanAdd', (ban) => e.ExecutionFunction(ban));
|
|
|
|
break;
|
|
|
|
case EventType.GuildBanRemove:
|
|
|
|
client.on('guildBanRemove', (ban) => e.ExecutionFunction(ban));
|
|
|
|
break;
|
|
|
|
case EventType.GuildCreate:
|
|
|
|
client.on('guildCreate', (guild) => e.ExecutionFunction(guild));
|
|
|
|
break;
|
|
|
|
case EventType.GuildMemberAdd:
|
|
|
|
client.on('guildMemberAdd', (member) => e.ExecutionFunction(member));
|
|
|
|
break;
|
|
|
|
case EventType.GuildMemberRemove:
|
|
|
|
client.on('guildMemberRemove', (member) => e.ExecutionFunction(member));
|
|
|
|
break;
|
|
|
|
case EventType.GuildMemberUpdate:
|
|
|
|
client.on('guildMemberUpdate', (oldMember, newMember) => e.ExecutionFunction(oldMember, newMember));
|
|
|
|
break;
|
|
|
|
case EventType.MessageCreate:
|
|
|
|
client.on('messageCreate', (message) => e.ExecutionFunction(message));
|
|
|
|
break;
|
|
|
|
case EventType.MessageDelete:
|
|
|
|
client.on('messageDelete', (message) => e.ExecutionFunction(message));
|
|
|
|
break;
|
|
|
|
case EventType.MessageUpdate:
|
|
|
|
client.on('messageUpdate', (oldMessage, newMessage) => e.ExecutionFunction(oldMessage, newMessage));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
console.error('Event not implemented.');
|
|
|
|
}
|
2022-04-24 14:46:37 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|