Advertisement
Short_Circuit

Derpibooru Random Image Bot for Discord

Dec 27th, 2015
668
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # The DerpiRand Discord bot, by Bytewave
  2.  
  3. # Runs under Python 3.5 - requires discord.py and DerPyBooru
  4. # run 'pip install derpybooru discord.py' at the command line for setup
  5.  
  6. import discord # Discord Python library
  7. from derpibooru import Search, user, sort # DerPyBooru
  8. import threading # Thread library to create listen thread for exitting the script
  9. import os # OS library for os._exit
  10.  
  11. # CONFIG
  12. # Populate these strings with their respective data pls
  13. dc_username    = '' # Discord username
  14. dc_password    = '' # Discord password
  15. command        = '!derpirand' # Chat command
  16. db_api_key     = '' # Derpibooru API key (optional)
  17. user_blacklist = [] # Blacklisted Discord IDs
  18. exit_cmds      = ['exit', 'quit', 'close', 'stop', 'q']
  19. # END CONFIG
  20.  
  21. # EXIT THREADING
  22. class ExitThread(threading.Thread):
  23.     def run(self):
  24.         while True:
  25.             if input().lower() in exit_cmds:
  26.                 print('--------')
  27.                 print('Logging out...')
  28.                 client.logout()
  29.                 print('Success.')
  30.                 os._exit(0)
  31.  
  32. try:
  33.     ExitThread().start()
  34. except:
  35.     print('[ERROR]: ', sys.exc_info()[0])
  36.     print('Halting execution...')
  37.     exit(255)
  38. # END EXIT THREADING
  39.  
  40. client = discord.Client()
  41.  
  42. print('Attempting Discord login...')
  43. client.login(dc_username, dc_password) # Log in to Discord
  44.  
  45. @client.event
  46. def on_message(message):
  47.     if message.content.lower().startswith(command):
  48.         print('Request received!') # Debug and logging
  49.         requester        = message.author
  50.         requester_name   = requester.name
  51.         requester_id     = requester.id
  52.         print('Requester:', requester_name, '|', requester_id)
  53.         print('Channel:  ', message.channel.server.name, '|', message.channel.name)
  54.  
  55.         query = message.content[(len(command) + 1):] # Strip command from message text
  56.         print('Query:    ', query)
  57.  
  58.         query_list = query.split(',') # Split CSV list of tags into list
  59.  
  60.         if requester_id not in user_blacklist:
  61.             client.send_typing(message.channel) # Typing notification while loading
  62.  
  63.             search  = Search().query(*query_list).key(db_api_key).sort_by(sort.RANDOM).limit(1) # DerPyBooru searching
  64.             results = [image for image in search]
  65.  
  66.             if len(results) == 0:
  67.                 print('Result:    No images found.')
  68.                 client.send_message(message.channel, '@' + requester_name + ' (' + query + '): No images found.', [requester], False)
  69.             else:
  70.                 result = results[0]
  71.                 print('Result:   ', result.url)
  72.  
  73.                 client.send_message(message.channel, '@' + requester_name + ' (' + query + '): ' + result.url, [requester], False)
  74.         else:
  75.             print('Result:    User blacklisted, ignoring request.')
  76.  
  77.         print('========')
  78.  
  79. @client.event
  80. def on_ready(): # More debug information
  81.     print('Success!')
  82.     print('--------')
  83.     print('Logged in as:')
  84.     print('Username:   ', dc_username)
  85.     print('Client name:', client.user.name)
  86.     print('ID:         ', client.user.id)
  87.     print('--------')
  88.     print('Configuration info:')
  89.     print('Command:               ', command)
  90.     print('Derpibooru API key:    ', db_api_key)
  91.     print('--------')
  92.     print('Type one of the following to exit:')
  93.     print(*exit_cmds)
  94.     print('========')
  95.  
  96. if __name__ == '__main__':
  97.     client.run() # Actually do our thing.
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement