-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
297 lines (270 loc) · 12 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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/**
* Automatically detects new posts made on Reddit that match the specified
* queries and are in the specified subreddits, and sends them to Discord.
* @author @eric-lu-VT (Eric Lu)
*/
require('dotenv').config();
const mongoose = require('mongoose');
const RedditPost = require('./schema/redditPost');
const ServerPost = require('./schema/serverPost');
var snoowrap = require('snoowrap');
const { Client, Intents, MessageEmbed } = require('discord.js');
const { REST } = require('@discordjs/rest');
const { Routes } = require('discord-api-types/v9');
const { commands } = require('./deploy-commands.js');
// Connect to MongoDB
mongoose.connect(process.env.MONGOURI, { useNewUrlParser: true, useUnifiedTopology: true })
.then((result) => console.log('Connected to MongoDB'))
.catch((err) => console.log(err));
// Connect to Reddit API
const reddit = new snoowrap({
userAgent: process.env.USERAGENT,
clientId: process.env.REDDITBOTID,
clientSecret: process.env.REDDITBOTSECRET,
username: process.env.REDDITUSERUSERNAME,
password: process.env.REDDITUSERPASSWORD
});
// Connect to Discord API
const client = new Client({ intents: [Intents.FLAGS.GUILDS] });
var handle = null;
/**
* For each Discord server Bot is in, search for the queries attributed to the Discord server,
* and post the results to the Discord server.
* @param {Array.<String>} guilds list of all guild ids found in database
*/
function searchReddit(guilds) {
guilds.forEach((guildId) => {
ServerPost.findOne({ _id: guildId}, ['channels', 'queries']).then((resdoc) => {
resdoc.queries.forEach(q => {
reddit.getSubreddit(q.subreddit).search({query: q.query, time: 'hour', sort: 'new'}).then(data => {
data.forEach(listing => {
RedditPost.exists({postid: listing.name, guildId: guildId}).then((result) => {
if(result) {
// ("entry already exists");
}
else {
// Send Reddit search information to database
new RedditPost({
postid: listing.name,
subreddit: (listing.subreddit_name_prefixed),
url: ("https://www.reddit.com" + listing.permalink),
date: listing.created_utc,
guildId: guildId
}).save();
// console.log("Made an entry to DB");
var title = (listing.title);
if(title.length > 253) { // For Reddit posts, max character length = 256
title = title.substring(0, 253) + "...";
}
// For each elligible channel in the Discord server, send query results
resdoc.channels.forEach(channelId => {
client.channels.cache.get(channelId).send({embeds : [new MessageEmbed()
.setColor([255, 165, 0])
.setAuthor(listing.author.name, "", encodeURI("https://www.reddit.com/u/" + listing.author.name))
.setTitle(title)
.setDescription(listing.score + " votes and " + listing.num_comments + " comments so far")
.setFooter("On " + listing.subreddit_name_prefixed)
.setTimestamp(listing.created * 1000)
.setURL("https://www.reddit.com" + listing.permalink)]});
});
}
});
});
});
});
});
});
}
/**
* Initializes slash commands for servers and begins search timer.
* Run this command each time bot first turns on.
*/
function registerCommands() {
var guilds = [];
ServerPost.find({}, ['_id']).then((doc) => {
const rest = new REST({ version: '9' }).setToken(process.env.DISCORDBOTTOKEN);
doc.forEach(res => {
guilds.push(res._id);
rest.put(Routes.applicationGuildCommands(process.env.DISCORDID, res._id), { body: commands })
.then().catch(console.error);
});
});
handle = setInterval(() => searchReddit(guilds), 30000); // run every 30 seconds
}
/**
* Processes to run when bot first connects to Discord.
* @listens 'ready'
*/
client.once('ready', () => {
registerCommands();
console.log('Connected to Discord');
});
/**
* Processes to run when a slash command is received.
* @listens 'interactionCreate'
*/
client.on('interactionCreate', async interaction => {
if (!interaction.isCommand()) return; // only process slash commands
const { commandName } = interaction;
if (commandName === 'ping') {
await interaction.reply('Pong!');
}
else if(commandName === 'addchannel') {
// Add channel to corresponding Discord server in the database
ServerPost.findOneAndUpdate({_id: interaction.guildId},
{ $push : { 'channels': interaction.channelId}},
{upsert: true}).then(data => {});
// Display add results to Discord server
var embd = new MessageEmbed()
.setColor([46, 204, 113])
.setTitle("Added channel!")
.setDescription("Added the following channel:")
.addField("Channel", String(interaction.channelId))
.addField("Server", String(interaction.guildId))
.setAuthor(interaction.user.username,
`https://cdn.discordapp.com/avatars/${interaction.user.id}/${interaction.user.avatar}.png?size=256`)
.setTimestamp();
await interaction.reply({embeds : [embd]});
}
else if (commandName === 'removechannel') {
// Remove channel from the corresponding server in the database
ServerPost.findOneAndUpdate({_id: interaction.guildId},
{ $pull : { 'channels': interaction.channelId}}).then(data => {});
// Display remove results to Discord server
var embd = new MessageEmbed()
.setColor([46, 204, 113])
.setTitle("Removed channel!")
.setDescription("Removed the following channel:")
.addField("Channel", String(interaction.channelId))
.addField("Server", String(interaction.guildId))
.setAuthor(interaction.user.username,
`https://cdn.discordapp.com/avatars/${interaction.user.id}/${interaction.user.avatar}.png?size=256`)
.setTimestamp();
await interaction.reply({embeds : [embd]});
}
else if (commandName === 'addquery') {
// Process which search term and subreddit to look for
query = interaction.options.getString("input").split(" ");
var queryStr = "";
var subredditStr = "";
if(query.length > 1) {
if(query.length == 1) {
queryStr = query[0];
subredditStr = "All";
}
else {
for(var i = 0; i < query.length - 1; i++) {
queryStr += query[i];
}
subredditStr = query[query.length - 1];
}
}
queryStr = queryStr.toLowerCase();
subredditStr = subredditStr.toLowerCase();
// If query does not exist in database, send this:
var embd = new MessageEmbed()
.setColor([231, 76, 60])
.setTitle("Failed to add query...")
.setDescription("The following query already exists:")
.addField("Query", String(queryStr))
.addField("Subreddit", String(subredditStr))
.setAuthor(interaction.user.username,
`https://cdn.discordapp.com/avatars/${interaction.user.id}/${interaction.user.avatar}.png?size=256`)
.setTimestamp();
// If query does exist in the database, handle as follows
ServerPost.exists({_id: interaction.guildId, queries: {$elemMatch: {query: queryStr, subreddit: subredditStr}}})
.then((result) => {
if(!result) {
ServerPost.findOneAndUpdate({_id: interaction.guildId},
{ $push : { 'queries': {
query: queryStr,
subreddit: subredditStr
}}},
{upsert: true}).then(data => {});
embd.setColor([46, 204, 113]);
embd.setTitle("Added query!");
embd.setDescription("Added the following query:");
}
interaction.reply({embeds : [embd]});
});
}
else if (commandName === 'removequery') {
// Process which search term and subreddit to look for
query = interaction.options.getString("input").split(" ");
var queryStr = "";
var subredditStr = "";
if(query.length > 1) {
if(query.length == 1) {
queryStr = query[0];
subredditStr = "All";
}
else {
for(var i = 0; i < query.length - 1; i++) {
queryStr += query[i];
}
subredditStr = query[query.length - 1];
}
}
queryStr = queryStr.toLowerCase();
subredditStr =subredditStr.toLowerCase();
// If query does not exist in database, send this:
var embd = new MessageEmbed()
.setColor([231, 76, 60])
.setTitle("Failed to remove query...")
.setDescription("The following query does not exist in the database:")
.addField("Query", String(queryStr))
.addField("Subreddit", String(subredditStr))
.setAuthor(interaction.user.username,
`https://cdn.discordapp.com/avatars/${interaction.user.id}/${interaction.user.avatar}.png?size=256`)
.setTimestamp();
// If query does exist in the database, handle as follows
ServerPost.exists({_id: interaction.guildId, queries: {$elemMatch: { query: queryStr, subreddit: subredditStr}}})
.then((result) => {
if(result) {
ServerPost.findOneAndUpdate({_id: interaction.guildId},
{ $pull : { 'queries': {
query: queryStr,
subreddit: subredditStr
}}}).then(data => {});
embd.setColor([46, 204, 113]);
embd.setTitle("Removed query!");
embd.setDescription("Removed the following query:");
}
interaction.reply({embeds : [embd]});
});
}
});
/**
* Processes to run when the bot is added to a new Discord server.
* @listens "guildCreate"
*/
client.on("guildCreate", guild => {
clearInterval(handle);
var channels = [];
guild.channels.cache.forEach((channel) => {
if(channel.type == "GUILD_TEXT"
&& channel.permissionsFor(client.user).has("VIEW_CHANNEL")
&& channel.permissionsFor(client.user).has("SEND_MESSAGES")) {
channels.push(channel.id);
}
})
new ServerPost({ //TODO: figure out a way to not need to upload dummy entry to database
_id: guild.id,
channels: channels,
queries: [{
query: "asgagagasgasag",
subreddit: "asgagagasgasag"
}]
}).save();
registerCommands();
});
/**
* Processes to run when the bot is removed from a Discord server.
* @listens "guildDelete"
*/
client.on("guildDelete", guild => {
clearInterval(handle);
ServerPost.findOneAndRemove({_id: guild.id}).then(data => {});
registerCommands();
});
client.login(process.env.DISCORDBOTTOKEN); // Formally turn on bot