diff options
author | 2018-10-11 02:56:02 -0700 | |
---|---|---|
committer | 2018-10-11 02:56:02 -0700 | |
commit | 21de080a3717a5642f5f081079227e6e7f21e271 (patch) | |
tree | 389fe48083cada62f073eb930b8778752fa07c25 /bot/cogs/monstersurvey.py | |
parent | Merge pull request #34 from markylon/master (diff) |
Initial commit.
Diffstat (limited to 'bot/cogs/monstersurvey.py')
-rw-r--r-- | bot/cogs/monstersurvey.py | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/bot/cogs/monstersurvey.py b/bot/cogs/monstersurvey.py new file mode 100644 index 00000000..5e94b8a5 --- /dev/null +++ b/bot/cogs/monstersurvey.py @@ -0,0 +1,74 @@ +import logging +import json +from discord.ext import commands +from discord.ext.commands import Bot, Context +import discord +from typing import Optional +import aiohttp +from http import HTTPStatus + +log = logging.getLogger(__name__) + + +class MonsterSurvey: + + def __init__(self, bot: Bot): + self.bot = Bot + self._monsters = None + + async def monster_summary(self, monster): + monster = monster.replace(' ', '_') + wiki_url = "http://en.wikipedia.org/w/api.php" \ + "?format=json" \ + "&action=query" \ + "&prop=extracts" \ + "&prop=pageimages" \ + "&explaintext&exintro" \ + "&redirects=1" \ + f"&titles={monster}" + print(wiki_url) + async with aiohttp.ClientSession() as session: + response = await session.get(wiki_url) + if response.status == HTTPStatus.OK: + result = json.loads(await response.text()) + return next(iter(result['query']['pages'].values()))['extract'] + + @property + def survey_data(self) -> dict: + """Get an updated instance of the survey data at all times""" + with open('../bot/resources/monstersurvey.json', 'r') as f: + self.monsters = json.load(f) + return self.monsters + + @commands.group(name='monster', aliases=('ms',), invoke_without_command=True) + async def monster_group(self, ctx: Context): + await ctx.invoke(self.bot.get_command(name="help"), 'monstersurvey') + + @monster_group.command(name='vote') + async def monster_vote(self, ctx: Context, name: Optional[str] = None): + embed = discord.Embed(name='Vote for your favorite monster') + if not name: + for k,v in self.monsters.items(): + msg = f"`!monster show {k}` for more information\n" \ + f"`!monster vote {k}` to cast your vote for this monster." + embed.add_field(name=f"{k}", value=f"{msg}", inline=False) + + await ctx.send(embed=embed) + + @monster_group.command(name='show') + async def monster_show(self, ctx: Context, *monster: str): + monster = ' '.join(monster) + if self.monsters.get(monster, None): + summary = await self.monster_summary(monster) + embed = discord.Embed(name=monster) + embed.add_field(name=f"About {monster}", value=summary) + await ctx.send(embed=embed) + + +def setup(bot: Bot): + bot.add_cog(MonsterSurvey(bot)) + + + + + |