diff --git a/Xiao.js b/Xiao.js index 95089b11..31ad3e19 100644 --- a/Xiao.js +++ b/Xiao.js @@ -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 => { diff --git a/commands/util/command-leaderboard-import.js b/commands/util/command-leaderboard-import.js index 8ec20eda..22e087f3 100644 --- a/commands/util/command-leaderboard-import.js +++ b/commands/util/command-leaderboard-import.js @@ -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.'); } }; diff --git a/package.json b/package.json index a3600b04..58a7acae 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xiao", - "version": "116.1.5", + "version": "116.1.6", "description": "Your personal server companion.", "main": "Xiao.js", "scripts": { diff --git a/structures/Client.js b/structures/Client.js index 9669c298..40ce41eb 100644 --- a/structures/Client.js +++ b/structures/Client.js @@ -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; + } }; diff --git a/types/json-file.js b/types/json-file.js deleted file mode 100644 index 30bb3ab0..00000000 --- a/types/json-file.js +++ /dev/null @@ -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); - } -};