From 26e3045aec9a4131df072924a974024122418fa1 Mon Sep 17 00:00:00 2001 From: Bluenix Date: Thu, 7 Oct 2021 21:36:50 +0200 Subject: [PATCH 1/6] Add command that let's Tyrant pick a user's role This command can be used when somebody doesn't know what role they want to pick. --- tyrant/__main__.py | 3 ++- tyrant/cogs/pick_team.py | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 tyrant/cogs/pick_team.py diff --git a/tyrant/__main__.py b/tyrant/__main__.py index 7c247ca..96d01a6 100644 --- a/tyrant/__main__.py +++ b/tyrant/__main__.py @@ -15,8 +15,9 @@ ) # Load the extensions we want -bot.load_extension("tyrant.cogs.lemon_facts") bot.load_extension("tyrant.cogs.fruit_vs_vegetables") +bot.load_extension("tyrant.cogs.lemon_facts") +bot.load_extension("tyrant.cogs.pick_team") bot.load_extension("tyrant.cogs.purge") # Validate the token diff --git a/tyrant/cogs/pick_team.py b/tyrant/cogs/pick_team.py new file mode 100644 index 0000000..51f646e --- /dev/null +++ b/tyrant/cogs/pick_team.py @@ -0,0 +1,34 @@ +from discord import AllowedMentions +from discord.ext import commands +from discord.ext.commands import Bot, Cog, Context + +from tyrant import constants + + +class PickTeam(Cog): + """Command letting the Tyrant pick a team for the user.""" + + @commands.command(aliases=("team",)) + async def pick(self, ctx: Context): + """Let the Tyrant pick a fruit or vegetable for you.""" + # We want a sense of randomness from this algorithm so that the chosen + # roles are evenly spread out, at the same time we want the command to + # choose the same roles even if invoked twice. + key = hash( + ctx.author.display_name + + ctx.author.discriminator + + ctx.author.display_avatar.key + ) + + index = key % len(constants.ALL_FRUIT_AND_VEG_ROLES) + role = constants.ALL_FRUIT_AND_VEG_ROLES[index] + + await ctx.send( + f"The Tyrant decided that your role will be: <@&{role}>", + allowed_mentions=AllowedMentions.none() + ) + + +def setup(bot: Bot) -> None: + """Called by discord.py to load the extension which will add the above cog.""" + bot.add_cog(PickTeam()) From adfa98ac0fe0b9d19aed663c36f2ddbf18be9e51 Mon Sep 17 00:00:00 2001 From: Bluenix Date: Tue, 12 Oct 2021 22:16:05 +0200 Subject: [PATCH 2/6] Automatically assign team role in 'pick' command --- tyrant/cogs/fruit_vs_vegetables.py | 13 +++++++----- tyrant/cogs/pick_team.py | 22 +++++++++++++++++--- tyrant/constants.py | 32 +++++++++++++++--------------- 3 files changed, 43 insertions(+), 24 deletions(-) mode change 100755 => 100644 tyrant/cogs/fruit_vs_vegetables.py diff --git a/tyrant/cogs/fruit_vs_vegetables.py b/tyrant/cogs/fruit_vs_vegetables.py old mode 100755 new mode 100644 index bfd29e4..a824922 --- a/tyrant/cogs/fruit_vs_vegetables.py +++ b/tyrant/cogs/fruit_vs_vegetables.py @@ -13,6 +13,11 @@ def __init__(self, bot: Bot): self.bot = bot self.locks = {} + async def assign_roles(self, member, *roles) -> None: + """Assign roles to a member and remove the old team roles.""" + await member.remove_roles(*[role for role in member.roles if role.id in constants.ALL_FRUIT_AND_VEG_ROLES]) + await member.add_roles(*roles) + @Cog.listener() async def on_raw_reaction_add(self, payload): """Distribute fruit or vegetable role, when appropriate.""" @@ -36,13 +41,11 @@ async def on_raw_reaction_add(self, payload): # Get the role ID from the emoji fruit_role_id = constants.EMOJI_TO_ROLE[emoji.name] - team_id = constants.EMOJI_TO_TEAM[emoji.name] + team_id = constants.ROLE_TO_TEAM[fruit_role_id] fruit_role = guild.get_role(fruit_role_id) team_role = guild.get_role(team_id) - # Get rid of old roles, assign the new ones - await member.remove_roles(*[role for role in member.roles if role.id in constants.ALL_FRUIT_AND_VEG_ROLES]) - await member.add_roles(fruit_role, team_role) + await self.assign_roles(member, fruit_role, team_role) # Finally, remove all other reactions than this one fruit_message = await channel.fetch_message(constants.Messages.fruit_role_assignment) @@ -79,7 +82,7 @@ async def on_raw_reaction_remove(self, payload): # Get the role ID from the emoji fruit_role_id = constants.EMOJI_TO_ROLE[emoji.name] - team_id = constants.EMOJI_TO_TEAM[emoji.name] + team_id = constants.ROLE_TO_TEAM[fruit_role_id] team_role = guild.get_role(team_id) # Remove all fruit and veg roles from the member diff --git a/tyrant/cogs/pick_team.py b/tyrant/cogs/pick_team.py index 51f646e..de37f36 100644 --- a/tyrant/cogs/pick_team.py +++ b/tyrant/cogs/pick_team.py @@ -1,6 +1,7 @@ from discord import AllowedMentions from discord.ext import commands from discord.ext.commands import Bot, Cog, Context +from loguru import logger from tyrant import constants @@ -8,6 +9,10 @@ class PickTeam(Cog): """Command letting the Tyrant pick a team for the user.""" + def __init__(self, bot: Bot): + """Initialize this cog with the Bot instance.""" + self.bot = bot + @commands.command(aliases=("team",)) async def pick(self, ctx: Context): """Let the Tyrant pick a fruit or vegetable for you.""" @@ -21,14 +26,25 @@ async def pick(self, ctx: Context): ) index = key % len(constants.ALL_FRUIT_AND_VEG_ROLES) - role = constants.ALL_FRUIT_AND_VEG_ROLES[index] + choice = constants.ALL_FRUIT_AND_VEG_ROLES[index] + + if cog := self.bot.get_cog("FruitVsVegetables"): + await cog.assign_roles( + ctx.author, + ctx.guild.get_role(choice), + ctx.guild.get_role(constants.ROLE_TO_TEAM[choice]) + ) + else: + # The FruitVsVegetables cog being loaded is a pretty important + # aspect of this command but we can still recover without it + logger.warning('Could not assign role because FruitVsVegetables cog is unloaded') await ctx.send( - f"The Tyrant decided that your role will be: <@&{role}>", + f"The Tyrant decided that your role will be: <@&{choice}>", allowed_mentions=AllowedMentions.none() ) def setup(bot: Bot) -> None: """Called by discord.py to load the extension which will add the above cog.""" - bot.add_cog(PickTeam()) + bot.add_cog(PickTeam(bot)) diff --git a/tyrant/constants.py b/tyrant/constants.py index 60b8b91..2e06905 100644 --- a/tyrant/constants.py +++ b/tyrant/constants.py @@ -151,22 +151,22 @@ "🥑": 883078311494963210, } -EMOJI_TO_TEAM = { - "🌶️": 883078871770071051, - "🥕": 883078871770071051, - "yellow_bell_pepper": 883078871770071051, - "🥑": 883078871770071051, - "blue_asparagus": 883078871770071051, - "cabbage": 883078871770071051, - "radish": 883078871770071051, - - "pomegranate": 883080439231840259, - "🍊": 883080439231840259, - "🍌": 883080439231840259, - "🥝": 883080439231840259, - "🫐": 883080439231840259, - "🍇": 883080439231840259, - "dragonfruit": 883080439231840259, +ROLE_TO_TEAM = { + 883077551927484476: 883078871770071051, + 883077759985930290: 883078871770071051, + 883078108457083010: 883078871770071051, + 883078311494963210: 883078871770071051, + 883078590655258674: 883078871770071051, + 882529427962069047: 883078871770071051, + 802889394074681426: 883078871770071051, + + 802850922375282698: 883080439231840259, + 802843500638240789: 883080439231840259, + 843204335793602601: 883080439231840259, + 802851237837799455: 883080439231840259, + 882360895462846545: 883080439231840259, + 882633463432355882: 883080439231840259, + 816010822777831444: 883080439231840259, } From 900740f96bda85fe42dad15091c267e4eb2e3061 Mon Sep 17 00:00:00 2001 From: Bluenix Date: Sun, 27 Feb 2022 13:15:21 +0100 Subject: [PATCH 3/6] Migrate pick team command to disnake --- tyrant/cogs/pick_team.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tyrant/cogs/pick_team.py b/tyrant/cogs/pick_team.py index de37f36..bcc92d6 100644 --- a/tyrant/cogs/pick_team.py +++ b/tyrant/cogs/pick_team.py @@ -1,6 +1,6 @@ -from discord import AllowedMentions -from discord.ext import commands -from discord.ext.commands import Bot, Cog, Context +from disnake import AllowedMentions +from disnake.ext import commands +from disnake.ext.commands import Bot, Cog, Context from loguru import logger from tyrant import constants From 9e5166c9756efce20300511dff7741725ece2f73 Mon Sep 17 00:00:00 2001 From: Bluenix Date: Sun, 27 Feb 2022 13:15:56 +0100 Subject: [PATCH 4/6] Make team reveal more dramatic --- tyrant/cogs/pick_team.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tyrant/cogs/pick_team.py b/tyrant/cogs/pick_team.py index bcc92d6..96d3c7a 100644 --- a/tyrant/cogs/pick_team.py +++ b/tyrant/cogs/pick_team.py @@ -40,7 +40,7 @@ async def pick(self, ctx: Context): logger.warning('Could not assign role because FruitVsVegetables cog is unloaded') await ctx.send( - f"The Tyrant decided that your role will be: <@&{choice}>", + f"**The Tyrant decided that your role will be**... <@&{choice}>", allowed_mentions=AllowedMentions.none() ) From fc3e36b9984a30011d1753a042b8e5910b422361 Mon Sep 17 00:00:00 2001 From: Bluenix Date: Sun, 27 Feb 2022 13:16:31 +0100 Subject: [PATCH 5/6] Limit pick team command from being used in DMs --- tyrant/cogs/pick_team.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tyrant/cogs/pick_team.py b/tyrant/cogs/pick_team.py index 96d3c7a..3e2c81e 100644 --- a/tyrant/cogs/pick_team.py +++ b/tyrant/cogs/pick_team.py @@ -16,6 +16,9 @@ def __init__(self, bot: Bot): @commands.command(aliases=("team",)) async def pick(self, ctx: Context): """Let the Tyrant pick a fruit or vegetable for you.""" + if ctx.guild is None: + return await ctx.send('This command can only be used in the server.') + # We want a sense of randomness from this algorithm so that the chosen # roles are evenly spread out, at the same time we want the command to # choose the same roles even if invoked twice. From a9b00c893f1e87ad9e9cd1a381824de79f46b82a Mon Sep 17 00:00:00 2001 From: Bluenix Date: Sun, 27 Feb 2022 15:02:28 +0100 Subject: [PATCH 6/6] Rename pick team command to `random_team` --- tyrant/__main__.py | 2 +- tyrant/cogs/{pick_team.py => random_team.py} | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) rename tyrant/cogs/{pick_team.py => random_team.py} (88%) diff --git a/tyrant/__main__.py b/tyrant/__main__.py index 60e46ac..ad0d585 100644 --- a/tyrant/__main__.py +++ b/tyrant/__main__.py @@ -23,8 +23,8 @@ bot.load_extension("tyrant.cogs.ask_tyrant") bot.load_extension("tyrant.cogs.fruit_vs_vegetables") bot.load_extension("tyrant.cogs.lemon_facts") -bot.load_extension("tyrant.cogs.pick_team") bot.load_extension("tyrant.cogs.purge") +bot.load_extension("tyrant.cogs.random_team") bot.load_extension("tyrant.cogs.teamcount") # Validate the token diff --git a/tyrant/cogs/pick_team.py b/tyrant/cogs/random_team.py similarity index 88% rename from tyrant/cogs/pick_team.py rename to tyrant/cogs/random_team.py index 3e2c81e..42e49bd 100644 --- a/tyrant/cogs/pick_team.py +++ b/tyrant/cogs/random_team.py @@ -6,15 +6,15 @@ from tyrant import constants -class PickTeam(Cog): - """Command letting the Tyrant pick a team for the user.""" +class RandomTeam(Cog): + """Command letting the Tyrant pick a random team for the user.""" def __init__(self, bot: Bot): """Initialize this cog with the Bot instance.""" self.bot = bot - @commands.command(aliases=("team",)) - async def pick(self, ctx: Context): + @commands.command(aliases=("enlist", "enroll")) + async def random_team(self, ctx: Context): """Let the Tyrant pick a fruit or vegetable for you.""" if ctx.guild is None: return await ctx.send('This command can only be used in the server.') @@ -50,4 +50,4 @@ async def pick(self, ctx: Context): def setup(bot: Bot) -> None: """Called by discord.py to load the extension which will add the above cog.""" - bot.add_cog(PickTeam(bot)) + bot.add_cog(RandomTeam(bot))