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
+39 -36
View File
@@ -1,5 +1,8 @@
import DiscordModule, { HybridInteractionMessage } from "../utils/DiscordModule";
import { CommandInteraction, Message, Interaction } from "discord.js";
import { makeSuccessEmbed, makeProcessingEmbed, sendMessageOrInteractionResponse } from "../utils/DiscordMessage";
import { makeSuccessEmbed, makeProcessingEmbed, sendHybridInteractionMessageResponse } from "../utils/DiscordMessage";
import DiscordProvider from "../providers/Discord";
import NodePing from "ping";
import { Promise } from "bluebird";
@@ -13,7 +16,6 @@ enum MeasureType {
DiscordWebsocket = "discordwebsocket"
}
const EMBEDS = {
PING_INFO: (data: Message | Interaction, description: string) => {
return makeSuccessEmbed({
@@ -22,44 +24,43 @@ const EMBEDS = {
description: description,
user: (data instanceof Interaction) ? data.user : data.author
});
},
PINGING: (data: HybridInteractionMessage) => {
return makeProcessingEmbed({
icon: data.isMessage() ? undefined : '⌛',
title: `Measuring network performance`,
user: (data.isInteraction()) ? data.getInteraction().user : data.getMessage().author
});
}
}
export default class Ping {
export default class Ping extends DiscordModule {
async onCommand(command: string, args: any, message: Message) {
if (command.toLowerCase() !== 'ping') return;
await this.process(message, args);
public id = "Discord_Ping";
public commands = ["ping"];
public commandInteractionName = "ping";
async GuildOnModuleCommand(args: any, message: Message) {
await this.run(new HybridInteractionMessage(message), args);
}
async interactionCreate(interaction: CommandInteraction) {
if (interaction.isCommand()) {
if (typeof interaction.commandName === 'undefined') return;
if ((interaction.commandName).toLowerCase() !== 'ping') return;
await this.process(interaction, interaction.options);
}
async GuildModuleCommandInteractionCreate(interaction: CommandInteraction) {
await this.run(new HybridInteractionMessage(interaction), interaction.options);
}
async process(data: Interaction | Message, args: any) {
const isSlashCommand = data instanceof CommandInteraction && data.isCommand();
const isMessage = data instanceof Message;
if (!isSlashCommand && !isMessage) return;
let placeholder;
const loadingEmbed = makeProcessingEmbed({
icon: isMessage ? undefined : '⌛',
title: `Measuring network performance`,
user: (data instanceof Interaction) ? data.user : data.author
});
async run(data: HybridInteractionMessage, args: any) {
let placeholder: (HybridInteractionMessage | undefined);
let beforeEditDate = Date.now();
placeholder = await sendMessageOrInteractionResponse(data, { embeds: [loadingEmbed] });
let _placeholder = await sendHybridInteractionMessageResponse(data, { embeds: [EMBEDS.PINGING(data)] });
if (_placeholder)
placeholder = new HybridInteractionMessage(_placeholder);
let afterEditDate = Date.now();
let desString = [];
let finalString = [];
await Promise.map(Configuration.getConfig("Ping"), (entry: any) => {
return new Promise(async (resolve, reject) => {
@@ -70,33 +71,35 @@ export default class Ping {
if (!res.alive) {
stringCurrent += "Failed";
return desString.push(stringCurrent);
return finalString.push(stringCurrent);
}
if (res.avg === 'unknown' || res.min === 'unknown' || res.max === 'unknown') {
stringCurrent += "Failed";
return desString.push(stringCurrent);
return finalString.push(stringCurrent);
}
stringCurrent += `${parseFloat(res.avg).toFixed(1)}ms (${parseFloat(res.min).toFixed(1)}ms - ${parseFloat(res.max).toFixed(1)}ms)`;
desString.push(stringCurrent);
finalString.push(stringCurrent);
} else if (entry.type === MeasureType.DiscordWebsocket) {
stringCurrent += `${Math.round(DiscordProvider.client.ws.ping).toFixed(1)}ms`;
desString.push(stringCurrent);
finalString.push(stringCurrent);
} else if (entry.type === MeasureType.DiscordHTTPPing) {
stringCurrent += `${Math.abs(afterEditDate - beforeEditDate).toFixed(1)}ms`;
desString.push(stringCurrent);
finalString.push(stringCurrent);
}
resolve();
});
}, { concurrency: 5 });
desString.push("");
desString.push("💻 Running on " + `${os.hostname()}${Environment.get().NODE_ENV === "development" ? ' / Development Environment' : ''}`);
finalString.push("");
finalString.push("💻 Running on " + `${os.hostname()}${Environment.get().NODE_ENV === "development" ? ' / Development Environment' : ''}`);
if (isSlashCommand) return await data.editReply({ embeds: [EMBEDS.PING_INFO(data, desString.join('\n'))] });
else if (typeof placeholder !== "undefined" && isMessage && placeholder instanceof Message) return await placeholder.edit({ embeds: [EMBEDS.PING_INFO(data, desString.join('\n'))] });
if (data.isSlashCommand())
return await data.getMessageComponentInteraction().editReply({ embeds: [EMBEDS.PING_INFO(data.getRaw(), finalString.join('\n'))] });
else if (data && data.isMessage() && placeholder && placeholder.isMessage())
return await placeholder.getMessage().edit({ embeds: [EMBEDS.PING_INFO(data.getRaw(), finalString.join('\n'))] });
}
}