async def gif_command(self, ctx, *search: str): """sends a random GIF""" embed = discord.Embed(color=discord.Color.green()) api_key = load_config('MAIN', 'giphy_api_key') async with aiohttp.ClientSession() as session: if not search: response = await session.get(f'https://api.giphy.com/v1/gifs/random?api_key={api_key}') # loads the response data as json data = json.loads(await response.text()) # sets the embed image to the # image from the response try: embed.set_image(url=data['data']['images']['original']['url']) except KeyError: logging.warning('Keyerror in data', extra=data, exc_info=True) raise else: response = await session.get( f'https://api.giphy.com/v1/gifs/search?q={search}&api_key={api_key}&limit=10') # loads the response data as json data = json.loads(await response.text()) # chooses a random gif from the GIFS found by GIPHY gif_choice = random.randint(0, 9) # sets the embed image to the # image from the response try: embed.set_image(url=data['data'][gif_choice]['images']['original']['url']) except KeyError: logging.warning('Keyerror in data', extra=data, exc_info=True) raise await ctx.send(embed=embed)
def __init__(self, bot): self.bot = bot self.config = load_config()
from datetime import datetime, timedelta import aiohttp import discord from dateutil import parser from pytz import timezone from sqlalchemy import asc from bot import load_config from .models import session, Follows config = load_config() async def twitch_api_call(ctx, endpoint, channel, params): async with aiohttp.ClientSession() as session: url = f'https://api.twitch.tv/helix/{endpoint}{channel}{params}' headers = {'Authorization': 'Bearer ' + config['twitch']['token']} async with session.get(url, headers=headers, timeout=60) as resp: data = await resp.json() error_codes = [400, 401, 403, 404, 422, 429, 500, 503] if resp.status == 200: return data elif resp.status in error_codes: await embed_message(ctx, message_type='Error', message=data['error']) elif aiohttp.ServerTimeoutError: await embed_message(ctx, message_type='Error',
#!/usr/bin/env python3 import logging import logging.handlers import asyncio import http_server import bot logging.basicConfig(format="%(asctime)s %(message)s", level=logging.INFO, handlers=[ logging.handlers.TimedRotatingFileHandler( "logs/wvote.log", when="W0", backupCount=10), logging.StreamHandler() ]) bot.load_config() loop = asyncio.get_event_loop() loop.create_task(bot.client.start(bot.client.bot_key)) loop.create_task(http_server.start_http()) loop.run_forever()