From ab8c828b2c37037b1b949051a608e12b73ee0717 Mon Sep 17 00:00:00 2001 From: Daniel Odendahl Jr Date: Mon, 12 Mar 2018 18:38:56 +0000 Subject: [PATCH] decode base64 --- commands/text-edit/base64.js | 18 +++++++++++++++--- package.json | 2 +- util/Util.js | 6 ++++-- 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/commands/text-edit/base64.js b/commands/text-edit/base64.js index b1df79d6..05592468 100644 --- a/commands/text-edit/base64.js +++ b/commands/text-edit/base64.js @@ -1,5 +1,6 @@ const { Command } = require('discord.js-commando'); -const { base64 } = require('../../util/Util'); +const { list, base64 } = require('../../util/Util'); +const modes = ['encode', 'decode']; module.exports = class Base64Command extends Command { constructor(client) { @@ -9,7 +10,18 @@ module.exports = class Base64Command extends Command { group: 'text-edit', memberName: 'base64', description: 'Converts text to Base64.', + details: `**Modes**: ${modes.join(', ')}`, args: [ + { + key: 'mode', + prompt: `Would you like to ${list(modes, 'or')}?`, + type: 'string', + validate: mode => { + if (modes.includes(mode.toLowerCase())) return true; + return `Invalid mode, please enter either ${list(modes, 'or')}.`; + }, + parse: mode => mode.toLowerCase() + }, { key: 'text', prompt: 'What text would you like to convert to Base64?', @@ -23,7 +35,7 @@ module.exports = class Base64Command extends Command { }); } - run(msg, { text }) { - return msg.say(base64(text)); + run(msg, { mode, text }) { + return msg.say(base64(text, mode)); } }; diff --git a/package.json b/package.json index c62f9a7a..d2dbdcf2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xiao", - "version": "68.0.0", + "version": "69.0.0", "description": "Your personal server companion.", "main": "Xiao.js", "scripts": { diff --git a/util/Util.js b/util/Util.js index 7936b8c7..bbd9a41d 100644 --- a/util/Util.js +++ b/util/Util.js @@ -54,8 +54,10 @@ class Util { return arr; } - static base64(text) { - return Buffer.from(text).toString('base64'); + static base64(text, mode = 'encode') { + if (mode === 'encode') return Buffer.from(text).toString('base64'); + if (mode === 'decode') return Buffer.from(text, 'base64').toString('utf8'); + throw new TypeError(`${mode} is not a supported base64 mode.`); } static hash(text, algorithm) {