-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
78 lines (64 loc) · 2.32 KB
/
index.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
const fs = require("fs");
const configData = fs.readFileSync("./config.json", "utf8");
const config = JSON.parse(configData);
let token = null;
let userId = null;
var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
async function login() {
return new Promise(async (resolve, _) => {
let res = await http("POST", "login", `user=${config.username}&password=${config.password}`);
if(res.status == "success"){
token = res.data.authToken
userId = res.data.userId
return resolve(true);
}
}).catch((err) => {
return JSON.stringify({ error: err });
});
}
async function main(){
let res = await http("GET", `chat.search?roomId=${config.roomId || "GENERAL"}&searchText=${config.searchText || "Duck"}&count=${config.count || "10"}`, ``);
// console.log("Messages:", res)
res.messages.forEach(async element => {
if(element.reactions != null && element.reactions[`:${config.emoji}:`] != null && element.reactions[`:${config.emoji}:`].usernames != null && element.reactions[`:${config.emoji}:`].usernames.includes(config.username)){
return;
}
res = await http("POST", "chat.react", `{ "messageId" : "${element._id}" , "emoji" : "${config.emoji || "duck"}", "shouldReact" : true }`);
});
}
async function http(method,endpoint, reqData) {
return new Promise(async (resolve, _) => {
var url = config.url + "/api/v1/" + endpoint;
let returnval = null;
var xhr = new XMLHttpRequest();
xhr.open(method, url);
endpoint == "login" ? xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded") : xhr.setRequestHeader("Content-Type", "application/json");
token != "" && token != null ? xhr.setRequestHeader("X-Auth-Token", token) : "";
userId != "" && userId != null ? xhr.setRequestHeader("X-User-Id", userId) : "";
xhr.onreadystatechange = async function () {
// console.log(xhr)
if (xhr.readyState === 4) {
if(xhr.status == 401){
let loggedin = await login();
if(loggedin){
let res = await http(method,endpoint,reqData);
returnval = res;
}
}else{
returnval = xhr.responseText
}
return resolve(JSON.parse(returnval));
}
};
var data = reqData;
xhr.send(data);
}).catch((err) => {
return JSON.stringify({ error: err });
});
}
async function init(){
await login()
main()
}
init()
setInterval(function (){main()}, config.interval || 30000)