Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from io import BytesIO
- from pathlib import Path
- import discord
- 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)
- if message.content.startswith('!links') and LINKS:
- await message.channel.send(LINKS)
- 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