-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
139 lines (122 loc) · 4.48 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
import pkg from "snoostorm";
const { CommentStream, SubmissionStream } = pkg;
import Snoowrap from "snoowrap";
import getUrls from "get-urls";
import response from "./responses.js"; // Container for bot responses to users
import wordCounter from "./uniqueoccurances.js";
// Setup the Snoowrap client with variables passed from the Heroku env
const client = new Snoowrap({
userAgent: 'INAT_BOT 0.2',
clientId: process.env.CLIENT_ID,
clientSecret: process.env.CLIENT_SECRET,
username: process.env.REDDIT_USER,
password: process.env.REDDIT_PASS
});
// The submission stream from snoostorm
const submissions = new SubmissionStream(client, {
subreddit: "inat",
limit: 10, // pulls the latest 10 threads created
pollTime: 20000, // does this check every 20 seconds, limited by Reddit's restrictions
});
submissions.on("item", submission => {
// Check if the submission has already been moderated
if (notModerated(submission)) {
console.log(submission.title);
let reply = "";
if(nftCheck.test(submission.selftext.toLowerCase()) || nftCheck.test(submission.title.toLowerCase()))
{
reply += response.nft;
submission.remove();
}
// Checks if the post is a offer post
if (includesWord("offer", submission.link_flair_text)) {
// Offer posts require at least 150 words to be posted
if (countWords(submission.selftext) < 150) {
reply += 'Word Count: ' + countWords(submission.selftext) + '\n\n';
reply += response.offerLimit;
submission.remove();
}
}
else {
// All other posts require at least 250 words to be posted
if (countWords(submission.selftext) < 250) {
reply += 'Word Count: ' + countWords(submission.selftext) + '\n\n';
reply += response.wordLimit;
submission.remove();
}
}
// Check if any urls exist in the submission
if (getUrls(submission.selftext).size == 0) {
reply += response.url;
}
// Check if the word mmo appears in the body
if (mmoCheck.test(submission.selftext.toLowerCase()) || mmoCheck.test(submission.title.toLowerCase())) {
reply += response.mmo;
}
// Check if any of the previous checks succeeded and added to the "reply" string
if (reply != "") {
submission.reply(reply);
}
// Checks for the percentage of unique word usage in the post
if (uniquePercentage(submission.selftext) < 40) {
client.composeMessage({
text: submission.url,
subject: "High Repetition Alert",
to: "r/INAT"
});
}
}
});
// The comment stream from snoostorm
const comments = new CommentStream(client, {
subreddit: "inat",
limit: 10, // Same limit and pollTime on both means 60 requests a minute / Reddit's max
pollTime: 20000,
});
comments.on("item", comment => {
let reply = "";
// Check if the comment text contains the scope command
if (includesWord("scope();", comment.body)) {
reply = response.scope;
}
if (reply != "") {
comment.reply(reply);
}
});
// Splits a string by the spaces to get a total word count
const countWords = (str) => {
return str.trim().split(/\s+/).length;
}
// Substring check
const includesWord = (word, str) => {
return str.toLowerCase().includes(word);
}
// MMO Keyword check
const mmoCheck = new RegExp(
["mmo", "mmos", "mmorpg"].map(item => `\\b${item}\\b`).join("|")
)
// NFT Keyword check
const nftCheck = new RegExp(
["nft", "nfts", "crypto", "cryptocurrency", "blockchain", "pay-to-earn", "pay to earn"].map(item => `\\b${item}\\b`).join("|")
)
const uniquePercentage = (str) => {
let occurance = wordCounter(str, false);
return (occurance.uniqueWordsCount * 100) / occurance.totalWords;
}
// Checks if the input submission already has a moderation post by the inat_bot
const notModerated = (submission) => {
if (submission.num_comments > 0) {
//snoowrap is all Promise-based, method takes place within the then callback
client.getSubmission(submission.id).expandReplies().then(thread => {
if (thread.comments.some(e => e['author']['name'] === "inat_bot")) {
return false;
}
else {
return true;
}
})
}
else {
return true;
}
}