From dcd3bc3f70988e819de6b0e0f70a600029e1ebfb Mon Sep 17 00:00:00 2001 From: Chris Lovering Date: Tue, 10 Aug 2021 14:13:31 +0100 Subject: Add February in_month check to love calculator command This ensure the valentines themed command can only be ran during the valentines season. --- bot/exts/valentines/lovecalculator.py | 3 +++ 1 file changed, 3 insertions(+) (limited to 'bot') diff --git a/bot/exts/valentines/lovecalculator.py b/bot/exts/valentines/lovecalculator.py index b10b7bca..41ba25ae 100644 --- a/bot/exts/valentines/lovecalculator.py +++ b/bot/exts/valentines/lovecalculator.py @@ -12,6 +12,8 @@ from discord.ext import commands from discord.ext.commands import BadArgument, Cog, clean_content from bot.bot import Bot +from bot.constants import Month +from bot.utils.decorators import in_month log = logging.getLogger(__name__) @@ -22,6 +24,7 @@ LOVE_DATA = sorted((int(key), value) for key, value in LOVE_DATA.items()) class LoveCalculator(Cog): """A cog for calculating the love between two people.""" + @in_month(Month.FEBRUARY) @commands.command(aliases=("love_calculator", "love_calc")) @commands.cooldown(rate=1, per=5, type=commands.BucketType.user) async def love(self, ctx: commands.Context, who: Union[Member, str], whom: Union[Member, str] = None) -> None: -- cgit v1.2.3 From cd904229b7150d2db26fda9aba5a51abf3f95c58 Mon Sep 17 00:00:00 2001 From: Chris Lovering Date: Tue, 10 Aug 2021 14:15:07 +0100 Subject: Require both members used in love calculator have opt-ed into lovefest This ensure that only uses that have opted into the love fest event can actually be used as part of this command. --- bot/exts/valentines/lovecalculator.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) (limited to 'bot') diff --git a/bot/exts/valentines/lovecalculator.py b/bot/exts/valentines/lovecalculator.py index 41ba25ae..ba5e0b92 100644 --- a/bot/exts/valentines/lovecalculator.py +++ b/bot/exts/valentines/lovecalculator.py @@ -4,7 +4,7 @@ import json import logging import random from pathlib import Path -from typing import Coroutine, Union +from typing import Coroutine, Optional, Union import discord from discord import Member @@ -12,7 +12,7 @@ from discord.ext import commands from discord.ext.commands import BadArgument, Cog, clean_content from bot.bot import Bot -from bot.constants import Month +from bot.constants import Lovefest, Month from bot.utils.decorators import in_month log = logging.getLogger(__name__) @@ -27,7 +27,7 @@ class LoveCalculator(Cog): @in_month(Month.FEBRUARY) @commands.command(aliases=("love_calculator", "love_calc")) @commands.cooldown(rate=1, per=5, type=commands.BucketType.user) - async def love(self, ctx: commands.Context, who: Union[Member, str], whom: Union[Member, str] = None) -> None: + async def love(self, ctx: commands.Context, who: Member, whom: Optional[Member] = None) -> None: """ Tells you how much the two love each other. @@ -49,6 +49,12 @@ class LoveCalculator(Cog): if whom is None: whom = ctx.author + if not all(( + Lovefest.role_id in [role.id for role in who.roles], + Lovefest.role_id in [role.id for role in whom.roles] + )): + raise BadArgument("Both members must have the love fest role!") + def normalize(arg: Union[Member, str]) -> Coroutine: if isinstance(arg, Member): # If we are given a member, return name#discrim without any extra changes -- cgit v1.2.3 From c334c20c03d4f3961c9b2628660efaae405e5d9c Mon Sep 17 00:00:00 2001 From: Chris Lovering Date: Tue, 10 Aug 2021 14:15:51 +0100 Subject: Sort the members before calculating a love score This ensures the same result for same input, regardless of order --- bot/exts/valentines/lovecalculator.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'bot') diff --git a/bot/exts/valentines/lovecalculator.py b/bot/exts/valentines/lovecalculator.py index ba5e0b92..d06d883d 100644 --- a/bot/exts/valentines/lovecalculator.py +++ b/bot/exts/valentines/lovecalculator.py @@ -65,7 +65,8 @@ class LoveCalculator(Cog): # This has to be done manually to be applied to usernames return clean_content(escape_markdown=True).convert(ctx, arg) - who, whom = [await normalize(arg) for arg in (who, whom)] + # Sort to ensure same result for same input, regardless of order + who, whom = sorted([await normalize(arg) for arg in (who, whom)]) # Make sure user didn't provide something silly such as 10 spaces if not (who and whom): -- cgit v1.2.3 From 66d531583beef60d27bcb3afdd4e127531b7cb75 Mon Sep 17 00:00:00 2001 From: Chris Lovering Date: Tue, 10 Aug 2021 14:23:35 +0100 Subject: Update love calculator docstring to reflect new behaviour Since we update how the command behaves, by not allow arbitrary stings, and being symmetrical, we should reflect this in the docstring. --- bot/exts/valentines/lovecalculator.py | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) (limited to 'bot') diff --git a/bot/exts/valentines/lovecalculator.py b/bot/exts/valentines/lovecalculator.py index d06d883d..d80f2adf 100644 --- a/bot/exts/valentines/lovecalculator.py +++ b/bot/exts/valentines/lovecalculator.py @@ -31,20 +31,18 @@ class LoveCalculator(Cog): """ Tells you how much the two love each other. - This command accepts users or arbitrary strings as arguments. - Users are converted from: + This command requires at least one member as input, if two are given love will be calculated between + those two users, if only one is given, the second member is asusmed to be the invoker. + Members are converted from: - User ID - Mention - name#discrim - name - nickname - Any two arguments will always yield the same result, though the order of arguments matters: - Running .love joseph erlang will always yield the same result. - Running .love erlang joseph won't yield the same result as .love joseph erlang - - If you want to use multiple words for one argument, you must include quotes. - .love "Zes Vappa" "morning coffee" + Any two arguments will always yield the same result, regardless of the order of arguments: + Running .love @joe#6000 @chrisjl#2655 will always yield the same result. + Running .love @chrisjl#2655 @joe#6000 will yield the same result as before. """ if whom is None: whom = ctx.author -- cgit v1.2.3 From 0a2e18423a8042f6ab7e96e3d0775402a5cddac8 Mon Sep 17 00:00:00 2001 From: Chris Lovering Date: Tue, 10 Aug 2021 14:24:57 +0100 Subject: Remove unneeded checks in love calculator command Now that we don't allow arbitrary strings, we can simplify the input validation/modification we do. --- bot/exts/valentines/lovecalculator.py | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) (limited to 'bot') diff --git a/bot/exts/valentines/lovecalculator.py b/bot/exts/valentines/lovecalculator.py index d80f2adf..46bcc6fd 100644 --- a/bot/exts/valentines/lovecalculator.py +++ b/bot/exts/valentines/lovecalculator.py @@ -4,7 +4,7 @@ import json import logging import random from pathlib import Path -from typing import Coroutine, Optional, Union +from typing import Coroutine, Optional import discord from discord import Member @@ -53,23 +53,13 @@ class LoveCalculator(Cog): )): raise BadArgument("Both members must have the love fest role!") - def normalize(arg: Union[Member, str]) -> Coroutine: - if isinstance(arg, Member): - # If we are given a member, return name#discrim without any extra changes - arg = str(arg) - else: - # Otherwise normalise case and remove any leading/trailing whitespace - arg = arg.strip().title() + def normalize(arg: Member) -> Coroutine: # This has to be done manually to be applied to usernames - return clean_content(escape_markdown=True).convert(ctx, arg) + return clean_content(escape_markdown=True).convert(ctx, str(arg)) # Sort to ensure same result for same input, regardless of order who, whom = sorted([await normalize(arg) for arg in (who, whom)]) - # Make sure user didn't provide something silly such as 10 spaces - if not (who and whom): - raise BadArgument("Arguments must be non-empty strings.") - # Hash inputs to guarantee consistent results (hashing algorithm choice arbitrary) # # hashlib is used over the builtin hash() to guarantee same result over multiple runtimes -- cgit v1.2.3 From 298f8fc85bf65b9fd52f524b0d61eb99ef7e4687 Mon Sep 17 00:00:00 2001 From: ChrisJL Date: Tue, 10 Aug 2021 15:17:30 +0100 Subject: Update love calculator error output to be consistent with other role check fails Co-authored-by: Matteo Bertucci --- bot/exts/valentines/lovecalculator.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'bot') diff --git a/bot/exts/valentines/lovecalculator.py b/bot/exts/valentines/lovecalculator.py index 46bcc6fd..d9b60cfa 100644 --- a/bot/exts/valentines/lovecalculator.py +++ b/bot/exts/valentines/lovecalculator.py @@ -51,7 +51,7 @@ class LoveCalculator(Cog): Lovefest.role_id in [role.id for role in who.roles], Lovefest.role_id in [role.id for role in whom.roles] )): - raise BadArgument("Both members must have the love fest role!") + raise BadArgument("Both members must have the lovefest role!") def normalize(arg: Member) -> Coroutine: # This has to be done manually to be applied to usernames -- cgit v1.2.3 From 871445727937791645e892e9fe33c6e7f47c2e04 Mon Sep 17 00:00:00 2001 From: Chris Lovering Date: Thu, 12 Aug 2021 18:41:32 +0100 Subject: Give lovefest unsub info in lovecalc footer --- bot/exts/valentines/lovecalculator.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'bot') diff --git a/bot/exts/valentines/lovecalculator.py b/bot/exts/valentines/lovecalculator.py index d9b60cfa..b59e60ad 100644 --- a/bot/exts/valentines/lovecalculator.py +++ b/bot/exts/valentines/lovecalculator.py @@ -12,7 +12,7 @@ from discord.ext import commands from discord.ext.commands import BadArgument, Cog, clean_content from bot.bot import Bot -from bot.constants import Lovefest, Month +from bot.constants import Client, Lovefest, Month from bot.utils.decorators import in_month log = logging.getLogger(__name__) @@ -85,6 +85,7 @@ class LoveCalculator(Cog): name="A letter from Dr. Love:", value=data["text"] ) + embed.set_footer(text=f"You can unsubscribe from lovefest by using {Client.prefix}lovefest unsub") await ctx.send(embed=embed) -- cgit v1.2.3 From 446c86f54ae92ee0cde9a52f2e3bf09362e0add4 Mon Sep 17 00:00:00 2001 From: Chris Lovering Date: Thu, 12 Aug 2021 18:44:25 +0100 Subject: Don't role check if running lovecalc on yourself This skips the role check if the author is running the love calc command on themselves. We still want to check the the user running against has the role, along with if the author is running it against 2 other people. i have also included info on how to get the lovefest role in the error embed. --- bot/exts/valentines/lovecalculator.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) (limited to 'bot') diff --git a/bot/exts/valentines/lovecalculator.py b/bot/exts/valentines/lovecalculator.py index b59e60ad..1cb10e64 100644 --- a/bot/exts/valentines/lovecalculator.py +++ b/bot/exts/valentines/lovecalculator.py @@ -12,7 +12,7 @@ from discord.ext import commands from discord.ext.commands import BadArgument, Cog, clean_content from bot.bot import Bot -from bot.constants import Client, Lovefest, Month +from bot.constants import Channels, Client, Lovefest, Month from bot.utils.decorators import in_month log = logging.getLogger(__name__) @@ -44,15 +44,19 @@ class LoveCalculator(Cog): Running .love @joe#6000 @chrisjl#2655 will always yield the same result. Running .love @chrisjl#2655 @joe#6000 will yield the same result as before. """ + if ( + Lovefest.role_id not in [role.id for role in who.roles] + or (whom is not None and Lovefest.role_id not in [role.id for role in whom.roles]) + ): + raise BadArgument( + "This command can only be ran against members with the lovefest role! " + "This role be can assigned by running " + f"`{Client.prefix}lovefest sub` in <#{Channels.community_bot_commands}>." + ) + if whom is None: whom = ctx.author - if not all(( - Lovefest.role_id in [role.id for role in who.roles], - Lovefest.role_id in [role.id for role in whom.roles] - )): - raise BadArgument("Both members must have the lovefest role!") - def normalize(arg: Member) -> Coroutine: # This has to be done manually to be applied to usernames return clean_content(escape_markdown=True).convert(ctx, str(arg)) -- cgit v1.2.3