Skip to content
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions tyrant/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@
intents=intents,
)

# Load the extensions we want
# Load the extensions we want (alphabetical)
bot.load_extension("tyrant.cogs.ask_tyrant")
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")
bot.load_extension("tyrant.cogs.teamcount")

Expand Down
13 changes: 8 additions & 5 deletions tyrant/cogs/fruit_vs_vegetables.py
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down
53 changes: 53 additions & 0 deletions tyrant/cogs/pick_team.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
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


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):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These command names and alises feel wrong. .team or .pick feels like commands that you would use if the user used them to pick a team, not a command that would randomly assign a team for you.

I think we should find a more descriptive name for this. something like .random_team feels like an obvious choice, and .enlist and .enroll feel like fun tyrant-themed aliases.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you want me to rename the cog and file with these changes?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure, the cog and the file can sort of be defended with the rationale that this bot is the tyrant and the tyrant is the one picking the role. But I don't think we can defend the commands, because the users are the ones running those commands.

I'll leave it up to you.

"""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.
key = hash(
ctx.author.display_name +
ctx.author.discriminator +
ctx.author.display_avatar.key
)

index = key % len(constants.ALL_FRUIT_AND_VEG_ROLES)
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**... <@&{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))
32 changes: 16 additions & 16 deletions tyrant/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,22 +173,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,
}


Expand Down