aboutsummaryrefslogtreecommitdiffstats
path: root/bot/cogs/monstersurvey.py
blob: 5e94b8a5bf015892c6c72821a200baec68f06d0e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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))