Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # The DerpiRand Discord bot, by Bytewave
- # Runs under Python 3.5 - requires discord.py and DerPyBooru
- # run 'pip install derpybooru discord.py' at the command line for setup
- import discord # Discord Python library
- from derpibooru import Search, user, sort # DerPyBooru
- import threading # Thread library to create listen thread for exitting the script
- import os # OS library for os._exit
- # CONFIG
- # Populate these strings with their respective data pls
- dc_username = '' # Discord username
- dc_password = '' # Discord password
- command = '!derpirand' # Chat command
- db_api_key = '' # Derpibooru API key (optional)
- user_blacklist = [] # Blacklisted Discord IDs
- exit_cmds = ['exit', 'quit', 'close', 'stop', 'q']
- # END CONFIG
- # EXIT THREADING
- class ExitThread(threading.Thread):
- def run(self):
- while True:
- if input().lower() in exit_cmds:
- print('--------')
- print('Logging out...')
- client.logout()
- print('Success.')
- os._exit(0)
- try:
- ExitThread().start()
- except:
- print('[ERROR]: ', sys.exc_info()[0])
- print('Halting execution...')
- exit(255)
- # END EXIT THREADING
- client = discord.Client()
- print('Attempting Discord login...')
- client.login(dc_username, dc_password) # Log in to Discord
- @client.event
- def on_message(message):
- if message.content.lower().startswith(command):
- print('Request received!') # Debug and logging
- requester = message.author
- requester_name = requester.name
- requester_id = requester.id
- print('Requester:', requester_name, '|', requester_id)
- print('Channel: ', message.channel.server.name, '|', message.channel.name)
- query = message.content[(len(command) + 1):] # Strip command from message text
- print('Query: ', query)
- query_list = query.split(',') # Split CSV list of tags into list
- if requester_id not in user_blacklist:
- client.send_typing(message.channel) # Typing notification while loading
- search = Search().query(*query_list).key(db_api_key).sort_by(sort.RANDOM).limit(1) # DerPyBooru searching
- results = [image for image in search]
- if len(results) == 0:
- print('Result: No images found.')
- client.send_message(message.channel, '@' + requester_name + ' (' + query + '): No images found.', [requester], False)
- else:
- result = results[0]
- print('Result: ', result.url)
- client.send_message(message.channel, '@' + requester_name + ' (' + query + '): ' + result.url, [requester], False)
- else:
- print('Result: User blacklisted, ignoring request.')
- print('========')
- @client.event
- def on_ready(): # More debug information
- print('Success!')
- print('--------')
- print('Logged in as:')
- print('Username: ', dc_username)
- print('Client name:', client.user.name)
- print('ID: ', client.user.id)
- print('--------')
- print('Configuration info:')
- print('Command: ', command)
- print('Derpibooru API key: ', db_api_key)
- print('--------')
- print('Type one of the following to exit:')
- print(*exit_cmds)
- print('========')
- if __name__ == '__main__':
- client.run() # Actually do our thing.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement