Attempt to Move the Translator to the Command File

This commit is contained in:
Daniel Odendahl Jr
2017-03-22 16:40:59 +00:00
parent 016ce5f6fd
commit 788a2cc9f4
2 changed files with 52 additions and 53 deletions
+52 -2
View File
@@ -1,5 +1,4 @@
const commando = require('discord.js-commando');
const translator = require('../../src/translatetest.js');
module.exports = class TranslatorCommand extends commando.Command {
constructor(Client){
@@ -20,8 +19,59 @@ module.exports = class TranslatorCommand extends commando.Command {
if(!message.channel.permissionsFor(this.client.user).hasPermission(['SEND_MESSAGES', 'READ_MESSAGES'])) return;
}
console.log("[Command] " + message.content);
let dictionary = {
"cow": "moo",
"cat": "meow"
};
function translateWord(word) {
let wordTranslate = dictionary[word.toLowerCase()];
if (wordTranslate === undefined) return word;
else return applyCase(word, wordTranslate);
}
function applyCase(wordA, wordB) {
if (wordA.length === 1 && wordB.length !== 1) return wordB;
if (wordA === wordA.toUpperCase()) return wordB.toUpperCase();
if (wordA === wordA.toLowerCase()) return wordB.toLowerCase();
let firstChar = wordA.slice(0, 1);
let otherChars = wordA.slice(1);
if (firstChar === firstChar.toUpperCase() && otherChars === otherChars.toLowerCase()) {
return wordB.slice(0, 1).toUpperCase() + wordB.slice(1).toLowerCase();
}
return wordB;
}
function isLetter(character) {
if (character.search(/[a-zA-Z'-]/) === -1) return false;
return true;
}
const translator = function (text) {
let translatedText = "";
let word = "";
for (let i = 0; i < text.length; i += 1) {
let character = text[i];
if (isLetter(character)) {
word += character;
} else {
if (word != "") {
let wordTranslate = translateWord(word);
translatedText += wordTranslate;
word = "";
}
translatedText += character;
}
}
if (word !== "") translatedText += translateWord(word);
return translatedText;
};
let thingToTranslate = message.content.split(" ").slice(1).join(" ");
let translated = translator.translate(thingToTranslate);
let translated = translator(thingToTranslate);
message.channel.send(translated);
}
};
-51
View File
@@ -1,51 +0,0 @@
var dictionary = {
"cow": "moo",
"cat": "meow"
};
function translateWord(word) {
var wordTranslate = dictionary[word.toLowerCase()];
if (wordTranslate === undefined) return word;
else return applyCase(word, wordTranslate);
}
function applyCase(wordA, wordB) {
if (wordA.length === 1 && wordB.length !== 1) return wordB;
if (wordA === wordA.toUpperCase()) return wordB.toUpperCase();
if (wordA === wordA.toLowerCase()) return wordB.toLowerCase();
var firstChar = wordA.slice(0, 1);
var otherChars = wordA.slice(1);
if (firstChar === firstChar.toUpperCase() && otherChars === otherChars.toLowerCase()) {
return wordB.slice(0, 1).toUpperCase() + wordB.slice(1).toLowerCase();
}
return wordB;
}
function isLetter(character) {
if (character.search(/[a-zA-Z'-]/) === -1) return false;
return true;
}
module.exports.dictionary = dictionary;
module.exports.translate = function (text) {
var translatedText = "";
var word = "";
for (var i = 0; i < text.length; i += 1) {
var character = text[i];
if (isLetter(character)) {
word += character;
} else {
if (word != "") {
var wordTranslate = translateWord(word);
translatedText += wordTranslate;
word = "";
}
translatedText += character;
}
}
if (word !== "") translatedText += translateWord(word);
return translatedText;
};