SHARE
TWEET

extended ....

DeaD_EyE Jul 31st, 2019 77 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import shlex
  2. import sys
  3. from io import BytesIO
  4. from pathlib import Path
  5.  
  6. import discord
  7. from googlesearch import search as google_search
  8. from aiohttp import request
  9.  
  10.  
  11. class CommandHandler:
  12.     def __init__(self, bot):
  13.         self.bot = bot
  14.         # currently this reference is not used
  15.    
  16.     async def command(self, message):
  17.         if message.content.startswith('!wetter'):
  18.             await self.wetter(message)
  19.         elif message.content.startswith('!links') and LINKS:
  20.             await message.channel.send(LINKS)
  21.         elif message.content.startswith('!google'):
  22.             await self.google(message)
  23.         elif message.content.startswith('!umfrage'):
  24.             await self.start_vote(message)
  25.         elif message.content.startswith('!kill'):
  26.             await message.channel.send('Ohhhh noooooooo... aaaaarghhh')
  27.             sys.exit(255)
  28.    
  29.     async def start_vote(self, message):
  30.         channel = message.channel
  31.         content = message.content
  32.         try:
  33.             _, vote = content.split(maxsplit=1)
  34.             question, *answers = shlex.split(vote)
  35.             print(question, list(enumerate(answers)))
  36.         except ValueError:
  37.             await channel.send('!umfrage "Frage?" "Antwort 1" "Antwort 2" ...')
  38.             return
  39.         try:
  40.             await channel.send(f'{message.author.mention} startet eine Umfrage.')
  41.             await channel.send(question)
  42.             answers = [f'{idx:02d}: {answer}' for idx, answer in enumerate(answers, 1)]
  43.             await channel.send('\n'.join(answers))
  44.         except Exception as e:
  45.             await channel.send(str(e)[:2000])
  46.    
  47.     async def google(self, message):
  48.         try:
  49.             _, query = message.content.split(maxsplit=1)
  50.         except ValueError:
  51.             return
  52.         results = '\n'.join(google_search(query, lang='de', stop=5, pause=0))
  53.         await message.channel.send(results)
  54.    
  55.     async def wetter(self, message):
  56.         try:
  57.             _, city = message.content.split()
  58.         except ValueError:
  59.             return
  60.         url = f'https://wttr.in/{city.lower()}.png'
  61.         headers = {
  62.             'User-Agent': 'curl',
  63.             'Accept-Language': 'de',
  64.             }
  65.         async with request('GET', url, headers=headers) as rep:
  66.             if rep.status == 200:
  67.                 try:
  68.                     picture = await rep.read()
  69.                 except Exception:
  70.                     return
  71.                 discord_file = discord.File(BytesIO(picture), f'wetter_in_{city}.png')
  72.                 await message.channel.send(file=discord_file)
  73.  
  74.  
  75. class Bot(discord.Client):
  76.  
  77.     def __init__(self):
  78.         super().__init__()
  79.         self.command_handler = CommandHandler(self)
  80.  
  81.     async def on_message(self, message):
  82.         if message.author == self.user:
  83.             return
  84.         if message.content.startswith('!'):
  85.             await self.command_handler.command(message)
  86.  
  87.     async def on_ready(self):
  88.         print(f'Logged in as {self.user.name} with id {self.user.id}')
  89.  
  90.  
  91. if __name__ == '__main__':
  92.     token = Path('.discord_bot_token')
  93.     if not token.exists():
  94.         print(
  95.             f'{token} does not exist. Please create this file with your api token',
  96.             file=sys.stdout
  97.             )
  98.         sys.exit(1)
  99.     links = Path('discord_bot_links.txt')
  100.     if links.exists():
  101.         LINKS = links.read_text()
  102.     else:
  103.         LINKS = None
  104.     token_str = token.read_text().strip()
  105.  
  106.     bot = Bot()
  107.     bot.run(token_str)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top