diff options
author | 2023-06-26 00:37:00 -0400 | |
---|---|---|
committer | 2023-06-26 00:37:00 -0400 | |
commit | c8bcc6353c9ce38003585c49236b733e392a2a03 (patch) | |
tree | c558e3d967a1b4096efab851b4b2c80a8b09c12a | |
parent | Bump ruff from 0.0.272 to 0.0.274 (#2651) (diff) | |
parent | Merge branch 'main' into simplify-subscribe (diff) |
Merge pull request #2650 from python-discord/simplify-subscribe
Simplify subscribe cog
-rw-r--r-- | bot/exts/info/subscribe.py | 82 |
1 files changed, 21 insertions, 61 deletions
diff --git a/bot/exts/info/subscribe.py b/bot/exts/info/subscribe.py index c4f206a27..4f57f7c0f 100644 --- a/bot/exts/info/subscribe.py +++ b/bot/exts/info/subscribe.py @@ -1,8 +1,6 @@ -import calendar import operator from dataclasses import dataclass -import arrow import discord from discord.ext import commands from discord.interactions import Interaction @@ -17,41 +15,19 @@ from bot.utils.channel import get_or_fetch_channel @dataclass(frozen=True) class AssignableRole: - """ - A role that can be assigned to a user. - - months_available is a tuple that signifies what months the role should be - self-assignable, using None for when it should always be available. - """ + """A role that can be assigned to a user.""" role_id: int - months_available: tuple[int] | None name: str | None = None # This gets populated within Subscribe.cog_load() - def is_currently_available(self) -> bool: - """Check if the role is available for the current month.""" - if self.months_available is None: - return True - return arrow.utcnow().month in self.months_available - - def get_readable_available_months(self) -> str: - """Get a readable string of the months the role is available.""" - if self.months_available is None: - return f"{self.name} is always available." - - # Join the months together with comma separators, but use "and" for the final seperator. - month_names = [calendar.month_name[month] for month in self.months_available] - available_months_str = ", ".join(month_names[:-1]) + f" and {month_names[-1]}" - return f"{self.name} can only be assigned during {available_months_str}." - ASSIGNABLE_ROLES = ( - AssignableRole(constants.Roles.announcements, None), - AssignableRole(constants.Roles.pyweek_announcements, None), - AssignableRole(constants.Roles.legacy_help_channels_access, None), - AssignableRole(constants.Roles.lovefest, None), - AssignableRole(constants.Roles.advent_of_code, None), - AssignableRole(constants.Roles.revival_of_code, None), + AssignableRole(constants.Roles.announcements), + AssignableRole(constants.Roles.pyweek_announcements), + AssignableRole(constants.Roles.legacy_help_channels_access), + AssignableRole(constants.Roles.lovefest), + AssignableRole(constants.Roles.advent_of_code), + AssignableRole(constants.Roles.revival_of_code), ) ITEMS_PER_ROW = 3 @@ -61,16 +37,7 @@ log = get_logger(__name__) class RoleButtonView(discord.ui.View): - """ - A view that holds the list of SingleRoleButtons to show to the member. - - Attributes - __________ - interaction_owner: discord.Member - The member that initiated the interaction - """ - - interaction_owner: discord.Member + """A view that holds the list of SingleRoleButtons to show to the member.""" def __init__(self, member: discord.Member, assignable_roles: list[AssignableRole]): super().__init__(timeout=DELETE_MESSAGE_AFTER) @@ -101,12 +68,8 @@ class SingleRoleButton(discord.ui.Button): LABEL_FORMAT = "{action} role {role_name}" def __init__(self, role: AssignableRole, assigned: bool, row: int): - if role.is_currently_available(): - style = self.REMOVE_STYLE if assigned else self.ADD_STYLE - label = self.LABEL_FORMAT.format(action="Remove" if assigned else "Add", role_name=role.name) - else: - style = self.UNAVAILABLE_STYLE - label = f"🔒 {role.name}" + style = self.REMOVE_STYLE if assigned else self.ADD_STYLE + label = self.LABEL_FORMAT.format(action="Remove" if assigned else "Add", role_name=role.name) super().__init__( style=style, @@ -124,10 +87,6 @@ class SingleRoleButton(discord.ui.Button): self.view.stop() return - if not self.role.is_currently_available(): - await interaction.response.send_message(self.role.get_readable_available_months(), ephemeral=True) - return - await members.handle_role_change( interaction.user, interaction.user.remove_roles if self.assigned else interaction.user.add_roles, @@ -135,8 +94,15 @@ class SingleRoleButton(discord.ui.Button): ) self.assigned = not self.assigned - await self.update_view(interaction) - await interaction.followup.send( + try: + await self.update_view(interaction) + send_function = interaction.followup.send + except discord.NotFound: + log.debug("Subscribe message for %s removed before buttons could be updated", interaction.user) + self.view.stop() + send_function = interaction.response.send_message + + await send_function( self.LABEL_FORMAT.format(action="Added" if self.assigned else "Removed", role_name=self.role.name), ephemeral=True, ) @@ -145,11 +111,7 @@ class SingleRoleButton(discord.ui.Button): """Updates the original interaction message with a new view object with the updated buttons.""" self.style = self.REMOVE_STYLE if self.assigned else self.ADD_STYLE self.label = self.LABEL_FORMAT.format(action="Remove" if self.assigned else "Add", role_name=self.role.name) - try: - await interaction.response.edit_message(view=self.view) - except discord.NotFound: - log.debug("Subscribe message for %s removed before buttons could be updated", interaction.user) - self.view.stop() + await interaction.response.edit_message(view=self.view) class AllSelfAssignableRolesView(discord.ui.View): @@ -203,14 +165,12 @@ class Subscribe(commands.Cog): self.assignable_roles.append( AssignableRole( role_id=role.role_id, - months_available=role.months_available, name=discord_role.name, ) ) - # Sort by role name, then shift unavailable roles to the end of the list + # Sort by role name self.assignable_roles.sort(key=operator.attrgetter("name")) - self.assignable_roles.sort(key=operator.methodcaller("is_currently_available"), reverse=True) placeholder_message_view_tuple = await self._fetch_or_create_self_assignable_roles_message() self_assignable_roles_message, self_assignable_roles_view = placeholder_message_view_tuple |