Files
Yumi/src/discord/InteractionManager.ts
T

232 lines
9.5 KiB
TypeScript
Raw Normal View History

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