-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.mjs
More file actions
124 lines (104 loc) · 3.21 KB
/
app.mjs
File metadata and controls
124 lines (104 loc) · 3.21 KB
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
require('dotenv').config()
const snoowrap = require('snoowrap')
const wrap = new snoowrap({
userAgent: 'nodejs:noice-bot:v0.1 (by /u/kriswithakthatplays)',
clientId: process.env.CLIENT_ID,
clientSecret: process.env.CLIENT_SECRET,
username: process.env.REDDIT_USER,
password: process.env.REDDIT_PASS
})
const standardBotDescription = "****^(I am a bot. *bleep bloop*)"
const uniqueReplies = [
"Noice, you say? To that, I raise you a noice.",
"Ha! Noice my dude.",
"Oh yeah? That is pretty noice.",
"Noice.",
"Quite noice.",
"Very noice."
]
let seenCommentList = []
let seenSubmissionList = []
let doNotReplyTo = []
let stalkingReddits = [
'testingground4bots',
'AwesomeBots',
'AInotHuman',
'scp',
'kriswithak'
]
function* replyGenerator() {
let replyList = [...uniqueReplies]
let replyCount = 0
while (true) {
yield `${replyList[replyCount]}
${standardBotDescription}`
replyCount++
if (replyCount >= replyList.length) {
replyCount = 0
}
}
}
const getNextReply = replyGenerator()
function nextReply() {
return getNextReply.next()['value']
}
function haveWeSeenThisComment(comment) {
return seenCommentList.includes(comment.id)
}
function haveWeSeenThisSubmission(submission) {
return seenSubmissionList.includes(submission.id)
}
function checkIfShouldReply (instance) {
return (
(
(instance.body && ~instance.body.toLowerCase().indexOf('noice')) ||
(instance.selftext && ~instance.selftext.toLowerCase().indexOf('noice')) ||
(instance.title && ~instance.title.toLowerCase().indexOf('noice'))
) &&
(!doNotReplyTo.includes(instance.author.id)) &&
(!haveWeSeenThisComment(instance) || !haveWeSeenThisSubmission(instance))
)
}
async function lookForTargets() {
/*
We're going to take the list of subreddits, map them to a long list of comments,
then filter them down to the ones we need to reply to. Then, we shove the ones
we need to reply to to the function that replies.
Ezpz
*/
const rawCommentsList = await Promise.all(
stalkingReddits.map(subRedditName => {
return wrap.getSubreddit(subRedditName).getNewComments()
})
)
const commentsToReplyTo = rawCommentsList
// Reduce the list of lists to a flat list
.reduce((allComments, subredditComments) => [...allComments, ...subredditComments])
// Look at the post and check a bunch of criteria in the check function
.filter(checkIfShouldReply)
if (commentsToReplyTo.length > 0) {
// Time to reply to posts and make sure we don't do it twice
// replyToComments(commentsToReplyTo)
}
return
}
function replyToComments(commentList) {
for (const comment of commentsToReplyTo) {
console.log(comment)
comment.reply(nextReply()).then(done => {
seenCommentList.push(comment.id)
})
}
}
async function init () {
let me = await wrap.getMe()
doNotReplyTo.push(me.id)
console.log("Initial loading done. Start polling for comments and posts")
setInterval(lookForTargets, 5000)
}
export {
init,
checkIfShouldReply,
lookForTargets,
wrap
}