diff options
Diffstat (limited to 'bot/cogs')
-rw-r--r-- | bot/cogs/__init__.py | 0 | ||||
-rw-r--r-- | bot/cogs/evergreen/__init__.py | 0 | ||||
-rw-r--r-- | bot/cogs/evergreen/uptime.py | 33 | ||||
-rw-r--r-- | bot/cogs/hacktober/__init__.py | 0 | ||||
-rw-r--r-- | bot/cogs/hacktober/hacktoberstats.py (renamed from bot/cogs/hacktoberstats.py) | 8 | ||||
-rw-r--r-- | bot/cogs/hacktober/halloween_facts.py (renamed from bot/cogs/halloween_facts.py) | 0 | ||||
-rw-r--r-- | bot/cogs/hacktober/halloweenify.py (renamed from bot/cogs/halloweenify.py) | 0 | ||||
-rw-r--r-- | bot/cogs/hacktober/monstersurvey.py | 192 | ||||
-rw-r--r-- | bot/cogs/hacktober/movie.py (renamed from bot/cogs/movie.py) | 0 | ||||
-rw-r--r-- | bot/cogs/hacktober/spookyreact.py (renamed from bot/cogs/spookyreact.py) | 4 | ||||
-rw-r--r-- | bot/cogs/template.py | 4 |
11 files changed, 233 insertions, 8 deletions
diff --git a/bot/cogs/__init__.py b/bot/cogs/__init__.py new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/bot/cogs/__init__.py diff --git a/bot/cogs/evergreen/__init__.py b/bot/cogs/evergreen/__init__.py new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/bot/cogs/evergreen/__init__.py diff --git a/bot/cogs/evergreen/uptime.py b/bot/cogs/evergreen/uptime.py new file mode 100644 index 00000000..ec4a3083 --- /dev/null +++ b/bot/cogs/evergreen/uptime.py @@ -0,0 +1,33 @@ +import arrow +from dateutil.relativedelta import relativedelta +from discord.ext import commands + +from bot import start_time + + +class Uptime: + """ + A cog for posting the bots uptime. + """ + + def __init__(self, bot): + self.bot = bot + + @commands.command(name='uptime') + async def uptime(self, ctx): + """ + Returns the uptime of the bot. + """ + difference = relativedelta(start_time - arrow.utcnow()) + uptime_string = start_time.shift( + seconds=-difference.seconds, + minutes=-difference.minutes, + hours=-difference.hours, + days=-difference.days + ).humanize() + await ctx.send(f"I started up {uptime_string}.") + + +# Required in order to load the cog, use the class name in the add_cog function. +def setup(bot): + bot.add_cog(Uptime(bot)) diff --git a/bot/cogs/hacktober/__init__.py b/bot/cogs/hacktober/__init__.py new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/bot/cogs/hacktober/__init__.py diff --git a/bot/cogs/hacktoberstats.py b/bot/cogs/hacktober/hacktoberstats.py index 536307cc..0755503c 100644 --- a/bot/cogs/hacktoberstats.py +++ b/bot/cogs/hacktober/hacktoberstats.py @@ -202,7 +202,7 @@ class Stats: For each PR: { "repo_url": str - "repo_shortname": str (e.g. "discord-python/hacktoberbot") + "repo_shortname": str (e.g. "python-discord/seasonalbot") "created_at": datetime.datetime } @@ -259,12 +259,12 @@ class Stats: """ Extract shortname from https://api.github.com/repos/* URL - e.g. "https://api.github.com/repos/discord-python/hacktoberbot" + e.g. "https://api.github.com/repos/python-discord/seasonalbot" | V - "discord-python/hacktoberbot" + "python-discord/seasonalbot" """ - exp = r"https?:\/\/api.github.com\/repos\/([/\-\w]+)" + exp = r"https?:\/\/api.github.com\/repos\/([/\-\_\.\w]+)" return re.findall(exp, in_url)[0] @staticmethod diff --git a/bot/cogs/halloween_facts.py b/bot/cogs/hacktober/halloween_facts.py index e97c80d2..e97c80d2 100644 --- a/bot/cogs/halloween_facts.py +++ b/bot/cogs/hacktober/halloween_facts.py diff --git a/bot/cogs/halloweenify.py b/bot/cogs/hacktober/halloweenify.py index a5fe45ef..a5fe45ef 100644 --- a/bot/cogs/halloweenify.py +++ b/bot/cogs/hacktober/halloweenify.py diff --git a/bot/cogs/hacktober/monstersurvey.py b/bot/cogs/hacktober/monstersurvey.py new file mode 100644 index 00000000..376ac1fa --- /dev/null +++ b/bot/cogs/hacktober/monstersurvey.py @@ -0,0 +1,192 @@ +import json +import logging +import os +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__) + +EMOJIS = { + 'SUCCESS': u'\u2705', + 'ERROR': u'\u274C' +} + + +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(), 'bot', 'resources', 'halloween', '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) + + 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'] + ) + 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', + 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, 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 get one vote, but can change it at any time.', + inline=False + ) + default_embed.add_field( + name='.monster leaderboard', + 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( + name='vote' + ) + 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 = self.get_name_by_leaderboard_index(name) + else: + name = name.lower() + m = self.voter_registry.get(name) + if m is 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 or show the following monsters:', + value=f"{', '.join(self.voter_registry.keys())}" + ) + return await ctx.send(embed=vote_embed) + self.cast_vote(ctx.author.id, name) + vote_embed.add_field( + name='Vote successful!', + value=f'You have successfully voted for {m["full_name"]}!', + inline=False + ) + 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) + + @monster_group.command( + name='show' + ) + 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: + """ + if name is None: + await ctx.invoke(self.monster_leaderboard) + return + if isinstance(name, int): + m = self.voter_registry.get(self.get_name_by_leaderboard_index(name)) + else: + 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) + 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}') + return await ctx.send(embed=embed) + + @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, 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(vr[m]['votes']) + percentage = ((votes / total_votes) * 100) if total_votes > 0 else 0 + 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) + + +def setup(bot): + bot.add_cog(MonsterSurvey(bot)) + log.debug("MonsterSurvey COG Loaded") diff --git a/bot/cogs/movie.py b/bot/cogs/hacktober/movie.py index 925f813f..925f813f 100644 --- a/bot/cogs/movie.py +++ b/bot/cogs/hacktober/movie.py diff --git a/bot/cogs/spookyreact.py b/bot/cogs/hacktober/spookyreact.py index 9146b797..8e9e8db6 100644 --- a/bot/cogs/spookyreact.py +++ b/bot/cogs/hacktober/spookyreact.py @@ -25,11 +25,11 @@ class SpookyReact: async def on_message(self, ctx: discord.Message): """ - A command to send the hacktoberbot github project + A command to send the seasonalbot github project Lines that begin with the bot's command prefix are ignored - Hacktoberbot's own messages are ignored + Seasonalbot's own messages are ignored """ for trigger in SPOOKY_TRIGGERS.keys(): trigger_test = re.search(SPOOKY_TRIGGERS[trigger][0], ctx.content.lower()) diff --git a/bot/cogs/template.py b/bot/cogs/template.py index aa01432c..e1b646e3 100644 --- a/bot/cogs/template.py +++ b/bot/cogs/template.py @@ -13,9 +13,9 @@ class Template: @commands.command(name='repo', aliases=['repository', 'project'], brief='A link to the repository of this bot.') async def repository(self, ctx): """ - A command to send the hacktoberbot github project + A command to send the seasonalbot github project """ - await ctx.send('https://github.com/discord-python/hacktoberbot') + await ctx.send('https://github.com/python-discord/seasonalbot') @commands.group(name='git', invoke_without_command=True, brief="A link to resources for learning Git") async def github(self, ctx): |