-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
71 lines (61 loc) · 2.86 KB
/
index.js
File metadata and controls
71 lines (61 loc) · 2.86 KB
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
const Discord = require('discord.js');
const dotenv = require('dotenv');
const client = new Discord.Client();
const WebSocket = require('ws');
dotenv.config();
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
const ws = new WebSocket(process.env.GATEWAY);
let isProcessing = false;
ws.on('open', function open() {
console.log("Connection established!");
});
client.on('message', message => {
if (message.content.startsWith('!db ')) {
if (isProcessing) {
message.reply("Nah I am already processing something, wait man.");
return;
}
if (message.content.startsWith('!db crawl ')) {
isProcessing = true;
let matches = [];
let string = "";
let chunks = [];
message.channel.send("I am contacting the DocBot Gateway server now. It may take some time to process!").then((msg) => {
ws.send(JSON.stringify({
"api_key": process.env.API_KEY,
"service": message.content.substr(10)
}));
ws.on('message', function incoming(data) {
data = JSON.parse(data);
if (data.message == "match") {
matches.push(data.parameters);
}
if (data.message == "finished") {
isProcessing = false;
if (matches.length == 0) {
if (Math.random() > 0.99) {
message.reply("I have found 0 matches!", { files: ["https://media1.tenor.com/images/b11044c627cef3e97d4d09680e3f2ec0/tenor.gif"] });
}else{
message.reply("I have found 0 matches!");
}
return;
}
msg.edit(msg.content + "\n\nFinished crawling the service! I have found " + matches.length + " matches. I will list them below:\n\n\n").then((msgedit) => {
matches.forEach((match) => {
let filteredSentence = match.sentence.replace("`", "");
string += `Case https://edit.tosdr.org/cases/${match.case}\nSentence: ${filteredSentence}\nDocument: https://edit.tosdr.org/documents/${match.document.id} (${match.document.name})\nThe Service \`${match.service.name}\` has a \`${match.service.rating}\` Rating\n\n`;
});
chunks = string.match(/(.|[\r\n]){1,2000}/g);
chunks.forEach((chunk) => {
message.channel.send(chunk);
})
});
}
});
});
}
}
});
client.login(process.env.TOKEN);