mirror of
https://github.com/kirameki-cafe/Yumi.git
synced 2026-09-13 10:49:20 +00:00
Optimized speed and use new configuration
This commit is contained in:
+26
-41
@@ -2,11 +2,11 @@ import { CommandInteraction, Message, Interaction } from "discord.js";
|
|||||||
import { makeSuccessEmbed, makeProcessingEmbed, sendMessageOrInteractionResponse } from "../utils/DiscordMessage";
|
import { makeSuccessEmbed, makeProcessingEmbed, sendMessageOrInteractionResponse } from "../utils/DiscordMessage";
|
||||||
import DiscordProvider from "../providers/Discord";
|
import DiscordProvider from "../providers/Discord";
|
||||||
import NodePing from "ping";
|
import NodePing from "ping";
|
||||||
import fs from "fs";
|
import { Promise, reject } from "bluebird";
|
||||||
import path from "path";
|
|
||||||
import os from "os";
|
import os from "os";
|
||||||
import Logger from "../libs/Logger";
|
import Logger from "../libs/Logger";
|
||||||
import Environment from "../providers/Environment";
|
import Environment from "../providers/Environment";
|
||||||
|
import Configuration from "../providers/Configuration";
|
||||||
|
|
||||||
enum MeasureType {
|
enum MeasureType {
|
||||||
Ping = "ping",
|
Ping = "ping",
|
||||||
@@ -14,7 +14,6 @@ enum MeasureType {
|
|||||||
DiscordWebsocket = "discordwebsocket"
|
DiscordWebsocket = "discordwebsocket"
|
||||||
}
|
}
|
||||||
|
|
||||||
let measureList: any = [];
|
|
||||||
|
|
||||||
const EMBEDS = {
|
const EMBEDS = {
|
||||||
PING_INFO: (data: Message | Interaction, description: string) => {
|
PING_INFO: (data: Message | Interaction, description: string) => {
|
||||||
@@ -29,20 +28,6 @@ const EMBEDS = {
|
|||||||
|
|
||||||
export default class Ping {
|
export default class Ping {
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
async onCommand(command: string, args: any, message: Message) {
|
async onCommand(command: string, args: any, message: Message) {
|
||||||
if (command.toLowerCase() !== 'ping') return;
|
if (command.toLowerCase() !== 'ping') return;
|
||||||
await this.process(message, args);
|
await this.process(message, args);
|
||||||
@@ -77,36 +62,36 @@ export default class Ping {
|
|||||||
|
|
||||||
let desString = [];
|
let desString = [];
|
||||||
|
|
||||||
// TODO: Optimize speed of this
|
await Promise.map(Configuration.getConfig("Ping"), (entry: any) => {
|
||||||
|
return new Promise(async (resolve, reject) => {
|
||||||
|
let stringCurrent = `${entry.title}`;
|
||||||
|
|
||||||
for (let entry of measureList) {
|
if (entry.type === MeasureType.Ping) {
|
||||||
|
const res = await NodePing.promise.probe(entry.host!, { min_reply: 4 });
|
||||||
|
|
||||||
let stringCurrent = `${entry.title}`;
|
if (!res.alive) {
|
||||||
|
stringCurrent += "Failed";
|
||||||
|
return desString.push(stringCurrent);
|
||||||
|
}
|
||||||
|
|
||||||
if (entry.type === MeasureType.Ping) {
|
if (res.avg === 'unknown' || res.min === 'unknown' || res.max === 'unknown') {
|
||||||
const res = await NodePing.promise.probe(entry.host!, { min_reply: 4 });
|
stringCurrent += "Failed";
|
||||||
|
return desString.push(stringCurrent);
|
||||||
|
}
|
||||||
|
|
||||||
if (!res.alive) {
|
stringCurrent += `${parseFloat(res.avg).toFixed(1)}ms (${parseFloat(res.min).toFixed(1)}ms - ${parseFloat(res.max).toFixed(1)}ms)`;
|
||||||
stringCurrent += "Failed";
|
desString.push(stringCurrent);
|
||||||
return 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);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (res.avg === 'unknown' || res.min === 'unknown' || res.max === 'unknown') {
|
resolve();
|
||||||
stringCurrent += "Failed";
|
});
|
||||||
return desString.push(stringCurrent);
|
}, { concurrency: 5 });
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
desString.push("");
|
desString.push("");
|
||||||
desString.push("💻 Running on " + `${os.hostname()}${Environment.get().NODE_ENV === "development" ? ' / Development Environment' : ''}`);
|
desString.push("💻 Running on " + `${os.hostname()}${Environment.get().NODE_ENV === "development" ? ' / Development Environment' : ''}`);
|
||||||
|
|||||||
Reference in New Issue
Block a user