-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
175 lines (148 loc) · 5.38 KB
/
app.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
const nconf = require('nconf');
const Discord = require('discord.js');
const axios = require('axios');
const qs = require('qs');
const {Logging} = require('@google-cloud/logging');
const cld3 = require('cld3-asm');
const esc = '٪';
nconf.file({
file: 'config.yaml',
format: require('nconf-yaml')
});
const projectId = nconf.get('gcp_project');
const logging = new Logging({projectId});
const log = logging.log(nconf.get('gcp_log_name'));
async function notice(msg) {
log.notice(logging.entry(null, msg));
if (msg.message) {
console.log(msg.message);
} else {
console.log(msg);
}
}
async function info(msg) {
log.info(logging.entry(null, msg));
}
async function warn(msg) {
log.warn(logging.entry(null, msg));
console.log(msg);
}
async function error(msg) {
log.error(logging.entry(null, msg));
console.log(msg);
}
notice({service: 'INIT', method: 'logging', message: 'DeepL Translator Bot Launched'});
const prefix = nconf.get('prefix');
const deepl_auth_key = nconf.get('deepl_auth_key');
const deepl_url = "https://api.deepl.com/v2/translate?auth_key="+deepl_auth_key;
const cross_channel_ids = nconf.get('cross_channel_pairs');
var cross_channels = {};
const client = new Discord.Client();
var cldIdentifier;
cld3.loadModule().then(factory => {
cldIdentifier = factory.create(0,700);
notice({service: 'INIT', method: 'cld3-asm', message: 'loadModule() factory created'});
client.login(nconf.get('discord_bot_token'));
});
client.once('ready', () => {
notice({service: 'INIT', method: 'discord', message: 'client connection ready'});
client.user.setActivity(nconf.get('auto_trans_channel'));
for (let [key, value] of Object.entries(cross_channel_ids)) {
client.channels.fetch(value).then( channel => {
cross_channels[key] = channel;
});
}
});
client.on('message', message => {
handleMessage(message).catch(err => {
error({service: 'ONMESSAGE', method: 'on', msgId: message.id, message: err.message, details: err});
});
});
async function handleMessage(message) {
try {
if (message.author.bot) {
return;
}
let cmd;
let text;
let replyChannel;
if (message.content.startsWith(prefix)) {
const cmdEndIndex = message.content.indexOf(' ');
cmd = message.content.substring(1, cmdEndIndex).toLowerCase();
text = message.content.substring(cmdEndIndex + 1);
replyChannel = message.channel;
info({service: 'ONMESSAGE', method: 'command', msgId: message.id, message: cmd});
} else if (cross_channels[message.channel.id] != null) {
replyChannel = cross_channels[message.channel.id];
text = message.content;
let src_ident = cldIdentifier.findLanguage(text);
let src_lang = src_ident.language;
if (src_lang === 'ja' || src_lang === 'zh') {
cmd = 'je'
} else {
cmd = 'ej'
}
info({service: 'ONMESSAGE', method: 'cld3-asm-detect', msgId: message.id, from_lang: src_lang, details: src_ident});
} else {
return;
}
info({service: 'ONMESSAGE', method: 'discord-message', msgId: message.id, author: message.author.username, channel: `${replyChannel.name}`, message: message.content, details: message.toJSON()});
if (cmd === 'ej') {
handleTranslationReply(text, 'JA', message, replyChannel);
} else if (cmd === 'je') {
handleTranslationReply(text, 'EN', message, replyChannel);
}
} catch {
warn({service: 'ONMESSAGE', method: 'handleMessage', msgId: message.id, message: err.message, details: err});
}
}
async function handleTranslationReply(text, target_lang, message, replyChannel) {
try {
replyChannel.startTyping();
const result = await translate(text, target_lang, message)
replyChannel.send(result);
replyChannel.stopTyping();
} catch (err) {
warn({service: 'TRANSLATE', method: 'handleTranslationReply', msgId: message.id, message: err.message, details: err});
}
}
async function translate(text, lang, message) {
let matches = text.matchAll(/(<[^\s]+>)/g);
let escapes = [];
for (const match of matches) {
escapes.push(match[0]);
text = text.replace(match[0], esc+escapes.length);
}
let result = await deeplTranslate(text, lang, message);
if (result == null || result.length == 0) {
result = "<Empty translation returned from service>";
} else if (result.length > 2000) {
result = "<Translation too large for Discord message>";
}
for (let i=1;i<=escapes.length;i++) {
result = result.replace(esc+i, escapes[i-1]);
}
return `\`\`\`${message.author.username}: ${result}\`\`\``;
}
async function deeplTranslate(text, targetLang, message) {
try {
const request = {
auth_key: deepl_auth_key,
target_lang: targetLang,
text: text,
};
info({service: 'TRANSLATE', method: 'deepl-request', msgId: message.id, to_lang: targetLang, message: text, length: text.length, details: request});
const response = await axios.post(deepl_url, qs.stringify(request))
client.user.setActivity("DeepL");
info({service: 'TRANSLATE',
method: 'deepl-response',
msgId: message.id,
from_lang: response.data.translations[0].detected_source_language,
message: response.data.translations[0].text,
});
return response.data.translations[0].text;
} catch (err) {
warn({service: 'TRANSLATE', method: 'deepl-response', msgId: message.id, message: err.message, details: err});
return "<Error returned from deepl>";
}
}