2020-07-06 01:51:48 +03:00
|
|
|
import { utilityCmd } from "../types";
|
|
|
|
import { noop, trimLines } from "../../../utils";
|
|
|
|
import { Message } from "eris";
|
|
|
|
|
|
|
|
const { performance } = require("perf_hooks");
|
|
|
|
|
|
|
|
export const PingCmd = utilityCmd({
|
|
|
|
trigger: "ping",
|
|
|
|
description: "Test the bot's ping to the Discord API",
|
|
|
|
permission: "can_ping",
|
|
|
|
|
|
|
|
async run({ message: msg, pluginData }) {
|
2020-11-09 20:03:57 +02:00
|
|
|
const times: number[] = [];
|
2020-07-06 01:51:48 +03:00
|
|
|
const messages: Message[] = [];
|
2020-11-09 20:03:57 +02:00
|
|
|
let msgToMsgDelay: number | undefined;
|
2020-07-06 01:51:48 +03:00
|
|
|
|
|
|
|
for (let i = 0; i < 4; i++) {
|
|
|
|
const start = performance.now();
|
|
|
|
const message = await msg.channel.createMessage(`Calculating ping... ${i + 1}`);
|
|
|
|
times.push(performance.now() - start);
|
|
|
|
messages.push(message);
|
|
|
|
|
2020-11-09 20:03:57 +02:00
|
|
|
if (msgToMsgDelay === undefined) {
|
2020-07-06 01:51:48 +03:00
|
|
|
msgToMsgDelay = message.timestamp - msg.timestamp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const highest = Math.round(Math.max(...times));
|
|
|
|
const lowest = Math.round(Math.min(...times));
|
|
|
|
const mean = Math.round(times.reduce((total, ms) => total + ms, 0) / times.length);
|
|
|
|
|
2020-11-09 20:03:57 +02:00
|
|
|
const shard = pluginData.client.shards.get(pluginData.client.guildShardMap[pluginData.guild.id])!;
|
2020-07-06 01:51:48 +03:00
|
|
|
|
|
|
|
msg.channel.createMessage(
|
|
|
|
trimLines(`
|
|
|
|
**Ping:**
|
|
|
|
Lowest: **${lowest}ms**
|
|
|
|
Highest: **${highest}ms**
|
|
|
|
Mean: **${mean}ms**
|
2020-11-09 20:03:57 +02:00
|
|
|
Time between ping command and first reply: **${msgToMsgDelay!}ms**
|
2020-07-06 01:51:48 +03:00
|
|
|
Shard latency: **${shard.latency}ms**
|
|
|
|
`),
|
|
|
|
);
|
|
|
|
|
|
|
|
// Clean up test messages
|
|
|
|
pluginData.client
|
|
|
|
.deleteMessages(
|
|
|
|
messages[0].channel.id,
|
|
|
|
messages.map(m => m.id),
|
|
|
|
)
|
|
|
|
.catch(noop);
|
|
|
|
},
|
|
|
|
});
|