Translator Test

This commit is contained in:
Daniel Odendahl Jr
2017-03-22 16:27:42 +00:00
parent 688a21f432
commit 016ce5f6fd
2 changed files with 78 additions and 0 deletions
+27
View File
@@ -0,0 +1,27 @@
const commando = require('discord.js-commando');
const translator = require('../../src/translatetest.js');
module.exports = class TranslatorCommand extends commando.Command {
constructor(Client){
super(Client, {
name: 'translatetest',
group: 'util',
memberName: 'translatetest',
description: "A test for a custom translator.",
examples: [";servers"]
});
}
hasPermission(msg) {
return this.client.isOwner(msg.author);
}
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 translated = translator.translate(thingToTranslate);
message.channel.send(translated);
}
};
+51
View File
@@ -0,0 +1,51 @@
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;
};