Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // after the bot first connects to Discord, it starts the loop by running
- // setTimeout(()=>{duck('research')}, 1000*5)
- // Duck
- var nextDuckCall
- duckPics = {'boss': null, 'research': null, 'eggs': null} // store url to detected image
- function duck(handle) { // called on infographic handle (the key in the above dict)
- let timeout = (1000*60*2)*(1+Math.random()/2) // timeout (in s) uniformly distrib. on [2,3]
- // use cloudscraper to get past cloudflare. fetch entire leekduck webpage at handle
- cloudscraper.get(`http://leekduck.com/${handle}/`, async function(error, response, body) { // result goes in "body"
- if (error) {
- console.log('CS error:', error)
- } else {
- let result // url to detected image. use regex to match hyperlinks detected in webpage body
- switch (handle) {
- case "research":
- result = 'https://leekduck.com/' + body.match(/assets\/img\/research\/field\/.+\.jpg/)[0]
- break
- case "boss":
- result = 'https://leekduck.com/' + body.match(/assets\/img\/raid-bosses\/graphic\/.+\.jpg/)[0]
- break
- case "eggs":
- result = 'https://leekduck.com/' + body.match(/assets\/img\/eggs\/[^d]+\.jpg/)[0]
- break
- default:
- result = ''
- break
- }
- if (result) { // if url found
- if (duckPics[handle] != result) { // and not the same as before
- text = `New ${handle} infographic from LeekDuck:`
- file = result
- attachment = {files: [file]}
- // post image to test chan
- bot.channels.get(pysstatus).send(text, attachment).catch(console.error)
- console.log(`Duck updated ${handle} (${result}).`)
- // repost image to real chan if not first-run (note that bot auto-restarts every day)
- if (duckPics[handle]) { // first-run = is there a url in the dict yet?
- bot.channels.get(pysstatus).send(`Duck sent! ${handle} (${result}).`)
- bot.channels.get(pglgeneral).send(text, attachment).catch(console.error)
- console.log(`Duck sent ${handle} (${result}).`)
- timeout = 1000*60*50 // override timeout w/ 50 mins to prevent spam
- }
- duckPics[handle] = result // save new url to compare with next time
- }
- }
- }
- nextHandle = {'boss': 'research', 'research': 'eggs', 'eggs': 'boss'}[handle] // increment to next handle
- nextDuckCall = setTimeout(()=>{duck(nextHandle)}, timeout) // call self again with timeout
- })
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement