Add file-based import/export command-leaderboard

This commit is contained in:
Dragon Fire
2020-06-04 13:59:10 -04:00
parent 4fdfd7b124
commit 885da10b88
4 changed files with 21 additions and 2 deletions
@@ -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' }] });
}
};
+14 -1
View File
@@ -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;