Animal Crossing speak, morse encode/decode

This commit is contained in:
Daniel Odendahl Jr
2017-12-20 15:24:51 +00:00
parent c9c4d6cabe
commit 1c684fa7f1
41 changed files with 83 additions and 9 deletions
+21 -4
View File
@@ -1,5 +1,6 @@
const { Command } = require('discord.js-commando');
const { pad } = require('../../util/Util');
const { list, pad } = require('../../util/Util');
const modes = ['encode', 'decode'];
module.exports = class BinaryCommand extends Command {
constructor(client) {
@@ -9,6 +10,17 @@ module.exports = class BinaryCommand extends Command {
memberName: 'binary',
description: 'Converts text to binary.',
args: [
{
key: 'mode',
prompt: `Would you like to ${list(modes, 'or')}?`,
type: 'string',
format: `<${modes.join('|')}>`,
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 binary?',
@@ -22,14 +34,19 @@ module.exports = class BinaryCommand extends Command {
});
}
run(msg, { text }) {
return msg.say(this.binary(text));
run(msg, { mode, text }) { // eslint-disable-line consistent-return
if (mode === 'encode') return msg.say(this.binary(text));
else if (mode === 'decode') return msg.say(this.unbinary(text));
}
binary(text) {
return text.split('').map(str => {
const converted = str.charCodeAt(0).toString(2);
return pad(converted, '00000000');
}).join('');
}).join(' ');
}
unbinary(text) {
return text.split(' ').map(str => String.fromCharCode(parseInt(str, 2))).join('');
}
};