From 2a6f9e46f08a92d0d9f061e0ad483e5eda6991e1 Mon Sep 17 00:00:00 2001 From: Dragon Fire Date: Mon, 1 Feb 2021 22:02:20 -0500 Subject: [PATCH] Add last-run leaderboard --- commands/util-public/last-run-leaderboard.js | 69 ++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 commands/util-public/last-run-leaderboard.js diff --git a/commands/util-public/last-run-leaderboard.js b/commands/util-public/last-run-leaderboard.js new file mode 100644 index 00000000..b7fd6ee8 --- /dev/null +++ b/commands/util-public/last-run-leaderboard.js @@ -0,0 +1,69 @@ +const Command = require('../../structures/Command'); +const { stripIndents } = require('common-tags'); +const moment = require('moment'); + +module.exports = class LastRunLeaderboardCommand extends Command { + constructor(client) { + super(client, { + name: 'last-run-leaderboard', + aliases: ['last-run-lb', 'lr-leaderboard', 'lr-lb'], + group: 'util-public', + memberName: 'last-run-leaderboard', + description: 'Responds with the bot\'s most recently run commands.', + guarded: true, + args: [ + { + key: 'page', + prompt: 'What page do you want to view?', + type: 'integer', + default: 1, + min: 1 + } + ] + }); + } + + run(msg, { page }) { + const commands = this.filterCommands( + this.client.registry.commands, + this.client.isOwner(msg.author), + msg.channel.nsfw + ); + const totalPages = Math.ceil(commands.size / 10); + if (page > totalPages) return msg.say(`Page ${page} does not exist (yet).`); + return msg.say(stripIndents` + __**Command Last Run Leaderboard (Page ${page}/${totalPages}):**__ + ${this.makeLeaderboard(commands, page).join('\n')} + `); + } + + makeLeaderboard(commands, page) { + let i = 0; + let previousPts = null; + let positionsMoved = 1; + return commands + .sort((a, b) => a.lastRun ? b.lastRun - a.lastRun : 1) + .map(command => { + if (previousPts === command.lastRun.toISOString()) { + positionsMoved++; + } else { + i += positionsMoved; + positionsMoved = 1; + } + previousPts = command.lastRun.toISOString(); + const displayTime = command.lastRun ? moment.utc(command.lastRun).format('MM/DD/YYYY h:mm A') : null; + return `**${i}.** ${command.name} (${displayTime || 'Never'})`; + }) + .slice((page - 1) * 10, page * 10); + } + + filterCommands(commands, owner, nsfw) { + return commands.filter(command => { + if (command.lastRun === undefined || command.unknown) return false; + if (!owner && command.hidden) return false; + if (!owner && command.ownerOnly) return false; + if (!owner && command.nsfw && !nsfw) return false; + return true; + }); + } +};