-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
60 lines (47 loc) · 1.36 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
const Snoowrap = require('snoowrap')
const dotenv = require('dotenv')
const chalk = require('chalk')
const fs = require('fs')
const showError = require('./lib/showError')
const urlChecker = require('./lib/urlChecker')
const makeComment = require('./lib/makeComment')
console.log(chalk.cyan.bold('GfycatDetailsConvert is booting up...'))
dotenv.config()
if (!('REDDIT_USERAGENT' in process.env)) {
showError('We wasn\'t able to find enviroment variables with credentials.')
}
if (!fs.existsSync('comment.tpl')) {
showError('We wasn\'t able to find comment.tpl file.')
}
const bot = new Snoowrap({
userAgent: process.env.REDDIT_USERAGENT,
clientId: process.env.REDDIT_CLIENTID,
clientSecret: process.env.REDDIT_CLIENTSECRET,
username: process.env.REDDIT_USERNAME,
password: process.env.REDDIT_PASSWORD
})
bot.config({
requestDelay: 2000,
continueAfterRatelimitError: true,
maxRetryAttempts: 5,
debug: process.env.NODE_ENV !== 'production'
})
async function pollNewPosts () {
let lastChecked
while (true) {
const posts = await bot.getNew('all', {
before: lastChecked,
show: 'all',
amount: 1000
})
if (posts.length > 0) {
lastChecked = posts[0].name
}
await Promise.all(posts
.filter(urlChecker)
.map(makeComment)
)
await new Promise(resolve => setTimeout(resolve, 60 * 1000))
}
}
pollNewPosts()