-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.js
355 lines (298 loc) · 9.46 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
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
//
// Requires
//
const app = require('./package.json');
//
// Database
//
let db = {
"users": {},
"comments": [],
"submissions": {}
}
//
// Configs
//
const config = require('./config/config.json');
const subredditsConfig = require('./config/subreddits.json');
const repliesConfig = require('./config/replies.json');
const typosConfig = require('./config/typos.json');
//
// Includes
//
const snoowrap = require('snoowrap');
const snoostorm = require('snoostorm');
const Reply = require('./libraries/reply.js').Reply;
const Typo = require('typogen').Typo;
const emotionDetection = require('emotional_alert');
const Sentiment = require('sentiment');
const fs = require('fs');
//
// Objects
//
const sentiment = new Sentiment();
const reddit = new snoowrap({
userAgent: config.redditCredentials.userAgent.replace("{version}", `v${app.version}`),
clientId: config.redditCredentials.appID,
clientSecret: config.redditCredentials.appSecret,
username: config.redditCredentials.username,
password: config.redditCredentials.password
});
//
// Some people like to use console.log where it doesn't belong. Thanks for that.
//
console._log = console.log;
console.log = function () {
};
//
// Global Variables
//
let replies = [];
let subreddits = [];
let typoEngine;
let requireDBSave = false;
//
// Functions
//
function saveDB() {
if (!requireDBSave) {
return;
}
fs.writeFileSync("./db.json", JSON.stringify(db, null, 2));
requireDBSave = false;
}
async function sleep(time) {
await new Promise(r => setTimeout(r, time));
}
function log(string) {
let date = new Date();
let time = (date.getMonth() + 1) + "/" + (date.getDate() + 1) + "/" + date.getFullYear() + ` ${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}`;
let msg = `[${time}] ${string}`;
console._log(msg);
fs.appendFileSync('bot.log', `${msg}\n`);
}
function shuffle(array) {
let index = array.length;
let temp;
let rIndex;
while (0 !== index) {
rIndex = Math.floor(Math.random() * index);
index--;
temp = array[index];
array[index] = array[rIndex];
array[rIndex] = temp;
}
return array;
}
function getSubmission(comment) {
let link = comment.permalink;
let split = link.split("/");
return split[4];
}
function removeFromArray(object, array) {
let index = array.indexOf(object);
if (index !== -1) array.splice(index, 1);
}
//
// Flow
//
function startListener(subreddit) {
subreddits.push(subreddit);
log(`Listening for comments on: ${subreddit.displayName}`);
let options = {
subreddit: subreddit.name,
results: subreddit.results,
pollTime: subreddit.pollTime * 1000
};
subreddit.listener = new snoostorm.CommentStream(reddit, options);
try {
subreddit.listener.on("item", comment => {
// Debug line for data gathering
if (config.debugMode) {
log(`Comment Received: ${comment.id} Subreddit: ${subreddit.displayName} Message: ${comment.body}`);
let rebuild = [];
let body = comment.body.replace(/([\uE000-\uF8FF]|\uD83C[\uDF00-\uDFFF]|\uD83D[\uDC00-\uDDFF])/g, "");
body.split(" ").forEach(word => {
if (word.length < 3) {
return;
}
if (word.includes("/r/")
|| word.includes("/u/")
|| word.includes("://")) {
return;
}
rebuild.push(word);
});
let naturalized = rebuild.join(" ");
let emotional = emotionDetection(naturalized);
let sent = sentiment.analyze(naturalized);
log(`- Emotion: ${emotional.bayes.prediction} Emotion Probability: ${emotional.bayes.proba} Vulnerability: ${emotional.emotional} Sentiment: ${sent.comparative}`);
}
// End debug lines
commentEvent({
subreddit: subreddit,
comment: comment
});
});
subreddit.listener.on("error", function (err) {
log(`Error encountered listening on ${subreddit.displayName}, Error: ${err.message}`);
});
} catch (err) {
log(`Error encountered listening on ${subreddit.displayName}, Error: ${err.message}`);
}
}
function commentEvent(event) {
if (event.comment.author.name.toLowerCase() == config.redditCredentials.username) {
return;
}
if (db.comments.includes(event.comment.id)) {
return;
}
event.time = new Date().getTime();
let replyArr = [...replies];
shuffle(replyArr);
replyArr.some(reply => {
if (reply.isTriggered(event)) {
if (reply.oncePerUser) {
if (Object.keys(db.users).includes(event.comment.author.name) &&
db.users[event.comment.author.name].includes(reply.name)) {
return false;
}
}
if (reply.oncePerSubmission) {
let sub = getSubmission(event.comment);
if (Object.keys(db.submissions).includes(sub) &&
db.submissions[sub].includes(reply.name)) {
return false;
}
}
log("Pushing to QUEUE!");
addToReplyQueue(reply, event);
return true;
}
});
}
function addToReplyQueue(reply, event) {
if (!Object.keys(db.users).includes(event.comment.author.name)) {
db.users[event.comment.author.name] = [];
}
let sub = getSubmission(event.comment);
if (!Object.keys(db.submissions).includes(sub)) {
db.submissions[sub] = [];
}
db.users[event.comment.author.name].push(reply.name);
db.submissions[sub].push(reply.name);
db.comments.push(event.comment.id);
requireDBSave = true;
let rep = {
reply: reply,
event: event
};
if (reply.replyDelay) {
let delay = reply.replyDelay;
if (reply.delayFuzz) {
delay += Math.floor(Math.random() * reply.delayFuzz);
}
rep.delay = delay;
}
event.subreddit.replyQueue.push(rep);
}
async function processReplyQueues() {
let tasks = [];
subreddits.forEach(subreddit => {
tasks.push(processReplyQueue(subreddit));
});
await Promise.all(tasks);
saveDB();
setTimeout(processReplyQueues, 1);
}
async function processReplyQueue(subreddit) {
let until = subreddit.lastReply + subreddit.cooldown
let time = new Date().getTime();
if (until > time) {
await sleep(until - time);
}
subreddit.replyQueue.some(reply => {
let ready = reply.event.time + reply.delay;
if (new Date().getTime() >= ready) {
doReply(reply);
return true;
}
});
}
function doReply(reply) {
let date = new Date();
let message = reply.reply.generateMessage();
if (config.debugMode) {
log("----------------------------------------------");
log(`Would of replied to Comment: ${reply.event.comment.id}`);
log(`Subreddit: ${reply.event.subreddit.displayName}`);
log(`Author: ${reply.event.comment.author.name}`);
log(`Message: ${reply.event.comment.body}`);
log(`Reply: ${message}`);
log("----------------------------------------------");
removeFromArray(reply, reply.event.subreddit.replyQueue);
reply.event.subreddit.lastReply = date.getTime();
} else {
reply.event.comment.reply(message).then(result => {
log("----------------------------------------------");
log(`Replied to Comment: ${reply.event.comment.id}`);
log(`Author: ${reply.event.comment.author.name}`);
log(`Message: ${reply.event.comment.body}`);
log(`Reply: ${message}`);
log("----------------------------------------------");
removeFromArray(reply, reply.event.subreddit.replyQueue);
reply.event.subreddit.lastReply = date.getTime();
if (reply.reply.cooldown > 0) {
reply.reply.putOnCooldown();
}
}, err => {
log(`Failed to reply to Comment: ${reply.event.comment.id} Error: ${err.message}`);
});
}
}
//
// Entry Point
//
process.stdout.write('\033c');
console._log("Glamour - An Advanced Reddit Bot");
console._log(`Version: ${app.version}`);
console._log(`Description: ${app.description}`);
console._log(`Author: ${app.author}`);
console._log("------------------------------------\n");
log("Starting up");
log("Loading DB");
if (fs.existsSync('./db.json')) {
let dbL = require('./db.json');
Object.assign(db, dbL);
}
log("Setting up typo system");
typoEngine = new Typo(typosConfig.typos);
log("Starting Reply Processor");
setTimeout(processReplyQueues, 1000);
log("Building replies");
repliesConfig.replies.forEach(rep => {
let reply;
try {
reply = new Reply(rep, typoEngine);
} catch (err) {
log(err);
return;
}
replies.push(reply);
});
log("Setting up Subreddits");
subredditsConfig.subreddits.forEach(sub => {
log(`Preparing ${sub.name}`);
let subreddit = {
name: sub.name,
displayName: `/r/${sub.name}`,
cooldown: sub.cooldown * 60000,
reddit: reddit.getSubreddit(sub.name),
replyQueue: [],
lastReply: 0,
pollTime: sub.pollTime,
results: sub.results
};
startListener(subreddit);
});