Formatted code and clean up

This commit is contained in:
2022-06-06 14:59:53 +07:00
parent 8b066dde58
commit 573040362d
29 changed files with 2208 additions and 1556 deletions
+74 -51
View File
@@ -1,9 +1,19 @@
import DiscordModule, { HybridInteractionMessage } from "../utils/DiscordModule";
import { Message, Interaction, CommandInteraction } from 'discord.js';
import { Message, Interaction, CommandInteraction } from "discord.js";
import { makeInfoEmbed, makeErrorEmbed, makeSuccessEmbed, makeProcessingEmbed, sendHybridInteractionMessageResponse } from "../utils/DiscordMessage";
import {registerAllGuildsCommands, unregisterAllGuildsCommands} from "../utils/DiscordInteraction";
import Users from "../services/Users";
import Users from '../services/Users';
import DiscordModule, { HybridInteractionMessage } from '../utils/DiscordModule';
import {
makeInfoEmbed,
makeErrorEmbed,
makeSuccessEmbed,
makeProcessingEmbed,
sendHybridInteractionMessageResponse
} from '../utils/DiscordMessage';
import {
registerAllGuildsCommands,
unregisterAllGuildsCommands
} from '../utils/DiscordInteraction';
const EMBEDS = {
INTERACTION_INFO: (data: Message | Interaction) => {
@@ -16,105 +26,118 @@ const EMBEDS = {
value: '``reloadAll``'
}
],
user: (data instanceof Interaction) ? data.user : data.author
user: data instanceof Interaction ? data.user : data.author
});
},
PROCESSING: (data: Message | Interaction) => {
return makeProcessingEmbed({
icon: (data instanceof Message) ? undefined : '⌛',
icon: data instanceof Message ? undefined : '⌛',
title: `Performing actions`,
user: (data instanceof Interaction) ? data.user : data.author
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
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
user: data instanceof Interaction ? data.user : data.author
});
},
RELOADALL_ERROR: (data: Message | Interaction, err: any) => {
return makeErrorEmbed ({
return makeErrorEmbed({
title: 'An error occurred while trying to reload interaction',
description: '```' + err + '```',
user: (data instanceof Interaction) ? data.user : data.author
})
user: data instanceof Interaction ? data.user : data.author
});
}
}
};
export default class InteractionManager extends DiscordModule {
public id = "Discord_InteractionManager";
public commands = ["interaction"];
public commandInteractionName = "interaction";
public id = 'Discord_InteractionManager';
public commands = ['interaction'];
public commandInteractionName = 'interaction';
async GuildOnModuleCommand(args: any, message: Message) {
await this.run(new HybridInteractionMessage(message), args);
}
async GuildModuleCommandInteractionCreate(interaction: CommandInteraction) {
async GuildModuleCommandInteractionCreate(interaction: CommandInteraction) {
await this.run(new HybridInteractionMessage(interaction), interaction.options);
}
async run(data: HybridInteractionMessage, args: any) {
const user = data.getUser();
if(!user) return;
if (!user) return;
if(!Users.isDeveloper(user.id))
return await sendHybridInteractionMessageResponse(data, { embeds:[EMBEDS.NOT_DEVELOPER(data.getRaw())] });
if (!Users.isDeveloper(user.id))
return await sendHybridInteractionMessageResponse(data, {
embeds: [EMBEDS.NOT_DEVELOPER(data.getRaw())]
});
const funct = {
reloadAll: async(data: HybridInteractionMessage) => {
reloadAll: async (data: HybridInteractionMessage) => {
let placeholder: HybridInteractionMessage | undefined;
let placeholder: (HybridInteractionMessage | undefined);
let _placeholder = await sendHybridInteractionMessageResponse(data, { embeds: [EMBEDS.PROCESSING(data.getRaw())] });
if (_placeholder)
placeholder = new HybridInteractionMessage(_placeholder);
let _placeholder = await sendHybridInteractionMessageResponse(data, {
embeds: [EMBEDS.PROCESSING(data.getRaw())]
});
if (_placeholder) placeholder = new HybridInteractionMessage(_placeholder);
try {
await unregisterAllGuildsCommands();
await registerAllGuildsCommands();
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);
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
);
} catch (err) {
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);
}
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
);
}
}
}
};
let query;
if(data.isMessage()) {
if(args.length === 0)
return await sendHybridInteractionMessageResponse(data, { embeds: [EMBEDS.INTERACTION_INFO(data.getRaw())] });
if (data.isMessage()) {
if (args.length === 0)
return await sendHybridInteractionMessageResponse(data, {
embeds: [EMBEDS.INTERACTION_INFO(data.getRaw())]
});
query = args[0].toLowerCase();
}
else if(data.isSlashCommand()) {
} else if (data.isSlashCommand()) {
query = args.getSubcommand();
}
switch(query) {
case "info":
return await sendHybridInteractionMessageResponse(data, { embeds: [EMBEDS.INTERACTION_INFO(data.getRaw())] });
case "reloadall":
switch (query) {
case 'info':
return await sendHybridInteractionMessageResponse(data, {
embeds: [EMBEDS.INTERACTION_INFO(data.getRaw())]
});
case 'reloadall':
return await funct.reloadAll(data);
}
}
}
}