Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import shlex
- import sys
- from io import BytesIO
- from pathlib import Path
- import discord
- from googlesearch import search as google_search
- from aiohttp import request
- class CommandHandler:
- def __init__(self, bot):
- self.bot = bot
- # currently this reference is not used
- async def command(self, message):
- if message.content.startswith('!wetter'):
- await self.wetter(message)
- elif message.content.startswith('!links') and LINKS:
- await message.channel.send(LINKS)
- elif message.content.startswith('!google'):
- await self.google(message)
- elif message.content.startswith('!umfrage'):
- await self.start_vote(message)
- elif message.content.startswith('!kill'):
- await message.channel.send('Ohhhh noooooooo... aaaaarghhh')
- sys.exit(255)
- async def start_vote(self, message):
- channel = message.channel
- content = message.content
- try:
- _, vote = content.split(maxsplit=1)
- question, *answers = shlex.split(vote)
- print(question, list(enumerate(answers)))
- except ValueError:
- await channel.send('!umfrage "Frage?" "Antwort 1" "Antwort 2" ...')
- return
- try:
- await channel.send(f'{message.author.mention} startet eine Umfrage.')
- await channel.send(question)
- answers = [f'{idx:02d}: {answer}' for idx, answer in enumerate(answers, 1)]
- await channel.send('\n'.join(answers))
- except Exception as e:
- await channel.send(str(e)[:2000])
- async def google(self, message):
- try:
- _, query = message.content.split(maxsplit=1)
- except ValueError:
- return
- results = '\n'.join(google_search(query, lang='de', stop=5, pause=0))
- await message.channel.send(results)
- async def wetter(self, message):
- try:
- _, city = message.content.split()
- except ValueError:
- return
- url = f'https://wttr.in/{city.lower()}.png'
- headers = {
- 'User-Agent': 'curl',
- 'Accept-Language': 'de',
- }
- async with request('GET', url, headers=headers) as rep:
- if rep.status == 200:
- try:
- picture = await rep.read()
- except Exception:
- return
- discord_file = discord.File(BytesIO(picture), f'wetter_in_{city}.png')
- await message.channel.send(file=discord_file)
- class Bot(discord.Client):
- def __init__(self):
- super().__init__()
- self.command_handler = CommandHandler(self)
- async def on_message(self, message):
- if message.author == self.user:
- return
- if message.content.startswith('!'):
- await self.command_handler.command(message)
- async def on_ready(self):
- print(f'Logged in as {self.user.name} with id {self.user.id}')
- if __name__ == '__main__':
- token = Path('.discord_bot_token')
- if not token.exists():
- print(
- f'{token} does not exist. Please create this file with your api token',
- file=sys.stdout
- )
- sys.exit(1)
- links = Path('discord_bot_links.txt')
- if links.exists():
- LINKS = links.read_text()
- else:
- LINKS = None
- token_str = token.read_text().strip()
- bot = Bot()
- bot.run(token_str)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement