-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathiaBot.js
81 lines (72 loc) · 2.34 KB
/
iaBot.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
const translate = require("translate-google");
const request = require('node-fetch');
const { URL, URLSearchParams } = require('url');
const { chat, traduction, inputLang, outputLang } = require('./config.json');
const mainURL = new URL(chat.url);
const urlOptions = {
bid: chat.brainID,
key: chat.key,
uid: null,
msg: null
};
const handleStatus = (client, status) => {
client.user.setStatus(status.state);
client.user.setActivity(status.name, {
type: status.type
});
};
const handleTalk = async (msg) => {
msg.content = msg.content.replace(/^<@!?[0-9]{1,20}> ?/i, '');
if (msg.content.length < 2 || (!isNaN(chat.channel) && chat.channel != msg.channel.id)) return;
msg.channel.sendTyping();
if(traduction) //traduction activé ?
{
translate(msg.content, {to: inputLang}).then(async res => {
urlOptions.uid = msg.author.id;
urlOptions.msg = res;
mainURL.search = new URLSearchParams(urlOptions).toString();
try {
let reply = await request(mainURL);
if (reply) {
reply = await reply.json();
translate(reply.cnt, {to: outputLang}).then(response => {
msg.reply({
content: response,
allowedMentions: {
repliedUser: false
}
})
})
}
} catch (e) {
console.log(e.stack);
}
}).catch(err => {
console.error(err)
})
}
else
{
urlOptions.uid = msg.author.id;
urlOptions.msg = msg.content;
mainURL.search = new URLSearchParams(urlOptions).toString();
try {
let reply = await request(mainURL);
if (reply) {
reply = await reply.json();
msg.reply({
content: reply.cnt,
allowedMentions: {
repliedUser: false
}
})
}
} catch (e) {
console.log(e.stack);
}
}
};
module.exports = {
handleStatus,
handleTalk
};