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); }, 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 => { client.on('message', async msg => {
+8 -29
View File
@@ -1,6 +1,4 @@
const Command = require('../../structures/Command'); const Command = require('../../structures/Command');
const fs = require('fs');
const path = require('path');
module.exports = class CommandLeaderboardImportCommand extends Command { module.exports = class CommandLeaderboardImportCommand extends Command {
constructor(client) { constructor(client) {
@@ -20,36 +18,17 @@ module.exports = class CommandLeaderboardImportCommand extends Command {
description: 'Imports a command leaderboard JSON file.', description: 'Imports a command leaderboard JSON file.',
details: 'Only the bot owner(s) may use this command.', details: 'Only the bot owner(s) may use this command.',
ownerOnly: true, ownerOnly: true,
guarded: true, guarded: true
args: [
{
key: 'file',
prompt: 'What file do you want to provide?',
type: 'json-file',
default: ''
}
]
}); });
} }
run(msg, { file }) { run(msg) {
if (!file) { try {
try { const results = this.client.importCommandLeaderboard();
const read = fs.readFileSync(path.join(__dirname, '..', '..', 'command-leaderboard.json'), { if (!results) return msg.reply('The JSON file provided is invalid.');
encoding: 'utf8' return msg.say('Successfully imported command leaderboard.');
}); } catch (err) {
file = JSON.parse(read); return msg.reply(`Could not read \`command-leaderboard.json\`: \`${err.message}\`.`);
} 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;
}
return msg.say('Successfully imported command leaderboard.');
} }
}; };
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "xiao", "name": "xiao",
"version": "116.1.5", "version": "116.1.6",
"description": "Your personal server companion.", "description": "Your personal server companion.",
"main": "Xiao.js", "main": "Xiao.js",
"scripts": { "scripts": {
+17
View File
@@ -2,6 +2,8 @@ const { CommandoClient } = require('discord.js-commando');
const { WebhookClient } = require('discord.js'); const { WebhookClient } = require('discord.js');
const Collection = require('@discordjs/collection'); const Collection = require('@discordjs/collection');
const winston = require('winston'); const winston = require('winston');
const fs = require('fs');
const path = require('path');
const PokemonStore = require('./pokemon/PokemonStore'); const PokemonStore = require('./pokemon/PokemonStore');
const MemePosterClient = require('./MemePoster'); const MemePosterClient = require('./MemePoster');
const activities = require('../assets/json/activity'); const activities = require('../assets/json/activity');
@@ -37,4 +39,19 @@ module.exports = class XiaoClient extends CommandoClient {
inPhoneCall(channel) { inPhoneCall(channel) {
return this.phone.some(call => call.origin.id === channel.id || call.recipient.id === channel.id); 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);
}
};