diff --git a/bot/cogs/snakes.py b/bot/cogs/snakes.py index c9ed8042..5ea78227 100644 --- a/bot/cogs/snakes.py +++ b/bot/cogs/snakes.py @@ -2,6 +2,11 @@ import logging from typing import Any, Dict +import discord +import random +import aiohttp +import json +from bs4 import BeautifulSoup from discord.ext.commands import AutoShardedBot, Context, command log = logging.getLogger(__name__) @@ -29,6 +34,25 @@ async def get_snek(self, name: str = None) -> Dict[str, Any]: :return: A dict containing information on a snake """ + # Getting the summary + + async with aiohttp.ClientSession() as session: + async with session.get('https://en.wikipedia.org/w/api.php?action=parse&format=json&page=' + name + '§ion=1') as resp: + text_data = await resp.json() + text = json.loads(text_data) + html = text["text"]["*"] + soup = BeautifulSoup(html) + summary = soup.find('p') + + # Getting the image + + async with aiohttp.ClientSession() as session: + async with session.get('https://en.wikipedia.org/w/api.php?format=json&action=query&titles=' + name + '&prop=pageimages&pithumbsize=300') as resp: + image_data = await resp.json() + image = json.loads(image_data) + url = image["thumbnail"]["source"] + + return {'summary': summary, 'url': url} @command() async def get(self, ctx: Context, name: str = None): """ @@ -41,8 +65,20 @@ async def get(self, ctx: Context, name: str = None): :param name: Optional, the name of the snake to get information for - omit for a random snake """ - # Any additional commands can be placed here. Be creative, but keep it to a reasonable amount! + data = await self.get_snek(name) + await ctx.send(data) + + # The snake moult command + @command() + async def moult(self, ctx: Context): + await ctx.send("sssss... moulting in progress...") + green = discord.utils.get(ctx.guild.roles, name="Green Skin") + black = discord.utils.get(ctx.guild.roles, name="Black Skin") + yellow = discord.utils.get(ctx.guild.roles, name="Yellow Skin") + await ctx.guild.me.remove_roles() + await ctx.guild.me.add_roles(random.choice(green, black, yellow)) + await ctx.send("sssss... and now you can't see me...") def setup(bot): bot.add_cog(Snakes(bot))