Move Translator Functions Outside of the Command Runner

This commit is contained in:
Daniel Odendahl Jr
2017-03-22 20:35:55 +00:00
parent 87bb4ad408
commit 39f4e49864
+29 -28
View File
@@ -1,23 +1,6 @@
const commando = require('discord.js-commando'); const commando = require('discord.js-commando');
module.exports = class TemmieCommand extends commando.Command { const dictionary = {
constructor(Client){
super(Client, {
name: 'temmie',
group: 'textedit',
memberName: 'temmie',
description: "Translate text to Temmie speak. (;temmie I am Temmie)",
examples: [";temmie I am Temmie."]
});
}
async run(message) {
if(message.channel.type !== 'dm') {
if(!message.channel.permissionsFor(this.client.user).hasPermission(['SEND_MESSAGES', 'READ_MESSAGES'])) return;
}
console.log("[Command] " + message.content);
let dictionary = {
"i": "tem", "i": "tem",
"hi": "hoi", "hi": "hoi",
"that": "dat", "that": "dat",
@@ -135,15 +118,15 @@ module.exports = class TemmieCommand extends commando.Command {
"your": "ur", "your": "ur",
"yours": "urs", "yours": "urs",
"there": "dere" "there": "dere"
}; };
function translateWord(word) { function translateWord(word) {
let wordTranslate = dictionary[word.toLowerCase()]; let wordTranslate = dictionary[word.toLowerCase()];
if (wordTranslate === undefined) return word; if (wordTranslate === undefined) return word;
else return applyCase(word, wordTranslate); else return applyCase(word, wordTranslate);
} }
function applyCase(wordA, wordB) { function applyCase(wordA, wordB) {
if (wordA.length === 1 && wordB.length !== 1) return wordB; if (wordA.length === 1 && wordB.length !== 1) return wordB;
if (wordA === wordA.toUpperCase()) return wordB.toUpperCase(); if (wordA === wordA.toUpperCase()) return wordB.toUpperCase();
if (wordA === wordA.toLowerCase()) return wordB.toLowerCase(); if (wordA === wordA.toLowerCase()) return wordB.toLowerCase();
@@ -153,21 +136,22 @@ module.exports = class TemmieCommand extends commando.Command {
return wordB.slice(0, 1).toUpperCase() + wordB.slice(1).toLowerCase(); return wordB.slice(0, 1).toUpperCase() + wordB.slice(1).toLowerCase();
} }
return wordB; return wordB;
} }
function isLetter(character) { function isLetter(character) {
if (character.search(/[a-zA-Z'-]/) === -1) return false; if (character.search(/[a-zA-Z'-]/) === -1) return false;
return true; return true;
} }
const translator = function (text) { const translator = function(text) {
let translatedText = ""; let translatedText = "";
let word = ""; let word = "";
for (let i = 0; i < text.length; i += 1) { for (let i = 0; i < text.length; i += 1) {
let character = text[i]; let character = text[i];
if (isLetter(character)) { if (isLetter(character)) {
word += character; word += character;
} else { }
else {
if (word != "") { if (word != "") {
let wordTranslate = translateWord(word); let wordTranslate = translateWord(word);
translatedText += wordTranslate; translatedText += wordTranslate;
@@ -180,8 +164,25 @@ module.exports = class TemmieCommand extends commando.Command {
if (word !== "") translatedText += translateWord(word); if (word !== "") translatedText += translateWord(word);
return translatedText; return translatedText;
}; };
module.exports = class TemmieCommand extends commando.Command {
constructor(Client) {
super(Client, {
name: 'temmie',
group: 'textedit',
memberName: 'temmie',
description: "Translate text to Temmie speak. (;temmie I am Temmie)",
examples: [";temmie I am Temmie."]
});
}
async run(message) {
if (message.channel.type !== 'dm') {
if (!message.channel.permissionsFor(this.client.user).hasPermission(['SEND_MESSAGES', 'READ_MESSAGES'])) return;
}
console.log("[Command] " + message.content);
let thingToTranslate = message.content.split(" ").slice(1).join(" "); let thingToTranslate = message.content.split(" ").slice(1).join(" ");
let temspeak = translator(thingToTranslate); let temspeak = translator(thingToTranslate);
let noing = temspeak.split("ing").join("in"); let noing = temspeak.split("ing").join("in");