3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-05-21 16:55:03 +00:00
zeppelin/backend/src/plugins/Utility/commands/PingCmd.ts
Miikka 86171cafd8 Merge pull request #209 from ghost/master
Add pong as an alias of ping
2021-08-14 17:30:05 +03:00

46 lines
1.4 KiB
TypeScript

import { Message } from "discord.js";
import { noop, trimLines } from "../../../utils";
import { utilityCmd } from "../types";
const { performance } = require("perf_hooks");
export const PingCmd = utilityCmd({
trigger: ["ping", "pong"],
description: "Test the bot's ping to the Discord API",
permission: "can_ping",
async run({ message: msg, pluginData }) {
const times: number[] = [];
const messages: Message[] = [];
let msgToMsgDelay: number | undefined;
for (let i = 0; i < 4; i++) {
const start = performance.now();
const message = await msg.channel.send(`Calculating ping... ${i + 1}`);
times.push(performance.now() - start);
messages.push(message);
if (msgToMsgDelay === undefined) {
msgToMsgDelay = message.createdTimestamp - msg.createdTimestamp;
}
}
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);
msg.channel.send(
trimLines(`
**Ping:**
Lowest: **${lowest}ms**
Highest: **${highest}ms**
Mean: **${mean}ms**
Time between ping command and first reply: **${msgToMsgDelay!}ms**
Shard latency: **${pluginData.client.ws.ping}ms**
`),
);
// Clean up test messages
msg.channel.bulkDelete(messages).catch(noop);
},
});