-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.js
146 lines (131 loc) · 6.83 KB
/
bot.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
const { execSync } = require('child_process');
const { readproperties, getProperty } = require('./bin/util.js');
const fs = require('fs');
const Discord = require('discord.js');
const MongoClient = require('mongodb');
const client = new Discord.Client();
readproperties(__dirname + "/properties/app.properties");
function loadSites(){
return fs.readdirSync("sites").toString().split(",").filter(e=>e.endsWith(".js")).map(e=>require('./sites/' + e));
}
function getCollection(mongo){
return mongo.db(getProperty("mongo.app.db")).collection(getProperty("mongo.app.db.collection"));
}
function getMongoConnection(){
return MongoClient("mongodb://" + getProperty("mongo.app.user.username") + ":" + getProperty("mongo.app.user.password") + "@localhost:" + getProperty("mongo.port") + "/appdata?retryWrites=true&w=majority", {useNewUrlParser: true, useUnifiedTopology: true});
}
client.on('ready', ()=>{
console.log('Logged in as ${client.user.tag}!');
setInterval(()=>{
sites = loadSites();
getMongoConnection().then((mongo)=>{
let collection = getCollection(mongo);
collection.find({}).toArray((err,res)=>{
if(err)console.log(err);
let subscriptions = res.map(e=>e);
for(sub of subscriptions){
let site = sites.find((e)=>sub.link.match(e.urlpattern));
if(site){
let thisCheck = site.getRecords(sub.link, __dirname + "/html.tmp");
let notificationList = site.getNotificationList(thisCheck, sub.lastCheck);
if(notificationList.length > 0){
for(let i=0;i<Math.ceil(notificationList.length/5);i++){
new Discord.User(client, sub.user).send(new Object({ embed: site.composeEmbeddedMessage(sub, notificationList.slice((i*5), 5))}));
}
collection.updateOne({_id:sub._id},{$set:{lastCheck:thisCheck}});
}
}
}
mongo.close();
});
console.log('here');
});
}, getProperty("discord.bot.interval.ms"));
});
client.on('message', (message)=>{
if(message.content.startsWith("!sb ")){
let cmd=message.content.split(" ");
let incorrect=false
if(cmd[1] === "subscribe"){
let sub = {
user:message.author,
name:cmd[2],
link:cmd[3]
}
if(sub.link){
sites = loadSites();
let site = sites.find((e)=>sub.link.match(e.urlpattern));
console.log(site);
if(site){
getMongoConnection().then((mongo)=>{
let collection = getCollection(mongo);
collection.find({"user.id":message.author.id}).toArray((err,res)=>{
let userSubscriptions = res.map(e=>e);
let nameIndex = userSubscriptions.map((e)=>e.name).indexOf(sub.name);
let linkIndex = userSubscriptions.map((e)=>e.link).indexOf(sub.link);
if( nameIndex == -1 && linkIndex == -1){
let lastCheck = site.getRecords(sub.link, __dirname + "/html.tmp");
collection.insertOne({"name":sub.name,"link":sub.link,"lastCheck":lastCheck,"user":message.author});
message.channel.send("A subscription with the name " + sub.name + " and link " + sub.link + " has been created for user " + message.author.username + "\nTo see a listing of your subscriptions, run: !sb list");
}else{
let msg = ""
if(nameIndex != -1){
msg+="A subscription with the name " + sub.name + " has previously been created for user " + message.author.username + "\n";
}
if(linkIndex != -1){
msg+="A subscription with the link " + sub.link + " has previously been created for user " + message.author.username + "\n";}
msg+="To see a listing of your subscriptions, run: !sb list"
message.channel.send(msg);
}
});
mongo.close();
});
}else{
message.channel.send(message.author.username + " looks like the site associated with " + sub.link + " is not supported.\nTo see the currently supported sites, run: !sb supported");
}
}else{
incorrect=true;
}
}else if(cmd[1] === "unsubscribe"){
let subscriptionName = cmd[2];
if(subscriptionName){
getMongoConnection().then((mongo)=>{
let collection = getCollection(mongo);
if(subscriptionName === "!all"){
collection.remove({"user.id":message.author.id});
message.channel.send("All subscriptions for user " + message.author.username + " have been deleted!");
}else{
collection.remove({"user.id":message.author.id,"name":subscriptionName});
message.channel.send(message.author.username + "'s subcription with name " + subscriptionName + " has been deleted!");
}
mongo.close();
});
}else{
incorrect=true;
}
}else if(cmd[1] === "list"){
getMongoConnection().then((mongo)=>{
let collection = getCollection(mongo);
collection.find({"user.id":message.author.id}).toArray((err,res)=>{
let userSubscriptions = res.map(e=>e);
let msg=message.author.username + " you have the following subscriptions:\n"
for(sub of userSubscriptions){
msg+=sub.name + " " + sub.link + "\n"
}
message.channel.send(msg);
mongo.close();
});
});
}else if(cmd[1] === "supported"){
let msg = "The following sites are currently supported:\n"
for(site of sites){
msg+=site.name + "\n";
}
message.channel.send(msg);
}else{
incorrect = true;
}
if(incorrect)message.channel.send("USAGE:\n !sb subscribe <subscription name> <subscription link>\n !sb unsubscribe <subscription name>\n !sb list\n !sb supported");
}
});
client.login(getProperty("discord.bot.token"));