Files
Yumi/src/discord/Ping.ts
T

105 lines
4.2 KiB
TypeScript
Raw Normal View History

2022-03-03 20:56:49 +07:00
import DiscordModule, { HybridInteractionMessage } from "../utils/DiscordModule";
2022-01-25 19:55:26 +07:00
import { CommandInteraction, Message, Interaction } from "discord.js";
2022-03-03 20:56:49 +07:00
import { makeSuccessEmbed, makeProcessingEmbed, sendHybridInteractionMessageResponse } from "../utils/DiscordMessage";
2022-01-25 19:55:26 +07:00
import DiscordProvider from "../providers/Discord";
import NodePing from "ping";
2022-02-28 11:50:47 +07:00
import { Promise } from "bluebird";
2022-01-25 19:55:26 +07:00
import os from "os";
import Environment from "../providers/Environment";
2022-02-27 17:32:18 +07:00
import Configuration from "../providers/Configuration";
2022-01-25 19:55:26 +07:00
enum MeasureType {
Ping = "ping",
DiscordHTTPPing = "discordhttp",
DiscordWebsocket = "discordwebsocket"
}
const EMBEDS = {
PING_INFO: (data: Message | Interaction, description: string) => {
return makeSuccessEmbed({
icon: '🌎',
title: `Network performance`,
description: description,
user: (data instanceof Interaction) ? data.user : data.author
});
2022-03-03 20:56:49 +07:00
},
PINGING: (data: HybridInteractionMessage) => {
return makeProcessingEmbed({
icon: data.isMessage() ? undefined : '⌛',
title: `Measuring network performance`,
user: (data.isInteraction()) ? data.getInteraction().user : data.getMessage().author
});
2022-01-25 19:55:26 +07:00
}
}
2022-03-03 20:56:49 +07:00
export default class Ping extends DiscordModule {
2022-01-25 19:55:26 +07:00
2022-03-03 20:56:49 +07:00
public id = "Discord_Ping";
public commands = ["ping"];
public commandInteractionName = "ping";
async GuildOnModuleCommand(args: any, message: Message) {
await this.run(new HybridInteractionMessage(message), args);
2022-01-25 19:55:26 +07:00
}
2022-03-03 20:56:49 +07:00
async GuildModuleCommandInteractionCreate(interaction: CommandInteraction) {
await this.run(new HybridInteractionMessage(interaction), interaction.options);
2022-01-25 19:55:26 +07:00
}
2022-03-03 20:56:49 +07:00
async run(data: HybridInteractionMessage, args: any) {
let placeholder: (HybridInteractionMessage | undefined);
2022-01-25 19:55:26 +07:00
let beforeEditDate = Date.now();
2022-03-03 20:56:49 +07:00
let _placeholder = await sendHybridInteractionMessageResponse(data, { embeds: [EMBEDS.PINGING(data)] });
if (_placeholder)
placeholder = new HybridInteractionMessage(_placeholder);
2022-01-25 19:55:26 +07:00
let afterEditDate = Date.now();
2022-03-03 20:56:49 +07:00
let finalString = [];
2022-01-25 19:55:26 +07:00
2022-02-27 17:32:18 +07:00
await Promise.map(Configuration.getConfig("Ping"), (entry: any) => {
return new Promise(async (resolve, reject) => {
let stringCurrent = `${entry.title}`;
2022-01-25 19:55:26 +07:00
2022-02-27 17:32:18 +07:00
if (entry.type === MeasureType.Ping) {
const res = await NodePing.promise.probe(entry.host!, { min_reply: 4 });
2022-01-25 19:55:26 +07:00
2022-02-27 17:32:18 +07:00
if (!res.alive) {
stringCurrent += "Failed";
2022-03-03 20:56:49 +07:00
return finalString.push(stringCurrent);
2022-02-27 17:32:18 +07:00
}
2022-01-25 19:55:26 +07:00
2022-02-27 17:32:18 +07:00
if (res.avg === 'unknown' || res.min === 'unknown' || res.max === 'unknown') {
stringCurrent += "Failed";
2022-03-03 20:56:49 +07:00
return finalString.push(stringCurrent);
2022-02-27 17:32:18 +07:00
}
2022-02-02 21:17:51 +07:00
2022-02-27 17:32:18 +07:00
stringCurrent += `${parseFloat(res.avg).toFixed(1)}ms (${parseFloat(res.min).toFixed(1)}ms - ${parseFloat(res.max).toFixed(1)}ms)`;
2022-03-03 20:56:49 +07:00
finalString.push(stringCurrent);
2022-02-27 17:32:18 +07:00
} else if (entry.type === MeasureType.DiscordWebsocket) {
stringCurrent += `${Math.round(DiscordProvider.client.ws.ping).toFixed(1)}ms`;
2022-03-03 20:56:49 +07:00
finalString.push(stringCurrent);
2022-02-27 17:32:18 +07:00
} else if (entry.type === MeasureType.DiscordHTTPPing) {
stringCurrent += `${Math.abs(afterEditDate - beforeEditDate).toFixed(1)}ms`;
2022-03-03 20:56:49 +07:00
finalString.push(stringCurrent);
2022-01-25 19:55:26 +07:00
}
2022-02-02 21:17:51 +07:00
2022-02-27 17:32:18 +07:00
resolve();
});
}, { concurrency: 5 });
2022-01-25 19:55:26 +07:00
2022-03-03 20:56:49 +07:00
finalString.push("");
finalString.push("💻 Running on " + `${os.hostname()}${Environment.get().NODE_ENV === "development" ? ' / Development Environment' : ''}`);
2022-01-25 19:55:26 +07:00
2022-03-03 20:56:49 +07:00
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'))] });
2022-01-25 19:55:26 +07:00
}
}