-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.js
151 lines (129 loc) · 4.69 KB
/
bot.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Translator Bot for Twitch
// Using google translate, via 'googletrans' package
// API Twitch
const tmi = require('tmi.js');
// API Google Translate
const gtrans = require('googletrans').default;
// Define configuration options here
// - username is the name of the channel/bot
// - password is generated on https://twitchapps.com/tmi/ page
// - channels is an array of channels
// - connection: defines additional options
//
// username and password are defined after CHANNEL_NAME and CHANNEL_PASSWORD environment variables
// (you also can overwrite botname and botpassword here)
let botname = process.env.CHANNEL_NAME;
let botpassword = process.env.CHANNEL_PASSWORD;
// TODO: If no channel & password, then exit...
if ((botname == undefined) || (botpassword == undefined)) {
console.error('No CHANNEL_NAME or CHANNEL_PASSWORD environment variable found. Exiting.');
process.exit(-1);
}
// add oauth: before botpassword
botpassword = 'oauth:' + botpassword;
const opts = {
identity: {
username: botname,
password: botpassword
},
channels: [
// using bot's channel is great for testing, but not mandatory
botname
//more channels
],
// Automatic reconnection
connection: { reconnect: true }
};
// Create a client with our options
const client = new tmi.client(opts);
const tr_lang = {
'de': ['de', 'sagt'],
'en': ['en', 'says'],
'fr': ['fr', 'dit'],
'pt': ['pt', 'disse'],
};
// Register our event handlers (defined below)
client.on('message', onMessageHandler);
client.on('connected', onConnectedHandler);
// Connect to Twitch:
client.connect();
// Called every time a message comes in
function onMessageHandler(target, context, msg, self) {
try {
// Ignore messages from the bot
if (self) { return; }
// Remove whitespace from chat message
let tMsg = msg.trim();
// Check if the message starts with @name
// in that case, extract the name and move the @name at the end of the message, and process
if (tMsg[0] === '@') {
let atnameEndIndex = tMsg.indexOf(' ');
let atname = tMsg.substring(0, atnameEndIndex);
let msg = tMsg.substring(atnameEndIndex + 1);
tMsg = msg + ' ' + atname;
console.info('Changed message :', tMsg);
}
// Filter commands (options)
if (tMsg[0] != '!') return;
// Extract command
let cmd = tMsg.split(' ')[0].substring(1).toLowerCase();
// Name for answering
let answername = '@' + context['display-name'];
// Command for displaying the commands (in english)
if (cmd === "lang" || cmd === "translate") {
client.say(target, 'I can (approximatevely) translate your messages in many languages. Simply start your message with one of these commands: !en (english) !fr (french) !de (german) !pt (portuguese)... ');
return;
}
// Commands for displaying messages explaining the translation feature in various languages
// TODO: sentences
const explanations = {
// 'germans': '',
// 'spanish': '',
'french': 'Vous pouvez utiliser notre bot traducteur. Commencez votre message par !en pour traduire votre message en anglais. Par exemple "!en Bonjour"',
}
if (cmd in explanations) {
client.say(target, explanations[cmd]);
return;
}
if (cmd in tr_lang) {
var ll = tr_lang[cmd];
//console.error(ll);
var txt = tMsg.substring(1 + cmd.length);
// Text must be at least 2 characters and max 200 characters
var lazy = false;
if (txt.length > 2) {
if (txt.length > 200) {
lazy = true;
txt = "i'm too lazy to translate long sentences ^^";
}
// Lazy mode, and english target => no translation, only displays 'lazy' message in english
if ((lazy === true) && (ll[0].indexOf('en') == 0)) {
say(target, context['display-name'] + ', ' + txt);
return;
}
// Translate text
gtrans(txt, { to: ll[0] }).then(res => {
if (lazy === true) {
// lazy mode sentence in english and also in requested language
client.say(target, context['display-name'] + ', ' + txt + '/' + res.text);
}
else {
// Translation
// TODO: Check is translated text == original text. In that case it
// means the command was not correctly used (ex: "!en hello friends")
client.say(target, context['display-name'] + ' ' + ll[1] + ': ' + res.text);
}
}).catch(err => {
console.error('Translation Error:', err);
})
}
}
}
catch (e) {
console.error(e.stack);
}
}
// Called every time the bot connects to Twitch chat
function onConnectedHandler(addr, port) {
console.log(`* Connected to ${addr}:${port}`);
}