Advertisement
Pyorot

Pyobot Duck

Nov 26th, 2019
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // after the bot first connects to Discord, it starts the loop by running
  2. // setTimeout(()=>{duck('research')}, 1000*5)
  3.  
  4. // Duck
  5. var nextDuckCall
  6. duckPics = {'boss': null, 'research': null, 'eggs': null} // store url to detected image
  7. function duck(handle) { // called on infographic handle (the key in the above dict)
  8.     let timeout = (1000*60*2)*(1+Math.random()/2) // timeout (in s) uniformly distrib. on [2,3]
  9.     // use cloudscraper to get past cloudflare. fetch entire leekduck webpage at handle
  10.     cloudscraper.get(`http://leekduck.com/${handle}/`, async function(error, response, body) { // result goes in "body"
  11.         if (error) {
  12.             console.log('CS error:', error)
  13.         } else {
  14.             let result // url to detected image. use regex to match hyperlinks detected in webpage body
  15.             switch (handle) {
  16.                 case "research":
  17.                     result = 'https://leekduck.com/' + body.match(/assets\/img\/research\/field\/.+\.jpg/)[0]
  18.                     break
  19.                 case "boss":
  20.                     result = 'https://leekduck.com/' + body.match(/assets\/img\/raid-bosses\/graphic\/.+\.jpg/)[0]
  21.                     break
  22.                 case "eggs":
  23.                     result = 'https://leekduck.com/' + body.match(/assets\/img\/eggs\/[^d]+\.jpg/)[0]
  24.                     break
  25.                 default:
  26.                     result = ''
  27.                     break
  28.             }
  29.             if (result) { // if url found
  30.                 if (duckPics[handle] != result) { // and not the same as before
  31.                     text = `New ${handle} infographic from LeekDuck:`
  32.                     file = result
  33.                     attachment = {files: [file]}
  34.                     // post image to test chan
  35.                     bot.channels.get(pysstatus).send(text, attachment).catch(console.error)
  36.                     console.log(`Duck updated ${handle} (${result}).`)
  37.                     // repost image to real chan if not first-run (note that bot auto-restarts every day)
  38.                     if (duckPics[handle]) { // first-run = is there a url in the dict yet?
  39.                         bot.channels.get(pysstatus).send(`Duck sent! ${handle} (${result}).`)
  40.                         bot.channels.get(pglgeneral).send(text, attachment).catch(console.error)
  41.                         console.log(`Duck sent ${handle} (${result}).`)
  42.                         timeout = 1000*60*50 // override timeout w/ 50 mins to prevent spam
  43.                     }
  44.                     duckPics[handle] = result // save new url to compare with next time
  45.                 }
  46.             }
  47.         }
  48.         nextHandle = {'boss': 'research', 'research': 'eggs', 'eggs': 'boss'}[handle] // increment to next handle
  49.         nextDuckCall = setTimeout(()=>{duck(nextHandle)}, timeout) // call self again with timeout
  50.     })
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement