Auto-Import Command Leaderboard

This commit is contained in:
Dragon Fire
2020-06-06 00:26:54 -04:00
parent a75fb109a7
commit 1d9095868d
5 changed files with 32 additions and 57 deletions
+8 -29
View File
@@ -1,6 +1,4 @@
const Command = require('../../structures/Command');
const fs = require('fs');
const path = require('path');
module.exports = class CommandLeaderboardImportCommand extends Command {
constructor(client) {
@@ -20,36 +18,17 @@ module.exports = class CommandLeaderboardImportCommand extends Command {
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',
default: ''
}
]
guarded: true
});
}
run(msg, { file }) {
if (!file) {
try {
const read = fs.readFileSync(path.join(__dirname, '..', '..', 'command-leaderboard.json'), {
encoding: 'utf8'
});
file = JSON.parse(read);
} catch (err) {
return msg.say(`Could not read \`command-leaderboard.json\`: \`${err.message}\`.`);
}
run(msg) {
try {
const results = this.client.importCommandLeaderboard();
if (!results) return msg.reply('The JSON file provided is invalid.');
return msg.say('Successfully imported command leaderboard.');
} catch (err) {
return msg.reply(`Could not read \`command-leaderboard.json\`: \`${err.message}\`.`);
}
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.');
}
};