Code refactor

This commit is contained in:
2022-03-03 20:56:49 +07:00
parent 18b0739592
commit 2c26c363ff
23 changed files with 1244 additions and 1166 deletions
+45
View File
@@ -1,6 +1,7 @@
import { MessageEmbed, User, MessagePayload, MessageOptions, GuildTextBasedChannel, TextChannel, DMChannel, PartialDMChannel, BaseGuildTextChannel, Message, ColorResolvable, Interaction, InteractionReplyOptions } from "discord.js";
import App from "..";
import Logger from "../libs/Logger";
import { HybridInteractionMessage } from "./DiscordModule";
const emotes = {
"yumiloading": "<a:yumiloading:887350424938627173>"
@@ -136,6 +137,50 @@ export async function sendMessageOrInteractionResponse(data: Message | Interacti
else if(isMessage) return await sendReply(data, payload);
}
export async function sendHybridInteractionMessageResponse(data: HybridInteractionMessage, payload: MessageOptions | InteractionReplyOptions, replace = false): (Promise<Message | Interaction | undefined>) {
if(data.isSlashCommand() || data.isButton() || data.isSelectMenu()) {
const messageComponent = data.getMessageComponentInteraction();
if(!messageComponent.replied) {
let message;
try {
if(!messageComponent.deferred) {
await messageComponent.reply(payload);
return messageComponent;
}
else {
await messageComponent.editReply(payload);
return messageComponent;
}
}
catch(errorDM) {
Logger.error(`Cannot find available destinations to send the message CID: ${messageComponent.channel!.id} UID: ${messageComponent.user.id} DM_ERR: ${errorDM}`);
return;
} finally {
return message;
}
}
else {
let message;
try {
if(replace)
return (await messageComponent.editReply(payload) as Message);
else
return (await messageComponent.followUp(payload) as Message);
}
catch(errorDM) {
Logger.error(`Cannot find available destinations to send the message CID: ${messageComponent.channel!.id} UID: ${messageComponent.user.id} DM_ERR: ${errorDM}`);
return;
} finally {
return message;
}
}
}
else if(data.isMessage()) return await sendReply(data.getMessage(), payload);
}
export function getEmotes() {
return emotes;
}
+120
View File
@@ -0,0 +1,120 @@
import { Guild, Interaction, Message, GuildMember, CommandInteraction, ButtonInteraction, TextBasedChannel, MessageComponentInteraction, User, SelectMenuInteraction } from "discord.js";
export default class DiscordModule {
public id?: string;
public commands?: (string[] | null) = null;
public commandInteractionName?: (string | null) = null;
constructor({ id, command, commandInteractionName }: {
id?: string,
command?: (string[] | null),
commandInteractionName?: (string | null)
} = {}) {
this.id = id;
this.commands = command;
this.commandInteractionName = commandInteractionName;
}
Init(): (void | Promise<void | any>) {}
Ready(): (void | Promise<void | any>) {}
GuildOnCommand(command: string, args: any, message: Message): (void | Promise<void | any>) {}
GuildOnModuleCommand(args: any, message: Message): (void | Promise<void | any>) {}
GuildInteractionCreate(interaction: Interaction): (void | Promise<void | any>) {}
GuildModuleInteractionCreate(interaction: Interaction): (void | Promise<void | any>) {}
GuildCommandInteractionCreate(interaction: CommandInteraction): (void | Promise<void | any>) {}
GuildModuleCommandInteractionCreate(interaction: CommandInteraction): (void | Promise<void | any>) {}
GuildSelectMenuInteractionCreate(interaction: SelectMenuInteraction): (void | Promise<void | any>) {}
GuildButtonInteractionCreate(interaction: ButtonInteraction): (void | Promise<void | any>) {}
GuildCreate(guild: Guild): (void | Promise<void | any>) {}
GuildMemberAdd(member: GuildMember): (void | Promise<void | any>) {}
GuildMessageCreate(message: Message): (void | Promise<void | any>) {}
}
export class HybridInteractionMessage {
public data: Interaction | Message;
constructor (data: Interaction | Message) {
this.data = data;
}
public isInteraction(): boolean {
return this.data instanceof Interaction;
}
public isSlashCommand(): boolean {
return this.data instanceof CommandInteraction && this.data.isCommand();
}
public isButton(): boolean {
return this.data instanceof ButtonInteraction && this.data.isButton();
}
public isSelectMenu(): boolean {
return this.data instanceof SelectMenuInteraction && this.data.isSelectMenu();
}
public isMessage(): boolean {
return this.data instanceof Message;
}
public getChannel(): (TextBasedChannel | null) {
return this.data.channel;
}
public getGuild(): (Guild | null) {
return this.data.guild;
}
public getUser(): (User | null) {
return (this.isInteraction()) ? this.getInteraction().user : this.getMessage().author
}
public getMember(): (GuildMember | null) {
return (this.data.member as GuildMember);
}
public getInteraction(): Interaction {
if(this.isMessage())
throw new Error("Unable to cast interaction to message");
return this.data as Interaction;
}
public getSlashCommand(): CommandInteraction {
if(this.isInteraction() && !(this.data as CommandInteraction))
throw new Error("Unable to cast to MessageComponentInteraction");
return this.data as CommandInteraction;
}
public getSelectMenu(): SelectMenuInteraction {
if(this.isInteraction() && !(this.data as SelectMenuInteraction))
throw new Error("Unable to cast to SelectMenuInteraction");
return this.data as SelectMenuInteraction;
}
public getMessageComponentInteraction(): MessageComponentInteraction {
if(this.isInteraction() && !(this.data as MessageComponentInteraction))
throw new Error("Unable to cast to MessageComponentInteraction");
return this.data as MessageComponentInteraction;
}
public getMessage(): Message {
if(this.isInteraction())
throw new Error("Unable to cast message to interaction");
return this.data as Message;
}
public getRaw(): (Interaction | Message) {
return this.data;
}
}