Files
Yumi/src/discord/Ping.ts
T

118 lines
4.3 KiB
TypeScript
Raw Normal View History

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";
import fs from "fs";
import path from "path";
import os from "os";
import Logger from "../libs/Logger";
import Environment from "../providers/Environment";
enum MeasureType {
Ping = "ping",
DiscordHTTPPing = "discordhttp",
DiscordWebsocket = "discordwebsocket"
}
2022-02-02 21:17:51 +07:00
let measureList: any = [];
2022-01-25 19:55:26 +07:00
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 {
2022-02-02 21:17:51 +07:00
async init() {
if (fs.existsSync(path.join(process.cwd(), 'configs/Ping.json'))) {
try {
const rawData = fs.readFileSync(path.join(process.cwd(), 'configs/Ping.json'));
const jsonData = JSON.parse(rawData.toString());
measureList = jsonData;
} catch (err) {
Logger.error("Unable to load custom Ping config: " + err);
}
}
}
2022-01-25 19:55:26 +07:00
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 = [];
// TODO: Optimize speed of this
2022-02-02 21:17:51 +07:00
for (let entry of measureList) {
2022-01-25 19:55:26 +07:00
let stringCurrent = `${entry.title}`;
2022-02-02 21:17:51 +07:00
if (entry.type === MeasureType.Ping) {
2022-01-25 19:55:26 +07:00
const res = await NodePing.promise.probe(entry.host!, { min_reply: 4 });
2022-02-02 21:17:51 +07:00
if (!res.alive) {
2022-01-25 19:55:26 +07:00
stringCurrent += "Failed";
return desString.push(stringCurrent);
}
2022-02-02 21:17:51 +07:00
if (res.avg === 'unknown' || res.min === 'unknown' || res.max === 'unknown') {
2022-01-25 19:55:26 +07:00
stringCurrent += "Failed";
return desString.push(stringCurrent);
}
2022-02-02 21:17:51 +07:00
2022-01-25 19:55:26 +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-02-02 21:17:51 +07:00
}
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
}
}