From a71893005f8f1827470b84f35b74d3598170431c Mon Sep 17 00:00:00 2001 From: Dragon Fire Date: Sun, 31 May 2020 18:38:37 -0400 Subject: [PATCH] Import/Export Command Leaderboard --- README.md | 4 +- commands/util/command-leaderboard-export.js | 35 +++++++++++++++++ commands/util/command-leaderboard-import.js | 42 +++++++++++++++++++++ package.json | 2 +- types/json-file.js | 27 +++++++++++++ 5 files changed, 108 insertions(+), 2 deletions(-) create mode 100644 commands/util/command-leaderboard-export.js create mode 100644 commands/util/command-leaderboard-import.js create mode 100644 types/json-file.js diff --git a/README.md b/README.md index 2af3ec75..970f8c1f 100644 --- a/README.md +++ b/README.md @@ -224,13 +224,15 @@ in the appropriate channel's topic to use it. ## Commands -Total: 449 +Total: 451 ### 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-export:** Exports a command leaderboard JSON file. (Owner-Only) +* **command-leaderboard-import:** Imports a command leaderboard JSON file. (Owner-Only) * **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. diff --git a/commands/util/command-leaderboard-export.js b/commands/util/command-leaderboard-export.js new file mode 100644 index 00000000..95ff5091 --- /dev/null +++ b/commands/util/command-leaderboard-export.js @@ -0,0 +1,35 @@ +const Command = require('../../structures/Command'); + +module.exports = class CommandLeaderboardExportCommand extends Command { + constructor(client) { + super(client, { + name: 'command-leaderboard-export', + aliases: [ + 'cmd-lb-export', + 'cmd-leaderboard-export', + 'command-lb-export', + 'export-cmd-lb', + 'export-cmd-leaderboard', + 'export-command-lb', + 'export-command-leaderboard' + ], + group: 'util', + memberName: 'command-leaderboard-export', + description: 'Exports a command leaderboard JSON file.', + details: 'Only the bot owner(s) may use this command.', + ownerOnly: true, + guarded: true + }); + } + + run(msg) { + let text = '{'; + for (const command of this.client.registry.commands.values()) { + if (command.uses === undefined) continue; + text += `"${command.name}":${command.uses},`; + } + text = text.slice(0, -1); + text += '}'; + return msg.say({ files: [{ attachment: Buffer.from(text), name: 'command-leaderboard.json' }] }); + } +}; diff --git a/commands/util/command-leaderboard-import.js b/commands/util/command-leaderboard-import.js new file mode 100644 index 00000000..5de184aa --- /dev/null +++ b/commands/util/command-leaderboard-import.js @@ -0,0 +1,42 @@ +const Command = require('../../structures/Command'); + +module.exports = class CommandLeaderboardImportCommand extends Command { + constructor(client) { + super(client, { + name: 'command-leaderboard-import', + aliases: [ + 'cmd-lb-import', + 'cmd-leaderboard-import', + 'command-lb-import', + 'import-cmd-lb', + 'import-cmd-leaderboard', + 'import-command-lb', + 'import-command-leaderboard' + ], + group: 'util', + memberName: 'command-leaderboard-import', + description: 'Imports a command leaderboard JSON file.', + details: 'Only the bot owner(s) may use this command.', + ownerOnly: true, + guarded: true, + args: [ + { + key: 'file', + prompt: 'What file do you want to provide?', + type: 'json-file' + } + ] + }); + } + + run(msg, { file }) { + if (typeof file !== 'object' || Array.isArray(file)) return msg.reply('Please provide a valid JSON file.'); + for (const [id, value] of Object.entries(file)) { + if (typeof value !== 'number') continue; + const found = this.client.registry.commands.get(id); + if (!found || found.uses === undefined) continue; + found.uses += value; + } + return msg.say('Successfully imported command leaderboard.'); + } +}; diff --git a/package.json b/package.json index a684b46e..9d2bf7d5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xiao", - "version": "115.0.0", + "version": "115.1.0", "description": "Your personal server companion.", "main": "Xiao.js", "scripts": { diff --git a/types/json-file.js b/types/json-file.js new file mode 100644 index 00000000..30bb3ab0 --- /dev/null +++ b/types/json-file.js @@ -0,0 +1,27 @@ +const { ArgumentType } = require('discord.js-commando'); +const fileTypeRe = /\.(json)$/i; +const request = require('node-superfetch'); + +module.exports = class JsonFileArgumentType extends ArgumentType { + constructor(client) { + super(client, 'json-file'); + } + + validate(value, msg) { + const attachment = msg.attachments.first(); + if (!attachment) return false; + if (!fileTypeRe.test(attachment.name)) return 'Please provide a JSON file.'; + return true; + } + + async parse(value, msg) { + const attachment = msg.attachments.first(); + const { body } = await request.get(attachment.url); + return body; + } + + isEmpty(value, msg, arg) { + if (msg.attachments.size) return false; + return this.client.registry.types.get('user').isEmpty(value, msg, arg); + } +};