From 885da10b88115ac223a16459f11faf7d1bdc2b28 Mon Sep 17 00:00:00 2001 From: Dragon Fire Date: Thu, 4 Jun 2020 13:59:10 -0400 Subject: [PATCH] Add file-based import/export command-leaderboard --- .gitignore | 1 + commands/util/command-leaderboard-export.js | 5 +++++ commands/util/command-leaderboard-import.js | 15 ++++++++++++++- package.json | 2 +- 4 files changed, 21 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 94f05d29..354cf5de 100644 --- a/.gitignore +++ b/.gitignore @@ -10,6 +10,7 @@ report*.json # Config Files .env config.json +command-leaderboard.json # In-Development Commands commands/games-sp/puyo-puyo.js diff --git a/commands/util/command-leaderboard-export.js b/commands/util/command-leaderboard-export.js index 95ff5091..2794450a 100644 --- a/commands/util/command-leaderboard-export.js +++ b/commands/util/command-leaderboard-export.js @@ -1,4 +1,6 @@ const Command = require('../../structures/Command'); +const fs = require('fs'); +const path = require('path'); module.exports = class CommandLeaderboardExportCommand extends Command { constructor(client) { @@ -30,6 +32,9 @@ module.exports = class CommandLeaderboardExportCommand extends Command { } text = text.slice(0, -1); text += '}'; + fs.writeFileSync(path.join(__dirname, '..', '..', 'command-leaderboard.json'), Buffer.from(text), { + encoding: 'utf8' + }); return msg.say({ files: [{ attachment: Buffer.from(text), name: 'command-leaderboard.json' }] }); } }; diff --git a/commands/util/command-leaderboard-import.js b/commands/util/command-leaderboard-import.js index 5de184aa..884690d7 100644 --- a/commands/util/command-leaderboard-import.js +++ b/commands/util/command-leaderboard-import.js @@ -1,4 +1,6 @@ const Command = require('../../structures/Command'); +const fs = require('fs'); +const path = require('path'); module.exports = class CommandLeaderboardImportCommand extends Command { constructor(client) { @@ -23,13 +25,24 @@ module.exports = class CommandLeaderboardImportCommand extends Command { { key: 'file', prompt: 'What file do you want to provide?', - type: 'json-file' + type: 'json-file', + default: '' } ] }); } 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}`); + } + } 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; diff --git a/package.json b/package.json index dddf8371..b8915cf2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xiao", - "version": "116.1.1", + "version": "116.1.2", "description": "Your personal server companion.", "main": "Xiao.js", "scripts": {