Skip to content

Commit adfa98a

Browse files
committed
Automatically assign team role in 'pick' command
1 parent 26e3045 commit adfa98a

File tree

3 files changed

+43
-24
lines changed

3 files changed

+43
-24
lines changed

tyrant/cogs/fruit_vs_vegetables.py

100755100644
Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ def __init__(self, bot: Bot):
1313
self.bot = bot
1414
self.locks = {}
1515

16+
async def assign_roles(self, member, *roles) -> None:
17+
"""Assign roles to a member and remove the old team roles."""
18+
await member.remove_roles(*[role for role in member.roles if role.id in constants.ALL_FRUIT_AND_VEG_ROLES])
19+
await member.add_roles(*roles)
20+
1621
@Cog.listener()
1722
async def on_raw_reaction_add(self, payload):
1823
"""Distribute fruit or vegetable role, when appropriate."""
@@ -36,13 +41,11 @@ async def on_raw_reaction_add(self, payload):
3641

3742
# Get the role ID from the emoji
3843
fruit_role_id = constants.EMOJI_TO_ROLE[emoji.name]
39-
team_id = constants.EMOJI_TO_TEAM[emoji.name]
44+
team_id = constants.ROLE_TO_TEAM[fruit_role_id]
4045
fruit_role = guild.get_role(fruit_role_id)
4146
team_role = guild.get_role(team_id)
4247

43-
# Get rid of old roles, assign the new ones
44-
await member.remove_roles(*[role for role in member.roles if role.id in constants.ALL_FRUIT_AND_VEG_ROLES])
45-
await member.add_roles(fruit_role, team_role)
48+
await self.assign_roles(member, fruit_role, team_role)
4649

4750
# Finally, remove all other reactions than this one
4851
fruit_message = await channel.fetch_message(constants.Messages.fruit_role_assignment)
@@ -79,7 +82,7 @@ async def on_raw_reaction_remove(self, payload):
7982

8083
# Get the role ID from the emoji
8184
fruit_role_id = constants.EMOJI_TO_ROLE[emoji.name]
82-
team_id = constants.EMOJI_TO_TEAM[emoji.name]
85+
team_id = constants.ROLE_TO_TEAM[fruit_role_id]
8386
team_role = guild.get_role(team_id)
8487

8588
# Remove all fruit and veg roles from the member

tyrant/cogs/pick_team.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
from discord import AllowedMentions
22
from discord.ext import commands
33
from discord.ext.commands import Bot, Cog, Context
4+
from loguru import logger
45

56
from tyrant import constants
67

78

89
class PickTeam(Cog):
910
"""Command letting the Tyrant pick a team for the user."""
1011

12+
def __init__(self, bot: Bot):
13+
"""Initialize this cog with the Bot instance."""
14+
self.bot = bot
15+
1116
@commands.command(aliases=("team",))
1217
async def pick(self, ctx: Context):
1318
"""Let the Tyrant pick a fruit or vegetable for you."""
@@ -21,14 +26,25 @@ async def pick(self, ctx: Context):
2126
)
2227

2328
index = key % len(constants.ALL_FRUIT_AND_VEG_ROLES)
24-
role = constants.ALL_FRUIT_AND_VEG_ROLES[index]
29+
choice = constants.ALL_FRUIT_AND_VEG_ROLES[index]
30+
31+
if cog := self.bot.get_cog("FruitVsVegetables"):
32+
await cog.assign_roles(
33+
ctx.author,
34+
ctx.guild.get_role(choice),
35+
ctx.guild.get_role(constants.ROLE_TO_TEAM[choice])
36+
)
37+
else:
38+
# The FruitVsVegetables cog being loaded is a pretty important
39+
# aspect of this command but we can still recover without it
40+
logger.warning('Could not assign role because FruitVsVegetables cog is unloaded')
2541

2642
await ctx.send(
27-
f"The Tyrant decided that your role will be: <@&{role}>",
43+
f"The Tyrant decided that your role will be: <@&{choice}>",
2844
allowed_mentions=AllowedMentions.none()
2945
)
3046

3147

3248
def setup(bot: Bot) -> None:
3349
"""Called by discord.py to load the extension which will add the above cog."""
34-
bot.add_cog(PickTeam())
50+
bot.add_cog(PickTeam(bot))

tyrant/constants.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -151,22 +151,22 @@
151151
"🥑": 883078311494963210,
152152
}
153153

154-
EMOJI_TO_TEAM = {
155-
"🌶️": 883078871770071051,
156-
"🥕": 883078871770071051,
157-
"yellow_bell_pepper": 883078871770071051,
158-
"🥑": 883078871770071051,
159-
"blue_asparagus": 883078871770071051,
160-
"cabbage": 883078871770071051,
161-
"radish": 883078871770071051,
162-
163-
"pomegranate": 883080439231840259,
164-
"🍊": 883080439231840259,
165-
"🍌": 883080439231840259,
166-
"🥝": 883080439231840259,
167-
"🫐": 883080439231840259,
168-
"🍇": 883080439231840259,
169-
"dragonfruit": 883080439231840259,
154+
ROLE_TO_TEAM = {
155+
883077551927484476: 883078871770071051,
156+
883077759985930290: 883078871770071051,
157+
883078108457083010: 883078871770071051,
158+
883078311494963210: 883078871770071051,
159+
883078590655258674: 883078871770071051,
160+
882529427962069047: 883078871770071051,
161+
802889394074681426: 883078871770071051,
162+
163+
802850922375282698: 883080439231840259,
164+
802843500638240789: 883080439231840259,
165+
843204335793602601: 883080439231840259,
166+
802851237837799455: 883080439231840259,
167+
882360895462846545: 883080439231840259,
168+
882633463432355882: 883080439231840259,
169+
816010822777831444: 883080439231840259,
170170
}
171171

172172

0 commit comments

Comments
 (0)