From bb8ef2d511c22cbe8939c16e087c3c8f9483e0e7 Mon Sep 17 00:00:00 2001 From: Dragory Date: Sun, 13 Jan 2019 23:30:48 +0200 Subject: [PATCH] Add !ping utility command (admin only by default). Make !reload_guild also admin only by default. --- src/plugins/Utility.ts | 43 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/src/plugins/Utility.ts b/src/plugins/Utility.ts index a56b0a00..4cf5ae7c 100644 --- a/src/plugins/Utility.ts +++ b/src/plugins/Utility.ts @@ -12,6 +12,8 @@ import { GuildSavedMessages } from "../data/GuildSavedMessages"; import { GuildArchives } from "../data/GuildArchives"; import { ZeppelinPlugin } from "./ZeppelinPlugin"; +const { performance } = require("perf_hooks"); + const MAX_SEARCH_RESULTS = 15; const MAX_CLEAN_COUNT = 50; const CLEAN_COMMAND_DELETE_DELAY = 5000; @@ -36,7 +38,8 @@ export class UtilityPlugin extends ZeppelinPlugin { info: false, server: false, reload_guild: false, - nickname: false + nickname: false, + ping: false }, overrides: [ { @@ -48,9 +51,15 @@ export class UtilityPlugin extends ZeppelinPlugin { clean: true, info: true, server: true, - reload_guild: true, nickname: true } + }, + { + level: ">=100", + permissions: { + reload_guild: true, + ping: true + } } ] }; @@ -417,6 +426,36 @@ export class UtilityPlugin extends ZeppelinPlugin { msg.channel.createMessage({ embed }); } + @d.command("ping") + @d.permission("ping") + async pingCmd(msg: Message) { + const times = []; + const messages: Message[] = []; + + 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); + } + + const highest = Math.round(Math.max(...times)); + const lowest = Math.round(Math.min(...times)); + const mean = Math.round(times.reduce((t, v) => t + v, 0) / times.length); + + msg.channel.createMessage( + trimLines(` + **Ping:** + Lowest: **${lowest}ms** + Highest: **${highest}ms** + Mean: **${mean}ms** + `) + ); + + // Clean up test messages + this.bot.deleteMessages(messages[0].channel.id, messages.map(m => m.id)).catch(noop); + } + @d.command("reload_guild") @d.permission("reload_guild") reloadGuildCmd(msg: Message) {