From 72c8d382c5211c7dc5aa71bdfe1704cd5f2718aa Mon Sep 17 00:00:00 2001 From: Dragon Fire Date: Sat, 23 May 2020 13:43:13 -0400 Subject: [PATCH] Command Leaderboard --- README.md | 3 +- Xiao.js | 5 +++ commands/util/command-leaderboard.js | 51 ++++++++++++++++++++++++++++ structures/Command.js | 1 + 4 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 commands/util/command-leaderboard.js diff --git a/README.md b/README.md index 88b7308c..583fba74 100644 --- a/README.md +++ b/README.md @@ -224,13 +224,14 @@ in the appropriate channel's topic to use it. ## Commands -Total: 438 +Total: 441 ### Utility: * **eval:** Executes JavaScript code. (Owner-Only) * **changelog:** Responds with the bot's latest 10 commits. * **cloc:** Responds with the bot's code line count. +* **command-leaderboard:** Responds with the bot's most used commands. * **credit:** Responds with a command's credits list. * **donate:** Responds with the bot's donation links. * **help:** Displays a list of available commands, or detailed information for a specific command. diff --git a/Xiao.js b/Xiao.js index c45876e9..ceb0cfd3 100644 --- a/Xiao.js +++ b/Xiao.js @@ -115,6 +115,11 @@ client.on('error', err => client.logger.error(err.stack)); client.on('warn', warn => client.logger.warn(warn)); +client.on('commandRun', command => { + if (command.uses === undefined) return; + command.uses++; +}); + client.on('commandError', (command, err) => client.logger.error(`[COMMAND:${command.name}]\n${err.stack}`)); client.login(XIAO_TOKEN); diff --git a/commands/util/command-leaderboard.js b/commands/util/command-leaderboard.js new file mode 100644 index 00000000..8114c713 --- /dev/null +++ b/commands/util/command-leaderboard.js @@ -0,0 +1,51 @@ +const Command = require('../../structures/Command'); +const { stripIndents } = require('common-tags'); + +module.exports = class CommandLeaderboardCommand extends Command { + constructor(client) { + super(client, { + name: 'command-leaderboard', + aliases: ['cmd-lb', 'cmd-leaderboard', 'command-lb'], + group: 'util', + memberName: 'command-leaderboard', + description: 'Responds with the bot\'s most used commands.', + guarded: true, + args: [ + { + key: 'page', + prompt: 'What page do you want to view?', + type: 'integer', + min: 1, + max: () => Math.ceil(this.client.registry.commands.size / 10) + } + ] + }); + } + + run(msg, { page }) { + return msg.say(stripIndents` + __**Command Usage Leaderboard (as of Last Restart):**__ + ${this.makeLeaderboard(page).join('\n')} + `); + } + + makeLeaderboard(page) { + let i = 0; + let previousPts = null; + let positionsMoved = 1; + return this.client.registry.commands + .filter(command => command.uses !== undefined) + .sort((a, b) => b.uses - a.uses) + .map(command => { + if (previousPts === command.uses) { + positionsMoved++; + } else { + i += positionsMoved; + positionsMoved = 1; + } + previousPts = command.uses; + return `**${i}.** ${command.name} (${command.uses} Use${command.uses === 1 ? '' : 's'})`; + }) + .slice((page - 1) * 10, page * 10); + } +}; diff --git a/structures/Command.js b/structures/Command.js index 40b59c5a..53c1054d 100644 --- a/structures/Command.js +++ b/structures/Command.js @@ -6,6 +6,7 @@ module.exports = class XiaoCommand extends Command { this.argsSingleQuotes = info.argsSingleQuotes || false; this.throttling = info.throttling || { usages: 1, duration: 2 }; + this.uses = 0; this.credit = info.credit || []; this.credit.push({ name: 'Dragon Fire',