-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
118 lines (98 loc) · 3.61 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
const Discord = require('discord.js');
const client = new Discord.Client();
const translate = require('google-translate-api');
client.on('ready', () => {
console.log('I am ready!');
});
const prefix = '>';
const flags = {
"en" : ":flag_gb:",
"ja" : ":flag_jp:",
"da" : ":flag_dk:"
}
const flagEmojis = {
"🇫🇷" : "fr",
"🇺🇸" : "en",
"🇬🇧" : "en",
"🇩🇪" : "de",
"🇯🇵" : "jp",
"🇪🇸" : "es",
"🇮🇹" : "it",
"🇷🇺" : "ru",
"🇦🇷" : "ar"
}
client.on('message', message => {
// console.log(message.content);
var content = message.content;
var user = message.channel.lastMessage.author;
//If it is >(+command) or a flag emoji
if(content.length >= prefix.length && content.substr(0, prefix.length) == prefix || content.length >= 4 && content.substr(0,4) in flagEmojis) {
var usedEmoji = content.length >= 4 && content.substr(0,4) in flagEmojis;
var args = content.substr(prefix.length).split(" ");
var command = usedEmoji ? flagEmojis[content.substr(0,4)] : args[0].toLowerCase();
var contentText = content.substr(prefix.length + args[0].length);
var authorMention = "<@" + message.author.id + ">";
switch (command) {
case "fr":
translateTo('fr', contentText, user, message);
break;
case "gb":
case "us":
case "en":
translateTo('en', contentText, user, message);
break;
case "de":
translateTo('de', contentText, user, message);
break;
case "es":
translateTo('es', contentText, user, message);
break;
case "ja":
case "jp":
translateTo('ja', contentText, user, message);
break;
case "it":
translateTo('it', contentText, user, message);
break;
case "ru":
translateTo('ru', contentText, user, message);
break;
case "ar":
translateTo('ar', contentText, user, message);
break;
case "help":
message.channel.send("Supported languages : " + Object.keys(flagEmojis))
}
}
});
function translateTo(language, content, user, message) {
var channel = message.channel;
var userId = user.id;
var userName = user.username;
var userAvatar = user.avatar;
var flagToName = flags[language] == undefined ? ":flag_" + language + ":" : flags[language];
translate(content, {to: language}).then(res => {
var iconUrl = userAvatar != null ? "https://cdn.discordapp.com/avatars/"+userId+"/"+ userAvatar +".png" : user.displayAvatarURL;
var color;
if(message.member== null) {
color = 16777215;
} else {
color = message.member.roles.keyArray().length != 1 ? message.member.roles.last().color : 16777215;
}
if(res.from.language.iso != language) {
var flagFromName = flags[res.from.language.iso] == undefined ? ":flag_" + res.from.language.iso + ":" : flags[res.from.language.iso];
const embed = {
"description": res.text + "\n" + flagFromName + " :loudspeaker: " + flagToName,
"color": color,
"author": {
"name": userName,
"icon_url": iconUrl
}
};
channel.send({embed});
}
}).catch(err => {
console.error(err);
});
}
client.login('BOT TOKEN GOES HERE');