-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
159 lines (137 loc) · 6.17 KB
/
app.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
const Discord = require('discord.js');
const fetch = require('node-fetch');
require('dotenv').config();
const admin = require('firebase-admin');
const serviceAccount = require('./serviceAccountKey.json');
admin.initializeApp({
credential: admin.credential.cert(serviceAccount)
});
const db = admin.firestore();
const client = new Discord.Client();
client.on('ready', () => {
console.log('Bot is ready');
});
setInterval(() => {
fetch("http://api.steampowered.com/ISteamNews/GetNewsForApp/v0002/?appid=570&count=100&format=json")
.then(data => data.json())
.then(data => {
const newsArr = data.appnews.newsitems;
const newestNews = newsArr.find(n => {
return n.feed_type === 1;
})
return newestNews;
})
.then(data => {
db.collection('patch').doc('lastPatch').get()
.then(doc => {
return doc.data().last_patch_id;
}).then(lastPatchId => {
if (data.gid !== lastPatchId) {
// for (chanel in channels) {
// channel.send("<@&" + botRole.id + "> " + data.url);
// lastPatchId = data.gid;
// }
db.collection('channels').get()
.then(snapshot => {
snapshot.forEach((doc) => {
console.log(doc.data());
client.channels.cache.get(doc.data().channel_id)
.send("<@&" + doc.data().role_id + "> " + data.url)
});
})
.catch(e => console.error(e));
db.collection('patch').doc('lastPatch').set({ 'last_patch_id': data.gid });
}
})
.catch(e => console.error(e));
})
.catch(e => console.error(e));
}, 5000); //120000 (2 mintues)
client.on('message', (msg) => {
//post the most recent Dota 2 update
if (msg.content === '!patch') {
fetch("http://api.steampowered.com/ISteamNews/GetNewsForApp/v0002/?appid=570&count=100&format=json")
.then(data => data.json())
.then(data => {
const newsArr = data.appnews.newsitems;
const newestNews = newsArr.find(n => {
return n.feed_type === 1;
})
return newestNews;
})
.then(data => msg.reply(data.url))
.catch(e => console.error(e));
}
//print bot command descriptions
else if (msg.content === '!patch help') {
msg.reply("\n!patch subscribe -- subscribes a channel to Dota 2 patch note updates\n!patch notify -- adds member to a role to recieve patch post alerts\n!patch help -- explains bot commands\n!patch -- gives info on most recent patch");
}
//subscribe a channel to recieve Dota 2 updates
else if (msg.content === '!patch subscribe') {
//check for existing role
let botRole = msg.guild.roles.cache.find(role => {
return role.name === 'Dota2PatchBotNotification';
});
//create new role if it doesn't exist
if (botRole === undefined) {
botRole = msg.guild.roles.create({
data: {
name: 'Dota2PatchBotNotification',
color: 'RED',
},
reason: 'role to notify people of Dota 2 patch notes from the Dota 2 Patch Bot',
});
}
db.collection('channels').doc(msg.guild.id)
.set({
'channel_id': msg.channel.id,
'role_id': botRole.id
});
client.channels.cache.get(msg.channel.id).send('Subscribed to #' + msg.channel.name + ' in ' + msg.guild.name);
}
//subscribe a channel to recieve Dota 2 updates
// else if (msg.content === '!patch subscribe') {
// let subscribedChannel = msg.channel.id;
// client.channels.cache.get(subscribedChannel).send('Subscribed to #' + msg.channel.name + ' in ' + msg.channel.guild.name);
// let lastPatchId = ""
// const patchPromise = new Promise((resolve, reject) => {
// setInterval(() => {
// if (subscribedChannel != "") {
// fetch("http://api.steampowered.com/ISteamNews/GetNewsForApp/v0002/?appid=570&count=100&format=json")
// .then(data => data.json())
// .then(data => {
// const newsArr = data.appnews.newsitems;
// const newestNews = newsArr.find(n => {
// return n.feed_type === 1;
// })
// return newestNews;
// })
// .then(data => {
// if (data.gid !== lastPatchId) {
// client.channels.cache.get(subscribedChannel).send("<@&" + botRole.id + "> " + data.url);
// lastPatchId = data.gid;
// }
// })
// .catch(e => console.error(e));
// }
// }, 5000) //120000 (2 mintues)
// })
// }
//get notified/alerted/@-ed when a new patch update is posted
else if (msg.content === '!patch notify') {
//if not yet subscribed, then error
db.collection("channels").doc(msg.guild.name).get().then((doc) => {
if (doc.exists) {
msg.member.roles.add(botRole)
msg.reply("you have subscribed to be notified of Dota2 patch notes and news");
} else {
// doc.data() will be undefined in this case
msg.reply("you have subscribed to be notified of Dota2 patch notes and news");
}
})
.catch(e => console.error(e));
msg.member.roles.add(botRole)
msg.reply("you have subscribed to be notified of Dota2 patch notes and news");
}
})
client.login(process.env.BOT_TOKEN)