mirror of
https://github.com/arthur-pbty/xiao.git
synced 2026-06-09 01:04:16 +02:00
Animal Crossing speak, morse encode/decode
This commit is contained in:
@@ -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('');
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user