-
Notifications
You must be signed in to change notification settings - Fork 0
/
chatMiddleware.js
50 lines (36 loc) · 1.2 KB
/
chatMiddleware.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
const resp_exceptions = require("./responses/exceptions.json")
const {generateResponse} = require("./chat")
// All Chatting goes through this middleware
function init_chat(input,message){
if(input == "" || input == null || input == undefined || input == "!c"){
sendMessage(message, getRandomItemFromArray(resp_exceptions["null_message"]))
return;
}
generateResponse(input).then(response => {
if(response.length == 0 || [""," "].includes(response)){
sendMessage(message, getRandomItemFromArray(resp_exceptions["cant_understand"]))
return;
}
sendMessage(message,response,false);
return;
}).catch(error => {
console.error(error);
});
}
function sendMessage(message,content,lazy_msg = true){
if(lazy_msg){
message.channel.sendTyping();
setTimeout(() => {
message.reply(content)
}, Math.min(content.length * 20, 1500));
}
else{
message.channel.sendTyping();
message.reply(content)
}
}
function getRandomItemFromArray(array) {
const randomIndex = Math.random()*array.length|0;
return array[randomIndex];
}
module.exports = { init_chat };