From 21de080a3717a5642f5f081079227e6e7f21e271 Mon Sep 17 00:00:00 2001 From: Derek Fitzpatrick Date: Thu, 11 Oct 2018 02:56:02 -0700 Subject: Initial commit. --- bot/cogs/monstersurvey.py | 74 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 bot/cogs/monstersurvey.py (limited to 'bot/cogs/monstersurvey.py') 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)) + + + + + -- cgit v1.2.3 From bea7d364e957e2f3f1d71cbfb00c306588861ec6 Mon Sep 17 00:00:00 2001 From: Derek Fitzpatrick Date: Thu, 11 Oct 2018 14:22:30 -0700 Subject: Merge branch 'deploy' of https://github.com/dfitzpatrick/hacktoberbot into 31-vote-for-favorite-monster # Conflicts: # Pipfile --- bot/cogs/monstersurvey.py | 86 ++++++++++++++++++++++++++++++++--------------- 1 file changed, 58 insertions(+), 28 deletions(-) (limited to 'bot/cogs/monstersurvey.py') diff --git a/bot/cogs/monstersurvey.py b/bot/cogs/monstersurvey.py index 5e94b8a5..46ab2485 100644 --- a/bot/cogs/monstersurvey.py +++ b/bot/cogs/monstersurvey.py @@ -3,66 +3,96 @@ import json from discord.ext import commands from discord.ext.commands import Bot, Context import discord -from typing import Optional +from typing import Optional, Tuple import aiohttp from http import HTTPStatus log = logging.getLogger(__name__) +EMOJIS = { + 'SUCCESS': u"\u2705", + 'ERROR': u"\u274C", +} + 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) + @property + def monsters(self) -> dict: + """Always get the most up to date version""" + path = '../bot/resources/monstersurvey/monstersurvey.json' + with open(path, 'r') as f: + self._monsters = json.load(f) + return self._monsters + + async def monster_info(self, monster) -> Tuple[Optional[str], Optional[str]]: + """Gets information relating to each monster. This first checks if there + are 'summary' and 'image' keys in the json file. If this fails, it will + attempt to pull summary information from Wikipedia. + """ + async def fetch_url(session: aiohttp.ClientSession, url: str) -> dict: + """Use wikipedia api url calls""" + response = await session.get(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 + return next(iter(result['query']['pages'].values())) + summary_url = "http://en.wikipedia.org/w/api.php" \ + "?format=json" \ + "&action=query" \ + "&prop=extracts" \ + "&explaintext&exintro" \ + "&redirects=1" \ + f"&titles={monster}" + image_url = "http://en.wikipedia.org/w/api.php" \ + "?action=query" \ + "&prop=pageimages" \ + "&format=json" \ + "&piprop=original" \ + f"&titles={monster}" + async with aiohttp.ClientSession() as s: + summary = self.monsters[monster].get('summary') \ + or (await fetch_url(s, summary_url)).get('extract') + + image = self.monsters[monster].get('image') \ + or (await fetch_url(s, image_url)).get('original', {}).get('source') + return summary, image @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') + pass @monster_group.command(name='vote') async def monster_vote(self, ctx: Context, name: Optional[str] = None): + """Casts a vote for a particular monster, or displays a list of all + monsters that are available for voting. + """ 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." + 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) - + # TODO: Add logic for voting await ctx.send(embed=embed) @monster_group.command(name='show') async def monster_show(self, ctx: Context, *monster: str): + """Display a detailed description for the monster, and image.""" monster = ' '.join(monster) if self.monsters.get(monster, None): - summary = await self.monster_summary(monster) + summary, image = await self.monster_info(monster) embed = discord.Embed(name=monster) - embed.add_field(name=f"About {monster}", value=summary) + embed.add_field(name=f"About {monster}", value=summary or "No information") + if image: + embed.set_image(url=image) + embed.set_footer(text=f"To vote for {monster}, type `.monster vote {monster}`") await ctx.send(embed=embed) + else: + await ctx.message.add_reaction(EMOJIS['ERROR']) def setup(bot: Bot): -- cgit v1.2.3 From 886e43a3e78cf34c83db73d8c96c52dbbcc16779 Mon Sep 17 00:00:00 2001 From: Derek Fitzpatrick Date: Fri, 12 Oct 2018 00:20:05 -0700 Subject: Refactor monstersurvey. Added leaderboard and show. --- bot/cogs/monstersurvey.py | 177 +++++++++++++------------ bot/resources/monstersurvey/monstersurvey.json | 32 ++++- 2 files changed, 118 insertions(+), 91 deletions(-) (limited to 'bot/cogs/monstersurvey.py') diff --git a/bot/cogs/monstersurvey.py b/bot/cogs/monstersurvey.py index 46ab2485..67bc9051 100644 --- a/bot/cogs/monstersurvey.py +++ b/bot/cogs/monstersurvey.py @@ -1,104 +1,107 @@ -import logging -import json -from discord.ext import commands from discord.ext.commands import Bot, Context -import discord from typing import Optional, Tuple -import aiohttp -from http import HTTPStatus +from discord.ext import commands +from discord import Embed +import discord, logging, json, os log = logging.getLogger(__name__) - EMOJIS = { - 'SUCCESS': u"\u2705", - 'ERROR': u"\u274C", + 'SUCCESS': u'\u2705', + 'ERROR': u'\u274C' } -class MonsterSurvey: - - def __init__(self, bot: Bot): - self.bot = Bot - self._monsters = None - @property - def monsters(self) -> dict: - """Always get the most up to date version""" - path = '../bot/resources/monstersurvey/monstersurvey.json' - with open(path, 'r') as f: - self._monsters = json.load(f) - return self._monsters +class MonsterServey: - async def monster_info(self, monster) -> Tuple[Optional[str], Optional[str]]: - """Gets information relating to each monster. This first checks if there - are 'summary' and 'image' keys in the json file. If this fails, it will - attempt to pull summary information from Wikipedia. - """ - async def fetch_url(session: aiohttp.ClientSession, url: str) -> dict: - """Use wikipedia api url calls""" - response = await session.get(url) - if response.status == HTTPStatus.OK: - result = json.loads(await response.text()) - return next(iter(result['query']['pages'].values())) - summary_url = "http://en.wikipedia.org/w/api.php" \ - "?format=json" \ - "&action=query" \ - "&prop=extracts" \ - "&explaintext&exintro" \ - "&redirects=1" \ - f"&titles={monster}" - image_url = "http://en.wikipedia.org/w/api.php" \ - "?action=query" \ - "&prop=pageimages" \ - "&format=json" \ - "&piprop=original" \ - f"&titles={monster}" - async with aiohttp.ClientSession() as s: - summary = self.monsters[monster].get('summary') \ - or (await fetch_url(s, summary_url)).get('extract') - - image = self.monsters[monster].get('image') \ - or (await fetch_url(s, image_url)).get('original', {}).get('source') - return summary, image - - @commands.group(name='monster', aliases=('ms',), invoke_without_command=True) + def __init__(self, bot: Bot): + self.bot = bot + self.registry_location = os.path.join(os.getcwd(), 'resources', 'monstersurvey', 'monstersurvey.json') + with open(self.registry_location, 'r') as jason: + self.voter_registry = json.load(jason) + + @commands.group( + name='monster', + aliases=['ms'] + ) async def monster_group(self, ctx: Context): - pass - - @monster_group.command(name='vote') + if ctx.invoked_subcommand is None: + default_embed = Embed( + title='Monster Voting', + color=0xFF6800, + description='Vote for your favorite monster!' + ) + default_embed.add_field( + name='.monster show monster_name(optional)', + value='Show a specific monster. If none is listed, show a brief of all.', + inline=False) + default_embed.add_field( + name='.monster vote monster_name', + value='Vote for a specific monster. You can vote more than once, but you can only vote for one monster' + 'at a time.', + inline=False + ) + default_embed.add_field( + name='.monster leaderboard', + value='Which monster has the most votes? This command will tell you.', + inline=False + ) + await ctx.send(embed=default_embed) + + @monster_group.command( + name='vote' + ) async def monster_vote(self, ctx: Context, name: Optional[str] = None): - """Casts a vote for a particular monster, or displays a list of all - monsters that are available for voting. - """ - 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) - # TODO: Add logic for voting - await ctx.send(embed=embed) - - @monster_group.command(name='show') - async def monster_show(self, ctx: Context, *monster: str): - """Display a detailed description for the monster, and image.""" - monster = ' '.join(monster) - if self.monsters.get(monster, None): - summary, image = await self.monster_info(monster) - embed = discord.Embed(name=monster) - embed.add_field(name=f"About {monster}", value=summary or "No information") - if image: - embed.set_image(url=image) - embed.set_footer(text=f"To vote for {monster}, type `.monster vote {monster}`") - await ctx.send(embed=embed) - else: - await ctx.message.add_reaction(EMOJIS['ERROR']) - - -def setup(bot: Bot): - bot.add_cog(MonsterSurvey(bot)) + """Casts a vote for a particular monster, or displays a list of monsters that can be voted for + if one is not given.""" + vote_embed = Embed( + name='Monster Voting', + color=0xFF6800 + ) + if name not in self.voter_registry.keys() and name is not None: + vote_embed.description = f'You cannot vote for {name} because it\'s not in the running.' + vote_embed.add_field( + name='Use `.monster show {monster_name} for more information on a specific monster', + value='or use `.monster vote {monster}` to cast your vote for said monster.', + inline=False + ) + vote_embed.add_field( + name='You may vote for the following monsters:', + value=f"{', '.join(self.voter_registry.keys())}" + ) + return await ctx.send(embed=vote_embed) + if name is None: + pass + @monster_group.command(name='show') + async def monster_show(self, ctx: Context, name: str): + m = self.voter_registry.get(name) + if not m: + # TODO: invoke .monster vote command to display list + raise commands.BadArgument("Monster does not exist.") + + embed = Embed(title=m['full_name'], color=0xFF6800) + embed.add_field(name='Summary', value=m['summary']) + embed.set_image(url=m['image']) + embed.set_footer(text=f'To vote for this monster, type .monster vote {name}') + await ctx.send(embed=embed) + @monster_group.command(name='leaderboard') + async def monster_leaderboard(self, ctx: Context): + vr = self.voter_registry + top = sorted(vr.values(), key=lambda k: len(k['votes']), reverse=True) + + embed = Embed(title="Leader board", color=0xFF6800) + total_votes = sum(len(m['votes']) for m in self.voter_registry.values()) + for rank, m in enumerate(top): + votes = len(m['votes']) + percentage = ((votes / total_votes) * 100) if total_votes > 0 else 0 + embed.add_field(name=f"{rank+1}. {m['full_name']}", + value=f"{votes} votes. {percentage:.1f}%" + f" of total votes.", inline=False) + await ctx.send(embed=embed) +def setup(bot): + bot.add_cog(MonsterServey(bot)) diff --git a/bot/resources/monstersurvey/monstersurvey.json b/bot/resources/monstersurvey/monstersurvey.json index 7e923dcc..a98870c3 100644 --- a/bot/resources/monstersurvey/monstersurvey.json +++ b/bot/resources/monstersurvey/monstersurvey.json @@ -1,8 +1,32 @@ { - "Frankenstein's Monster": { - "votes": [] - }, - "Count Dracula": { + "frankenstein": { + "full_name": "Frankenstein's Monster", + "summary": "His limbs were in proportion, and I had selected his features as beautiful. Beautiful! Great God! His yellow skin scarcely covered the work of muscles and arteries beneath; his hair was of a lustrous black, and flowing; his teeth of a pearly whiteness; but these luxuriances only formed a more horrid contrast with his watery eyes, that seemed almost of the same colour as the dun-white sockets in which they were set, his shrivelled complexion and straight black lips.", + "image": "https://upload.wikimedia.org/wikipedia/commons/a/a7/Frankenstein%27s_monster_%28Boris_Karloff%29.jpg", + "votes": [] + }, + "dracula": { + "full_name": "Count Dracula", + "summary": "Count Dracula is an undead, centuries-old vampire, and a Transylvanian nobleman who claims to be a Székely descended from Attila the Hun. He inhabits a decaying castle in the Carpathian Mountains near the Borgo Pass. Unlike the vampires of Eastern European folklore, which are portrayed as repulsive, corpse-like creatures, Dracula wears a veneer of aristocratic charm. In his conversations with Jonathan Harker, he reveals himself as deeply proud of his boyar heritage and nostalgic for the past, which he admits have become only a memory of heroism, honour and valour in modern times.", + "image": "https://upload.wikimedia.org/wikipedia/commons/thumb/9/90/Bela_Lugosi_as_Dracula%2C_anonymous_photograph_from_1931%2C_Universal_Studios.jpg/250px-Bela_Lugosi_as_Dracula%2C_anonymous_photograph_from_1931%2C_Universal_Studios.jpg", + "votes": [] + }, + "blob": { + "full_name": "The Blob", + "summary": "", + "image": "", + "votes": [] + }, + "goofy": { + "full_name": "Goofy in the Monster's INC World", + "summary": "Pure nightmare fuel.\nThis monster is nothing like its original counterpart. With two different eyes, a pointed nose, fins growing out of its blue skin, and dark spots covering his body, he's a true nightmare come to life.", + "image": "https://www.dailydot.com/wp-content/uploads/3a2/a8/bf38aedbef9f795f.png", + "votes": [] + }, + "refisio": { + "full_name": "Refisio", + "summary": "Who let this guy write this? That's who the real monster is.", + "image": "https://avatars0.githubusercontent.com/u/24819750?s=460&v=4", "votes": [] } } -- cgit v1.2.3 From abc481b13a419537b5ee462d1ceb718d1df87503 Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 12 Oct 2018 16:45:33 -0400 Subject: Finished MonsterSurvey cog. Added docstrings to all commands, refactored a tiny bit of code within the two commands that you wrote. --- bot/cogs/monstersurvey.py | 92 ++++++++++++++++++++++++++++++++++++----------- 1 file changed, 72 insertions(+), 20 deletions(-) (limited to 'bot/cogs/monstersurvey.py') diff --git a/bot/cogs/monstersurvey.py b/bot/cogs/monstersurvey.py index 67bc9051..289e56cd 100644 --- a/bot/cogs/monstersurvey.py +++ b/bot/cogs/monstersurvey.py @@ -1,8 +1,9 @@ from discord.ext.commands import Bot, Context -from typing import Optional, Tuple +from asyncio import sleep as asleep +from typing import Optional from discord.ext import commands from discord import Embed -import discord, logging, json, os +import logging, json, os log = logging.getLogger(__name__) @@ -12,19 +13,33 @@ EMOJIS = { } -class MonsterServey: +class MonsterSurvey: + """ + Vote for your favorite monster! + This command allows users to vote for their favorite listed monster. + Users may change their vote, but only their current vote will be counted. + """ def __init__(self, bot: Bot): + """Initializes values for the bot to use within the voting commands.""" self.bot = bot self.registry_location = os.path.join(os.getcwd(), 'resources', 'monstersurvey', 'monstersurvey.json') with open(self.registry_location, 'r') as jason: self.voter_registry = json.load(jason) + def json_write(self): + with open(self.registry_location, 'w') as jason: + json.dump(self.voter_registry, jason, indent=2) + @commands.group( name='monster', aliases=['ms'] ) async def monster_group(self, ctx: Context): + """ + The base voting command. If nothing is called, then it will return an embed. + """ + if ctx.invoked_subcommand is None: default_embed = Embed( title='Monster Voting', @@ -46,7 +61,7 @@ class MonsterServey: value='Which monster has the most votes? This command will tell you.', inline=False ) - await ctx.send(embed=default_embed) + return await ctx.send(embed=default_embed) @monster_group.command( name='vote' @@ -58,42 +73,78 @@ class MonsterServey: name='Monster Voting', color=0xFF6800 ) - if name not in self.voter_registry.keys() and name is not None: - vote_embed.description = f'You cannot vote for {name} because it\'s not in the running.' + if name is None or name.lower() not in self.voter_registry.keys(): + if name is not None: + vote_embed.description = f'You cannot vote for {name} because it\'s not in the running.' vote_embed.add_field( - name='Use `.monster show {monster_name} for more information on a specific monster', + name='Use `.monster show {monster_name}` for more information on a specific monster', value='or use `.monster vote {monster}` to cast your vote for said monster.', inline=False ) vote_embed.add_field( - name='You may vote for the following monsters:', + name='You may vote for or show the following monsters:', value=f"{', '.join(self.voter_registry.keys())}" ) return await ctx.send(embed=vote_embed) - if name is None: - pass - - + for monster in self.voter_registry.keys(): + if ctx.author.id in self.voter_registry[monster]['votes']: + if name != monster: + self.voter_registry[monster]['votes'].remove(ctx.author.id) + break + else: + vote_embed.add_field( + name='Vote unsuccessful.', + value='You already voted for this monster. ' + 'If you want to change your vote, use another monster.', + inline=False + ) + await ctx.send(embed=vote_embed) + await asleep(.5) + return await ctx.invoke(self.monster_vote) + self.voter_registry[name]['votes'].append(ctx.author.id) + vote_embed.add_field( + name='Vote successful!', + value=f'You have successfully voted for {self.voter_registry[name]["full_name"]}!', + inline=False + ) + vote_embed.set_thumbnail(url=self.voter_registry[name]['image']) + self.json_write() + return await ctx.send(embed=vote_embed) - @monster_group.command(name='show') + @monster_group.command( + name='show' + ) async def monster_show(self, ctx: Context, name: str): + """ + Shows the named monster. If one is not named, it sends the default voting embed instead. + :param ctx: + :param name: + :return: + """ m = self.voter_registry.get(name) if not m: - # TODO: invoke .monster vote command to display list - raise commands.BadArgument("Monster does not exist.") - + await ctx.send('That monster does not exist.') + return await ctx.invoke(self.monster_vote) embed = Embed(title=m['full_name'], color=0xFF6800) embed.add_field(name='Summary', value=m['summary']) embed.set_image(url=m['image']) embed.set_footer(text=f'To vote for this monster, type .monster vote {name}') - await ctx.send(embed=embed) + return await ctx.send(embed=embed) - @monster_group.command(name='leaderboard') + @monster_group.command( + name='leaderboard', + aliases=['lb'] + ) async def monster_leaderboard(self, ctx: Context): + """ + Shows the current standings. + :param ctx: + :return: + """ vr = self.voter_registry top = sorted(vr.values(), key=lambda k: len(k['votes']), reverse=True) - embed = Embed(title="Leader board", color=0xFF6800) + embed = Embed(title="Monster Survey Leader Board", color=0xFF6800) total_votes = sum(len(m['votes']) for m in self.voter_registry.values()) for rank, m in enumerate(top): votes = len(m['votes']) @@ -103,5 +154,6 @@ class MonsterServey: f" of total votes.", inline=False) await ctx.send(embed=embed) + def setup(bot): - bot.add_cog(MonsterServey(bot)) + bot.add_cog(MonsterSurvey(bot)) -- cgit v1.2.3 From 2e71ff8fe3f7482de50b01c917640f21f62a40fe Mon Sep 17 00:00:00 2001 From: Derek Fitzpatrick Date: Fri, 12 Oct 2018 15:38:48 -0700 Subject: Fixed linting: Multiple imports on single line. --- Pipfile | 1 + bot/cogs/monstersurvey.py | 4 +++- bot/resources/monstersurvey/monstersurvey.json | 8 +++++--- 3 files changed, 9 insertions(+), 4 deletions(-) (limited to 'bot/cogs/monstersurvey.py') diff --git a/Pipfile b/Pipfile index a702616f..158d2787 100644 --- a/Pipfile +++ b/Pipfile @@ -5,6 +5,7 @@ name = "pypi" [packages] "discord.py" = {ref = "rewrite", git = "https://github.com/Rapptz/discord.py"} +"flake8" = "*" [dev-packages] "flake8" = "*" diff --git a/bot/cogs/monstersurvey.py b/bot/cogs/monstersurvey.py index 289e56cd..dd19d401 100644 --- a/bot/cogs/monstersurvey.py +++ b/bot/cogs/monstersurvey.py @@ -3,7 +3,9 @@ from asyncio import sleep as asleep from typing import Optional from discord.ext import commands from discord import Embed -import logging, json, os +import logging +import json +import os log = logging.getLogger(__name__) diff --git a/bot/resources/monstersurvey/monstersurvey.json b/bot/resources/monstersurvey/monstersurvey.json index a98870c3..016bda3e 100644 --- a/bot/resources/monstersurvey/monstersurvey.json +++ b/bot/resources/monstersurvey/monstersurvey.json @@ -7,9 +7,11 @@ }, "dracula": { "full_name": "Count Dracula", - "summary": "Count Dracula is an undead, centuries-old vampire, and a Transylvanian nobleman who claims to be a Székely descended from Attila the Hun. He inhabits a decaying castle in the Carpathian Mountains near the Borgo Pass. Unlike the vampires of Eastern European folklore, which are portrayed as repulsive, corpse-like creatures, Dracula wears a veneer of aristocratic charm. In his conversations with Jonathan Harker, he reveals himself as deeply proud of his boyar heritage and nostalgic for the past, which he admits have become only a memory of heroism, honour and valour in modern times.", + "summary": "Count Dracula is an undead, centuries-old vampire, and a Transylvanian nobleman who claims to be a Sz\u00c3\u00a9kely descended from Attila the Hun. He inhabits a decaying castle in the Carpathian Mountains near the Borgo Pass. Unlike the vampires of Eastern European folklore, which are portrayed as repulsive, corpse-like creatures, Dracula wears a veneer of aristocratic charm. In his conversations with Jonathan Harker, he reveals himself as deeply proud of his boyar heritage and nostalgic for the past, which he admits have become only a memory of heroism, honour and valour in modern times.", "image": "https://upload.wikimedia.org/wikipedia/commons/thumb/9/90/Bela_Lugosi_as_Dracula%2C_anonymous_photograph_from_1931%2C_Universal_Studios.jpg/250px-Bela_Lugosi_as_Dracula%2C_anonymous_photograph_from_1931%2C_Universal_Studios.jpg", - "votes": [] + "votes": [ + 224734305581137921 + ] }, "blob": { "full_name": "The Blob", @@ -29,4 +31,4 @@ "image": "https://avatars0.githubusercontent.com/u/24819750?s=460&v=4", "votes": [] } -} +} \ No newline at end of file -- cgit v1.2.3 From f2a5a5f727b5fd2dfe8e34aa5d5cc9dd06511105 Mon Sep 17 00:00:00 2001 From: Derek Fitzpatrick Date: Wed, 31 Oct 2018 02:37:39 -0700 Subject: Resolved feedback from PR. Resolved lint extra spacing. --- bot/cogs/monstersurvey.py | 45 ++++++++++++++++++-------- bot/resources/monstersurvey/monstersurvey.json | 8 +---- 2 files changed, 32 insertions(+), 21 deletions(-) (limited to 'bot/cogs/monstersurvey.py') diff --git a/bot/cogs/monstersurvey.py b/bot/cogs/monstersurvey.py index dd19d401..e59caeee 100644 --- a/bot/cogs/monstersurvey.py +++ b/bot/cogs/monstersurvey.py @@ -1,11 +1,12 @@ -from discord.ext.commands import Bot, Context -from asyncio import sleep as asleep -from typing import Optional -from discord.ext import commands -from discord import Embed -import logging import json +import logging import os +from asyncio import sleep as asleep +from typing import Optional, Union + +from discord import Embed +from discord.ext import commands +from discord.ext.commands import Bot, Context log = logging.getLogger(__name__) @@ -25,11 +26,12 @@ class MonsterSurvey: def __init__(self, bot: Bot): """Initializes values for the bot to use within the voting commands.""" self.bot = bot - self.registry_location = os.path.join(os.getcwd(), 'resources', 'monstersurvey', 'monstersurvey.json') + self.registry_location = os.path.join(os.getcwd(), 'bot', 'resources', 'monstersurvey', 'monstersurvey.json') with open(self.registry_location, 'r') as jason: self.voter_registry = json.load(jason) def json_write(self): + log.info("Saved Monster Survey Results") with open(self.registry_location, 'w') as jason: json.dump(self.voter_registry, jason, indent=2) @@ -50,12 +52,11 @@ class MonsterSurvey: ) default_embed.add_field( name='.monster show monster_name(optional)', - value='Show a specific monster. If none is listed, show a brief of all.', + value='Show a specific monster. If none is listed, it will give you an error with valid choices.', inline=False) default_embed.add_field( name='.monster vote monster_name', - value='Vote for a specific monster. You can vote more than once, but you can only vote for one monster' - 'at a time.', + value='Vote for a specific monster. You get one vote, but can change it at any time.', inline=False ) default_embed.add_field( @@ -68,13 +69,16 @@ class MonsterSurvey: @monster_group.command( name='vote' ) - async def monster_vote(self, ctx: Context, name: Optional[str] = None): + async def monster_vote(self, ctx: Context, name: Optional[Union[int, str]] = None): """Casts a vote for a particular monster, or displays a list of monsters that can be voted for if one is not given.""" vote_embed = Embed( name='Monster Voting', color=0xFF6800 ) + if isinstance(name, int): + name = list(self.voter_registry.keys())[name] + if name is None or name.lower() not in self.voter_registry.keys(): if name is not None: vote_embed.description = f'You cannot vote for {name} because it\'s not in the running.' @@ -90,7 +94,7 @@ class MonsterSurvey: return await ctx.send(embed=vote_embed) for monster in self.voter_registry.keys(): if ctx.author.id in self.voter_registry[monster]['votes']: - if name != monster: + if name.lower() != monster: self.voter_registry[monster]['votes'].remove(ctx.author.id) break else: @@ -100,6 +104,7 @@ class MonsterSurvey: 'If you want to change your vote, use another monster.', inline=False ) + log.info(f"{ctx.author.name} Tried to vote for the same monster.") await ctx.send(embed=vote_embed) await asleep(.5) return await ctx.invoke(self.monster_vote) @@ -116,14 +121,25 @@ class MonsterSurvey: @monster_group.command( name='show' ) - async def monster_show(self, ctx: Context, name: str): + async def monster_show(self, ctx: Context, name: Optional[Union[int, str]] = None): """ Shows the named monster. If one is not named, it sends the default voting embed instead. :param ctx: :param name: :return: """ - m = self.voter_registry.get(name) + if name is None: + monster_choices = map(lambda m: f"'{m}'", self.voter_registry.keys()) + monster_choices = ', '.join(monster_choices) + embed = Embed(title="Uh Oh!", + description="I need you to provide a name for your" + f" monster. Choose from {monster_choices}") + await ctx.send(embed=embed) + return + if isinstance(name, int): + m = list(self.voter_registry.values())[name] + else: + m = self.voter_registry.get(name.lower()) if not m: await ctx.send('That monster does not exist.') return await ctx.invoke(self.monster_vote) @@ -159,3 +175,4 @@ class MonsterSurvey: def setup(bot): bot.add_cog(MonsterSurvey(bot)) + log.debug("MonsterSurvey COG Loaded") diff --git a/bot/resources/monstersurvey/monstersurvey.json b/bot/resources/monstersurvey/monstersurvey.json index 016bda3e..6c62e2c1 100644 --- a/bot/resources/monstersurvey/monstersurvey.json +++ b/bot/resources/monstersurvey/monstersurvey.json @@ -13,12 +13,6 @@ 224734305581137921 ] }, - "blob": { - "full_name": "The Blob", - "summary": "", - "image": "", - "votes": [] - }, "goofy": { "full_name": "Goofy in the Monster's INC World", "summary": "Pure nightmare fuel.\nThis monster is nothing like its original counterpart. With two different eyes, a pointed nose, fins growing out of its blue skin, and dark spots covering his body, he's a true nightmare come to life.", @@ -31,4 +25,4 @@ "image": "https://avatars0.githubusercontent.com/u/24819750?s=460&v=4", "votes": [] } -} \ No newline at end of file +} -- cgit v1.2.3 From b25491bb4f45628460964a43b02b39714e995168 Mon Sep 17 00:00:00 2001 From: Derek Fitzpatrick Date: Wed, 31 Oct 2018 22:29:08 -0700 Subject: Resolved PR comments. --- bot/cogs/monstersurvey.py | 88 +++++++++++++++----------- bot/resources/monstersurvey/monstersurvey.json | 2 +- 2 files changed, 52 insertions(+), 38 deletions(-) (limited to 'bot/cogs/monstersurvey.py') diff --git a/bot/cogs/monstersurvey.py b/bot/cogs/monstersurvey.py index e59caeee..9eafcadc 100644 --- a/bot/cogs/monstersurvey.py +++ b/bot/cogs/monstersurvey.py @@ -1,7 +1,6 @@ import json import logging import os -from asyncio import sleep as asleep from typing import Optional, Union from discord import Embed @@ -35,6 +34,28 @@ class MonsterSurvey: with open(self.registry_location, 'w') as jason: json.dump(self.voter_registry, jason, indent=2) + def cast_vote(self, id: int, monster: str): + """ + + :param id: The id of the person voting + :param monster: the string key of the json that represents a monster + :return: None + """ + vr = self.voter_registry + for m in vr.keys(): + if id not in vr[m]['votes'] and m == monster: + vr[m]['votes'].append(id) + else: + if id in vr[m]['votes'] and m != monster: + vr[m]['votes'].remove(id) + + def get_name_by_leaderboard_index(self, n): + n = n - 1 + vr = self.voter_registry + top = sorted(vr, key=lambda k: len(vr[k]['votes']), reverse=True) + name = top[n] if n >= 0 else None + return name + @commands.group( name='monster', aliases=['ms'] @@ -64,6 +85,7 @@ class MonsterSurvey: value='Which monster has the most votes? This command will tell you.', inline=False ) + default_embed.set_footer(text=f"Monsters choices are: {', '.join(self.voter_registry.keys())}") return await ctx.send(embed=default_embed) @monster_group.command( @@ -72,16 +94,21 @@ class MonsterSurvey: async def monster_vote(self, ctx: Context, name: Optional[Union[int, str]] = None): """Casts a vote for a particular monster, or displays a list of monsters that can be voted for if one is not given.""" + if name is None: + await ctx.invoke(self.monster_leaderboard) + return vote_embed = Embed( name='Monster Voting', color=0xFF6800 ) if isinstance(name, int): - name = list(self.voter_registry.keys())[name] + name = self.get_name_by_leaderboard_index(name) + else: + name = name.lower() + m = self.voter_registry.get(name) + if m is None: - if name is None or name.lower() not in self.voter_registry.keys(): - if name is not None: - vote_embed.description = f'You cannot vote for {name} because it\'s not in the running.' + vote_embed.description = f'You cannot vote for {name} because it\'s not in the running.' vote_embed.add_field( name='Use `.monster show {monster_name}` for more information on a specific monster', value='or use `.monster vote {monster}` to cast your vote for said monster.', @@ -92,29 +119,14 @@ class MonsterSurvey: value=f"{', '.join(self.voter_registry.keys())}" ) return await ctx.send(embed=vote_embed) - for monster in self.voter_registry.keys(): - if ctx.author.id in self.voter_registry[monster]['votes']: - if name.lower() != monster: - self.voter_registry[monster]['votes'].remove(ctx.author.id) - break - else: - vote_embed.add_field( - name='Vote unsuccessful.', - value='You already voted for this monster. ' - 'If you want to change your vote, use another monster.', - inline=False - ) - log.info(f"{ctx.author.name} Tried to vote for the same monster.") - await ctx.send(embed=vote_embed) - await asleep(.5) - return await ctx.invoke(self.monster_vote) - self.voter_registry[name]['votes'].append(ctx.author.id) + self.cast_vote(ctx.author.id, name) vote_embed.add_field( name='Vote successful!', - value=f'You have successfully voted for {self.voter_registry[name]["full_name"]}!', + value=f'You have successfully voted for {m["full_name"]}!', inline=False ) - vote_embed.set_thumbnail(url=self.voter_registry[name]['image']) + vote_embed.set_thumbnail(url=m['image']) + vote_embed.set_footer(text="Please note that any previous votes have been removed.") self.json_write() return await ctx.send(embed=vote_embed) @@ -129,17 +141,13 @@ class MonsterSurvey: :return: """ if name is None: - monster_choices = map(lambda m: f"'{m}'", self.voter_registry.keys()) - monster_choices = ', '.join(monster_choices) - embed = Embed(title="Uh Oh!", - description="I need you to provide a name for your" - f" monster. Choose from {monster_choices}") - await ctx.send(embed=embed) + await ctx.invoke(self.monster_leaderboard) return if isinstance(name, int): - m = list(self.voter_registry.values())[name] + m = self.voter_registry.get(self.get_name_by_leaderboard_index(name)) else: - m = self.voter_registry.get(name.lower()) + name = name.lower() + m = self.voter_registry.get(name) if not m: await ctx.send('That monster does not exist.') return await ctx.invoke(self.monster_vote) @@ -160,16 +168,22 @@ class MonsterSurvey: :return: """ vr = self.voter_registry - top = sorted(vr.values(), key=lambda k: len(k['votes']), reverse=True) + top = sorted(vr, key=lambda k: len(vr[k]['votes']), reverse=True) embed = Embed(title="Monster Survey Leader Board", color=0xFF6800) total_votes = sum(len(m['votes']) for m in self.voter_registry.values()) for rank, m in enumerate(top): - votes = len(m['votes']) + votes = len(vr[m]['votes']) percentage = ((votes / total_votes) * 100) if total_votes > 0 else 0 - embed.add_field(name=f"{rank+1}. {m['full_name']}", - value=f"{votes} votes. {percentage:.1f}%" - f" of total votes.", inline=False) + embed.add_field(name=f"{rank+1}. {vr[m]['full_name']}", + value=f"{votes} votes. {percentage:.1f}% of total votes.\n" + f"Vote for this monster by typing " + f"'.monster vote {m}'\n" + f"Get more information on this monster by typing " + f"'.monster show {m}'", + inline=False) + + embed.set_footer(text="You can also vote by their rank number. '.monster vote {number}' ") await ctx.send(embed=embed) diff --git a/bot/resources/monstersurvey/monstersurvey.json b/bot/resources/monstersurvey/monstersurvey.json index 6c62e2c1..209f1cbb 100644 --- a/bot/resources/monstersurvey/monstersurvey.json +++ b/bot/resources/monstersurvey/monstersurvey.json @@ -25,4 +25,4 @@ "image": "https://avatars0.githubusercontent.com/u/24819750?s=460&v=4", "votes": [] } -} +} \ No newline at end of file -- cgit v1.2.3