-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcommunityMod.js
More file actions
265 lines (243 loc) · 10.5 KB
/
communityMod.js
File metadata and controls
265 lines (243 loc) · 10.5 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
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
// reddit bot for r/ethtrader
var snoowrap = require('snoowrap');
const jsonfile = require('jsonfile');
var client = jsonfile.readFileSync('./.client');
var cache = require('./cache.json');
var log = require('./log.json');
const scores = require('./src/scores');
var users = require('./users.json');
async function updateUsers() {
try {
console.log("Updating user scores");
await scores.update();
users = await jsonfile.readFileSync('./users.json');
} catch(err) {
console.log(err);
}
}
userScores = {};
users.forEach(function(user) {
userScores[user.username] = user.weight;
});
var r = new snoowrap({
userAgent: client.userAgent,
clientId: client.clientId,
clientSecret: client.clientSecret,
username: client.username,
password: client.password
});
var subreddit = r.getSubreddit('ethtrader');
// set local timezone to utc
process.env.TZ = 'UTC';
// sleep function
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
phrase = "[automodremove"
// check if cli argument is "--reset"
if(process.argv[2] == "--reset" || process.argv[3] == "--reset") {
cache.seenIds = [];
jsonfile.writeFileSync('./cache.json', cache);
console.log("Cache reset");
}
async function main() {
try {
foundLast = 0;
foundNewest = 0;
runOnce = 0;
newSeens = 0;
if(cache.seenIds == undefined) {
cache.seenIds = [];
}
if(cache.seenIds.length == 0) {
params = {limit: 100};
runOnce = 1;
} else {
l = 1;
lastSeenId = cache.seenIds[cache.seenIds.length-l];
body = await r.getComment(lastSeenId).body;
while(body == undefined || body == "[deleted]") {
console.log("last comment deleted "+lastSeenId)
l++;
lastSeenId = cache.seenIds[cache.seenIds.length-l];
body = await r.getComment(lastSeenId).body;
}
lastSeen = await r.getComment(lastSeenId);
link = await lastSeen.link_id;
console.log("Last seen comment "+lastSeenId+" "+body+" "+link);
params = {limit: 100, before: lastSeenId};
}
while(foundLast == 0 && foundNewest == 0) {
timeString = new Date().toLocaleTimeString();
console.log("Checking for new comments "+timeString)
foundComment = 0;
if(params.limit == 500) {
before = 1;
} else {
before = 0;
}
await subreddit.getNewComments(params).then( async function(comments) {
// reverse comments so we can start from the oldest
comments.reverse();
for(c in comments) {
comment = comments[c];
if(comment == undefined) continue;
if(comment.body == undefined || comment.body == "[deleted]") continue;
//console.log(comment.name+" "+comment.parent_id+" "+comment.body);
foundComment = 1;
// check if we've already seen this comment
if(cache.seenIds.indexOf(comment.name) === -1) {
if(comment.body.trim().toLowerCase().indexOf(phrase) === 0 || comment.body.trim().toLowerCase().indexOf(phrase) === 1) {
await handleReport(comment);
}
cache.seenIds.push(comment.name);
newSeens = 1;
} else {
foundLast = 1;
}
}
if(!foundComment) {
// check last comment timestamp
getComm = await r.getComment(lastSeenId).created_utc;
// current utc time
now = Math.floor(Date.now() / 1000);
// if last comment is older than 1 hour, check for new comments
if(now - getComm > 3600) {
console.log("Last comment older than 1 hour, checking for new comments from newest");
params = {limit: 500};
foundComment = 1;
} else {
foundLast = 1;
foundNewest = 1;
}
} else {
foundNewest = 1;
foundLast = 1;
}
});
await sleep(500); // being nice to apis
}
} catch(err) {
console.log(err);
}
if(newSeens) {
jsonfile.writeFileSync('./cache.json', cache);
}
}
async function firstRun() {
// check if first or second arg is "--skip"
if(process.argv[2] == "--skip" || process.argv[3] == "--skip") {
await main();
return;
}
await updateUsers();
await main();
}
firstRun();
// run main function every 60 seconds
setInterval(main, 60000);
// update users every 24 hours
setInterval(updateUsers, 86400000);
async function handleReport(comment) {
//console.log(comment);
author = await comment.author.name;
if(userScores[author] == undefined) {
// console.log(" User does not have enough voting power");
} else if(userScores[author] > 20000) {
if(comment.link_id != comment.parent_id) {
//console.log("Checking comment to be removed");
toRemove = await r.getComment(comment.parent_id);
console.log("Found request from "+author);
link = await toRemove.permalink;
console.log(" Reported link: https://www.reddit.com"+link);
isRemoved = await toRemove.removed;
if(isRemoved) {
console.log(" Comment already removed");
return;
}
offendingUser = await toRemove.author.name;
console.log(" Reported user: "+offendingUser);
// check for previous mod reports
reports = await toRemove.mod_reports;
for(i in reports) {
if(reports[i][0] == "User "+author+" requested removal") {
console.log(" Comment already reported");
return;
}
}
if(userScores[offendingUser] > 20000) {
console.log(" User is approved");
// report comment instead of removing
await toRemove.report({reason: "User "+author+" requested removal"});
// send message to author
await r.composeMessage({
to: author,
subject: "Comment reported",
text: "Thank you for helping to keep the subreddit clean! The comment you requested for removal was created by an approved user. Their comment has been reported to the moderators for review.",
});
// log removal if author is not the same as the offending user (testing)
if(author != offendingUser) {
if(log[offendingUser] == undefined) {
log[offendingUser] = {"requestsMade": 0, "reportsMade": 0, "requestsAgainst": 0, "reportsAgainst": 1};
} else {
log[offendingUser].reportsAgainst++;
}
if(log[author] == undefined) {
log[author] = {"requestsMade": 0, "reportsMade": 1, "requestsAgainst": 0, "reportsAgainst": 0};
} else {
log[author].reportsMade++;
}
await jsonfile.writeFileSync('./log.json', log);
}
console.log(" Reported comment");
} else {
console.log(" Removing comment");
await toRemove.remove();
// check if comment was removed
toRemove = await r.getComment(comment.parent_id);
isRemoved = await toRemove.removed;
if(!isRemoved) {
console.log(" Failed");
await toRemove.report({reason: "User "+author+" requested removal"});
await r.composeMessage({
to: author,
subject: "Comment reported",
text: "Thank you for helping to keep the subreddit clean! For some reason, I was unable to remove the comment. The comment has been reported to the moderators for review.",
});
if(author != offendingUser) {
if(log[offendingUser] == undefined) {
log[offendingUser] = {"requestsMade": 0, "reportsMade": 0, "requestsAgainst": 0, "reportsAgainst": 1};
} else {
log[offendingUser].reportsAgainst++;
}
if(log[author] == undefined) {
log[author] = {"requestsMade": 0, "reportsMade": 1, "requestsAgainst": 0, "reportsAgainst": 0};
} else {
log[author].reportsMade++;
}
await jsonfile.writeFileSync('./log.json', log);
}
console.log(" Reported comment");
return;
}
await comment.reply("Thank you for helping to keep the subreddit clean! The comment you reported has been removed.");
// log removal if author is not the same as the offending user (testing)
if(author != offendingUser) {
if(log[offendingUser] == undefined) {
log[offendingUser] = {"requested": 0, "removed": 1};
} else {
log[offendingUser].removed++;
}
if(log[author] == undefined) {
log[author] = {"requested": 1, "removed": 0};
} else {
log[author].requested++;
}
await jsonfile.writeFileSync('./log.json', log);
}
}
} else {
//console.log(" Top level comment, leaving for automod")
}
}
}