Files
Yumi/src/discord/InteractionManager.ts
T

120 lines
4.8 KiB
TypeScript
Raw Normal View History

2022-03-03 20:56:49 +07:00
import DiscordModule, { HybridInteractionMessage } from "../utils/DiscordModule";
2021-09-18 06:28:41 -07:00
import { Message, Interaction, CommandInteraction } from "discord.js";
2022-03-03 20:56:49 +07:00
import { makeInfoEmbed, makeErrorEmbed, makeSuccessEmbed, makeProcessingEmbed, sendHybridInteractionMessageResponse } from "../utils/DiscordMessage";
2021-09-18 06:28:41 -07:00
import {registerAllGuildsCommands, unregisterAllGuildsCommands} from "../utils/DiscordInteraction";
import Users from "../services/Users";
const EMBEDS = {
INTERACTION_INFO: (data: Message | Interaction) => {
return makeInfoEmbed({
title: 'Interaction',
description: `This module contains management tool for interaction based contents`,
fields: [
{
name: 'Available arguments',
value: '``reloadAll``'
}
],
user: (data instanceof Interaction) ? data.user : data.author
});
},
PROCESSING: (data: Message | Interaction) => {
return makeProcessingEmbed({
icon: (data instanceof Message) ? undefined : '⌛',
title: `Performing actions`,
user: (data instanceof Interaction) ? data.user : data.author
});
},
NOT_DEVELOPER: (data: Message | Interaction) => {
return makeErrorEmbed({
title: 'Developer only',
description: `This command is restricted to the developers only`,
user: (data instanceof Interaction) ? data.user : data.author
});
},
RELOADALL_SUCCESS: (data: Message | Interaction) => {
return makeSuccessEmbed({
title: 'Reloaded all Interaction',
user: (data instanceof Interaction) ? data.user : data.author
});
},
RELOADALL_ERROR: (data: Message | Interaction, err: any) => {
return makeErrorEmbed ({
title: 'An error occurred while trying to reload interaction',
description: '```' + err + '```',
user: (data instanceof Interaction) ? data.user : data.author
})
}
}
2022-03-03 20:56:49 +07:00
export default class InteractionManager extends DiscordModule {
public id = "Discord_InteractionManager";
public commands = ["interaction"];
public commandInteractionName = "interaction";
async GuildOnModuleCommand(args: any, message: Message) {
await this.run(new HybridInteractionMessage(message), args);
2021-09-18 06:28:41 -07:00
}
2022-03-03 20:56:49 +07:00
async GuildModuleCommandInteractionCreate(interaction: CommandInteraction) {
await this.run(new HybridInteractionMessage(interaction), interaction.options);
2021-09-18 06:28:41 -07:00
}
2022-03-03 20:56:49 +07:00
async run(data: HybridInteractionMessage, args: any) {
2021-09-18 06:28:41 -07:00
2022-03-03 20:56:49 +07:00
const user = data.getUser();
if(!user) return;
2021-09-18 06:28:41 -07:00
2022-03-03 20:56:49 +07:00
if(!Users.isDeveloper(user.id))
return await sendHybridInteractionMessageResponse(data, { embeds:[EMBEDS.NOT_DEVELOPER(data.getRaw())] });
2021-09-18 06:28:41 -07:00
const funct = {
2022-03-03 20:56:49 +07:00
reloadAll: async(data: HybridInteractionMessage) => {
let placeholder: (HybridInteractionMessage | undefined);
let _placeholder = await sendHybridInteractionMessageResponse(data, { embeds: [EMBEDS.PROCESSING(data.getRaw())] });
if (_placeholder)
placeholder = new HybridInteractionMessage(_placeholder);
2021-09-18 06:28:41 -07:00
try {
await unregisterAllGuildsCommands();
await registerAllGuildsCommands();
2022-03-03 20:56:49 +07:00
if(data && data.isMessage() && placeholder && placeholder.isMessage())
return placeholder.getMessage().edit({ embeds: [EMBEDS.RELOADALL_SUCCESS(data.getRaw())] });
else if(data.isSlashCommand())
return await sendHybridInteractionMessageResponse(data, { embeds: [EMBEDS.RELOADALL_SUCCESS(data.getRaw())]}, true);
2021-09-18 06:28:41 -07:00
} catch (err) {
2022-03-03 20:56:49 +07:00
if(data && data.isMessage() && placeholder && placeholder.isMessage())
return placeholder.getMessage().edit({ embeds: [EMBEDS.RELOADALL_ERROR(data.getRaw(), err)] });
else if(data.isSlashCommand())
return await sendHybridInteractionMessageResponse(data, { embeds: [EMBEDS.RELOADALL_ERROR(data.getRaw(), err)] }, true);
2021-09-18 06:28:41 -07:00
}
}
}
let query;
2022-03-03 20:56:49 +07:00
if(data.isMessage()) {
if(args.length === 0)
return await sendHybridInteractionMessageResponse(data, { embeds: [EMBEDS.INTERACTION_INFO(data.getRaw())] });
2021-09-18 06:28:41 -07:00
2022-03-03 20:56:49 +07:00
query = args[0].toLowerCase();
2021-09-18 06:28:41 -07:00
}
2022-03-03 20:56:49 +07:00
else if(data.isSlashCommand()) {
2021-09-18 06:28:41 -07:00
query = args.getSubcommand();
}
switch(query) {
case "info":
2022-03-03 20:56:49 +07:00
return await sendHybridInteractionMessageResponse(data, { embeds: [EMBEDS.INTERACTION_INFO(data.getRaw())] });
2021-09-18 06:28:41 -07:00
case "reloadall":
return await funct.reloadAll(data);
}
}
}