diff options
| author | 2022-11-26 19:17:26 +0100 | |
|---|---|---|
| committer | 2022-11-26 19:17:26 +0100 | |
| commit | 6ab9f9cf2a5889f3f6999117458e75140a40533b (patch) | |
| tree | 302096eb83316e20d91b01502e1612326deb0bb3 | |
| parent | add the AllSelfAssignableRolesView and its corresponding ClaimAllSelfAssignab... (diff) | |
add the logic for attaching the persistent view
This comes with another dependency to try to locate the message that needs to hold the view.
If that message is not found, a new one will be created & sent.
| -rw-r--r-- | bot/exts/info/subscribe.py | 37 |
1 files changed, 36 insertions, 1 deletions
diff --git a/bot/exts/info/subscribe.py b/bot/exts/info/subscribe.py index 4991f4a96..c8886c4f7 100644 --- a/bot/exts/info/subscribe.py +++ b/bot/exts/info/subscribe.py @@ -13,6 +13,7 @@ from bot import constants from bot.bot import Bot from bot.decorators import redirect_output from bot.log import get_logger +from bot.utils.channel import get_or_fetch_channel @dataclass(frozen=True) @@ -74,7 +75,7 @@ class ClaimAllSelfAssignableRolesButton(discord.ui.Button): def __init__(self): super().__init__( style=discord.ButtonStyle.success, - label="Assign me a", + label="Claim all available roles", custom_id=self.CUSTOM_ID, row=1 ) @@ -163,6 +164,8 @@ class SingleRoleButton(discord.ui.Button): class Subscribe(commands.Cog): """Cog to allow user to self-assign & remove the roles present in ASSIGNABLE_ROLES.""" + SELF_ASSIGNABLE_ROLES_MESSAGE = "Click on this button to earn all the currently available server roles" + def __init__(self, bot: Bot): self.bot = bot self.assignable_roles: list[AssignableRole] = [] @@ -190,6 +193,9 @@ class Subscribe(commands.Cog): self.assignable_roles.sort(key=operator.attrgetter("name")) self.assignable_roles.sort(key=operator.methodcaller("is_currently_available"), reverse=True) + initial_self_assignable_roles_message = await self.__search_for_self_assignable_roles_message() + self.__attach_view_to_initial_self_assignable_roles_message(initial_self_assignable_roles_message) + @commands.cooldown(1, 10, commands.BucketType.member) @commands.command(name="subscribe", aliases=("unsubscribe",)) @redirect_output( @@ -210,6 +216,35 @@ class Subscribe(commands.Cog): delete_after=DELETE_MESSAGE_AFTER, ) + async def __search_for_self_assignable_roles_message(self) -> discord.Message: + """ + Searches for the message that holds the self assignable roles view. + + If the initial message isn't found, a new one will be created. + This message will always be needed to attach the persistent view to it + """ + roles_channel = await get_or_fetch_channel(constants.Channels.roles) + + async for message in roles_channel.history(limit=30): + if message.content == self.SELF_ASSIGNABLE_ROLES_MESSAGE: + log.debug(f"Found self assignable roles view message: {message.id}") + return message + + log.debug("Self assignable roles view message hasn't been found, creating a new one.") + view = AllSelfAssignableRolesView() + view.add_item(ClaimAllSelfAssignableRolesButton()) + return await roles_channel.send(self.SELF_ASSIGNABLE_ROLES_MESSAGE, view=view) + + def __attach_view_to_initial_self_assignable_roles_message(self, message: discord.Message) -> None: + """ + Attaches the persistent self assignable roles view. + + The message is searched for/created upon loading the Cog. + """ + view = AllSelfAssignableRolesView() + view.add_item(ClaimAllSelfAssignableRolesButton()) + self.bot.add_view(view, message_id=message.id) + async def setup(bot: Bot) -> None: """Load the Subscribe cog.""" |