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
+6
View File
@@ -70,6 +70,12 @@ client.on('ready', () => {
}
}, client.memePoster.postInterval);
}
try {
const results = client.importCommandLeaderboard();
if (!results) client.logger.error('[LEADERBOARD] command-leaderboard.json is not formatted correctly.');
} catch (err) {
client.logger.error(`[LEADERBOARD] Could not parse command-leaderboard.json:\n${err.stack}`);
}
});
client.on('message', async msg => {
+7 -28
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) {
run(msg) {
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}\`.`);
}
}
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;
}
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}\`.`);
}
}
};
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "xiao",
"version": "116.1.5",
"version": "116.1.6",
"description": "Your personal server companion.",
"main": "Xiao.js",
"scripts": {
+17
View File
@@ -2,6 +2,8 @@ const { CommandoClient } = require('discord.js-commando');
const { WebhookClient } = require('discord.js');
const Collection = require('@discordjs/collection');
const winston = require('winston');
const fs = require('fs');
const path = require('path');
const PokemonStore = require('./pokemon/PokemonStore');
const MemePosterClient = require('./MemePoster');
const activities = require('../assets/json/activity');
@@ -37,4 +39,19 @@ module.exports = class XiaoClient extends CommandoClient {
inPhoneCall(channel) {
return this.phone.some(call => call.origin.id === channel.id || call.recipient.id === channel.id);
}
importCommandLeaderboard() {
const read = fs.readFileSync(path.join(__dirname, '..', 'command-leaderboard.json'), {
encoding: 'utf8'
});
const file = JSON.parse(read);
if (typeof file !== 'object' || Array.isArray(file)) return null;
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 file;
}
};
-27
View File
@@ -1,27 +0,0 @@
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);
}
};