Advertisement
Guest User

Untitled

a guest
Feb 17th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.49 KB | None | 0 0
  1. import discord
  2. import os
  3. import logging
  4. import re
  5. import pathlib
  6. from google_images_download import google_images_download
  7.  
  8. TOKEN = 'XXXXX'
  9. client = discord.Client()
  10.  
  11. @client.event
  12. async def on_message(message):
  13.     #don't do anything if it is us
  14.     if message.author == client.user:
  15.         return
  16.  
  17.     if re.match('!mugabe\squote', message.content):
  18.         #TODO: We need to integrate with the mugabe api
  19.  
  20.         reply = "This should be a robert mugabe quote"
  21.  
  22.         await client.send_message(message.channel, reply)
  23.     elif re.match('!mugabe\simage', message.content):
  24.         await get_mugabe_pic(message)
  25.     elif re.match('!mugabe\shelp', message.content):
  26.         await help_message(message)
  27.     elif re.match('!mugabe\sabout', message.content):
  28.         await about(message)
  29.     else:
  30.         await invalid_command(message)
  31.  
  32.  
  33. @client.event
  34. async def on_ready():
  35.     print('Logged in:', client.user.name, client.user.id)
  36.  
  37.  
  38. async def help_message(message):
  39.     help_message = """
  40.        ```
  41.            *!mugabe quote* - I will find a Robert Mugabe Quote
  42.            *!mugabe image* - I will find a Picture of Robert Mugabe
  43.            *!mugabe help* - I will help you how to use me :D
  44.            *!mugabe about* - About Mugabe bot
  45.        ```
  46.    """
  47.     await client.send_message(message.channel, help_message)
  48.  
  49. async def about(message):
  50.     about_msg = """
  51.        This is a joke mugabe bot so Mr. Jesus could learn how to write discord bots.
  52.    """
  53.  
  54.     await client.send_message(message.channel, about_msg)
  55.  
  56. async def invalid_command(message):
  57.     invalid_command_msg = "Sorry I didn't understand that. Type `!mugabe help` to show what I respond to."
  58.  
  59.     await client.send_message(message.channel, invalid_command_msg)
  60.  
  61. async def get_mugabe_pic(message):
  62.     #for now assume anything after the
  63.     searchterm = re.sub('^!mugabe\simage\s', '', message.content)
  64.  
  65.     response = google_images_download.googleimagesdownload()
  66.  
  67.     arguments = {
  68.         'keywords': "robert mugabe",
  69.         'limit': 20,
  70.         'print_urls' : True
  71.     }
  72.  
  73.     #if we don't have a search term then
  74.     if searchterm.strip():
  75.         arguments['keywords'] = "robert mugabe {}".format(searchterm)
  76.  
  77.     paths = response.download(arguments)
  78.  
  79.     images = paths[list(paths.keys())[0]]
  80.  
  81.     if len(images) is not 0:
  82.         with open(images[0], 'rb') as pic:
  83.             await client.send_file(message.channel, pic)    
  84.  
  85.  
  86. if __name__ == '__main__':
  87.     client.run(TOKEN)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement