2022-01-25 19:55:26 +07:00
|
|
|
import { CommandInteraction, Message, Interaction } from "discord.js";
|
|
|
|
|
import { makeSuccessEmbed, makeProcessingEmbed, sendMessageOrInteractionResponse } from "../utils/DiscordMessage";
|
|
|
|
|
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
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default class Ping {
|
|
|
|
|
|
|
|
|
|
async onCommand(command: string, args: any, message: Message) {
|
2022-02-02 21:17:51 +07:00
|
|
|
if (command.toLowerCase() !== 'ping') return;
|
2022-01-25 19:55:26 +07:00
|
|
|
await this.process(message, args);
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-02 21:17:51 +07:00
|
|
|
async interactionCreate(interaction: CommandInteraction) {
|
|
|
|
|
if (interaction.isCommand()) {
|
|
|
|
|
if (typeof interaction.commandName === 'undefined') return;
|
|
|
|
|
if ((interaction.commandName).toLowerCase() !== 'ping') return;
|
2022-01-25 19:55:26 +07:00
|
|
|
await this.process(interaction, interaction.options);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async process(data: Interaction | Message, args: any) {
|
|
|
|
|
const isSlashCommand = data instanceof CommandInteraction && data.isCommand();
|
|
|
|
|
const isMessage = data instanceof Message;
|
|
|
|
|
|
2022-02-02 21:17:51 +07:00
|
|
|
if (!isSlashCommand && !isMessage) return;
|
2022-01-25 19:55:26 +07:00
|
|
|
|
|
|
|
|
let placeholder;
|
|
|
|
|
|
|
|
|
|
const loadingEmbed = makeProcessingEmbed({
|
|
|
|
|
icon: isMessage ? undefined : '⌛',
|
|
|
|
|
title: `Measuring network performance`,
|
|
|
|
|
user: (data instanceof Interaction) ? data.user : data.author
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
let beforeEditDate = Date.now();
|
2022-02-02 21:17:51 +07:00
|
|
|
placeholder = await sendMessageOrInteractionResponse(data, { embeds: [loadingEmbed] });
|
2022-01-25 19:55:26 +07:00
|
|
|
let afterEditDate = Date.now();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let desString = [];
|
|
|
|
|
|
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";
|
|
|
|
|
return desString.push(stringCurrent);
|
|
|
|
|
}
|
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";
|
|
|
|
|
return desString.push(stringCurrent);
|
|
|
|
|
}
|
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)`;
|
|
|
|
|
desString.push(stringCurrent);
|
|
|
|
|
} else if (entry.type === MeasureType.DiscordWebsocket) {
|
|
|
|
|
stringCurrent += `${Math.round(DiscordProvider.client.ws.ping).toFixed(1)}ms`;
|
|
|
|
|
desString.push(stringCurrent);
|
|
|
|
|
} else if (entry.type === MeasureType.DiscordHTTPPing) {
|
|
|
|
|
stringCurrent += `${Math.abs(afterEditDate - beforeEditDate).toFixed(1)}ms`;
|
|
|
|
|
desString.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
|
|
|
|
|
|
|
|
desString.push("");
|
|
|
|
|
desString.push("💻 Running on " + `${os.hostname()}${Environment.get().NODE_ENV === "development" ? ' / Development Environment' : ''}`);
|
|
|
|
|
|
2022-02-02 21:17:51 +07:00
|
|
|
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'))] });
|
2022-01-25 19:55:26 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|