Import/Export Command Leaderboard

This commit is contained in:
Dragon Fire
2020-05-31 18:38:37 -04:00
parent 525d256596
commit a71893005f
5 changed files with 108 additions and 2 deletions
+3 -1
View File
@@ -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.
@@ -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' }] });
}
};
@@ -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.');
}
};
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "xiao",
"version": "115.0.0",
"version": "115.1.0",
"description": "Your personal server companion.",
"main": "Xiao.js",
"scripts": {
+27
View File
@@ -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);
}
};