-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
75 lines (70 loc) · 2.26 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
var rs = require("rantscript");
var axios = require("axios");
var fs = require("fs");
module.exports = class Bot {
constructor() {}
async login(username,password) {
try {
this.username = username;
var token = await rs.login(username,password);
this.token = token["auth_token"];
return this.token;
}
catch (e) {
console.log(e.message);
}
}
async get() {
try {
var bots = (await axios("https://raw.githubusercontent.com/C0D4-101/devrant-bots/master/bots.json")).data.map(b=>b.name);
var notifs = (await rs.notifications(this.token)).data.items;
var ums = [];
let is_boolean = true;
for (var notif of notifs) {
if (notif.type=="comment_mention" && notif.read==0) {
var comments = (await rs.rant(notif["rant_id"])).comments;
for (var i in comments) {
if (comments[i].id==notif["comment_id"]) {
if(bots.indexOf(comments[i]["user_username"])==-1) is_boolean = false;
var rto = comments.slice(0,i).filter(c => c["user_username"]==this.username).map(c=>{return {text: c.body, id: c.id, time: c["created_time"]}});
ums.push({
rid: notif["rant_id"],
cid: notif["comment_id"],
is_bot: is_boolean,
text: comments[i].body.split("@"+this.username).reverse()[0],
user: comments[i]["user_username"],
rto: rto,
time: comments[i]["created_time"]
});
}
}
}
}
await rs.clearNotifications(this.token);
return ums;
}
catch (e) {
console.log(e.message);
}
}
async reply(username,rid,text,img) {
if (img) {
await this.load(img);
await rs.postComment("@"+username+" "+text,rid,this.token,"img.jpg");
}
else await rs.postComment("@"+username+" "+text,rid,this.token);
}
async load(url) {
const writer = fs.createWriteStream("img.jpg");
const response = await axios({
url,
method: 'GET',
responseType: 'stream'
})
response.data.pipe(writer)
return new Promise((resolve, reject) => {
writer.on('finish', resolve)
writer.on('error', reject)
})
}
}