From 5416280755631f7051e99e8a074af50c98974944 Mon Sep 17 00:00:00 2001 From: MarkKoz Date: Sun, 12 Apr 2020 11:56:54 -0700 Subject: Constants: add help channel cooldown role --- bot/constants.py | 1 + config-default.yml | 1 + 2 files changed, 2 insertions(+) diff --git a/bot/constants.py b/bot/constants.py index 2add028e7..49098c9f2 100644 --- a/bot/constants.py +++ b/bot/constants.py @@ -421,6 +421,7 @@ class Roles(metaclass=YAMLGetter): announcements: int contributors: int core_developers: int + help_cooldown: int helpers: int jammers: int moderators: int diff --git a/config-default.yml b/config-default.yml index f2b0bfa9f..b0165adf6 100644 --- a/config-default.yml +++ b/config-default.yml @@ -201,6 +201,7 @@ guild: roles: announcements: 463658397560995840 contributors: 295488872404484098 + help_cooldown: 699189276025421825 muted: &MUTED_ROLE 277914926603829249 partners: 323426753857191936 python_community: &PY_COMMUNITY_ROLE 458226413825294336 -- cgit v1.2.3 From 9e67ebedcdc181ab0f90307afca5cbc0b1b9e816 Mon Sep 17 00:00:00 2001 From: MarkKoz Date: Sun, 12 Apr 2020 12:03:11 -0700 Subject: HelpChannels: remove ensure_permissions_synchronization --- bot/cogs/help_channels.py | 30 +----------------------------- 1 file changed, 1 insertion(+), 29 deletions(-) diff --git a/bot/cogs/help_channels.py b/bot/cogs/help_channels.py index e73bbdae5..56d2d26cd 100644 --- a/bot/cogs/help_channels.py +++ b/bot/cogs/help_channels.py @@ -481,7 +481,6 @@ class HelpChannels(Scheduler, commands.Cog): f"Ensuring that all channels in `{self.available_category}` have " f"synchronized permissions after moving `{channel}` into it." ) - await self.ensure_permissions_synchronization(self.available_category) self.report_stats() async def move_to_dormant(self, channel: discord.TextChannel, caller: str) -> None: @@ -620,39 +619,13 @@ class HelpChannels(Scheduler, commands.Cog): # be put in the queue. await self.move_to_available() - @staticmethod - async def ensure_permissions_synchronization(category: discord.CategoryChannel) -> None: - """ - Ensure that all channels in the `category` have their permissions synchronized. - - This method mitigates an issue we have yet to find the cause for: Every so often, a channel in the - `Help: Available` category gets in a state in which it will no longer synchronizes its permissions - with the category. To prevent that, we iterate over the channels in the category and edit the channels - that are observed to be in such a state. If no "out of sync" channels are observed, this method will - not make API calls and should be fairly inexpensive to run. - """ - for channel in category.channels: - if not channel.permissions_synced: - log.info(f"The permissions of channel `{channel}` were out of sync with category `{category}`.") - await channel.edit(sync_permissions=True) - async def update_category_permissions( self, category: discord.CategoryChannel, member: discord.Member, **permissions ) -> None: - """ - Update the permissions of the given `member` for the given `category` with `permissions` passed. - - After updating the permissions for the member in the category, this helper function will call the - `ensure_permissions_synchronization` method to ensure that all channels are still synchronizing their - permissions with the category. It's currently unknown why some channels get "out of sync", but this - hopefully mitigates the issue. - """ + """Update the permissions of the given `member` for the given `category` with `permissions` passed.""" log.trace(f"Updating permissions for `{member}` in `{category}` with {permissions}.") await category.set_permissions(member, **permissions) - log.trace(f"Ensuring that all channels in `{category}` are synchronized after permissions update.") - await self.ensure_permissions_synchronization(category) - async def reset_send_permissions(self) -> None: """Reset send permissions for members with it set to False in the Available category.""" log.trace("Resetting send permissions in the Available category.") @@ -666,7 +639,6 @@ class HelpChannels(Scheduler, commands.Cog): await self.available_category.set_permissions(member, overwrite=None) log.trace(f"Ensuring channels in `Help: Available` are synchronized after permissions reset.") - await self.ensure_permissions_synchronization(self.available_category) async def reset_claimant_send_permission(self, channel: discord.TextChannel) -> None: """Reset send permissions in the Available category for the help `channel` claimant.""" -- cgit v1.2.3 From 06d12a02b91535b8536f877fdcd0d85aac6b1039 Mon Sep 17 00:00:00 2001 From: MarkKoz Date: Mon, 13 Apr 2020 11:08:26 -0700 Subject: HelpChannels: add helper function to check for claimant role --- bot/cogs/help_channels.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/bot/cogs/help_channels.py b/bot/cogs/help_channels.py index 56d2d26cd..d47a42ca6 100644 --- a/bot/cogs/help_channels.py +++ b/bot/cogs/help_channels.py @@ -412,6 +412,11 @@ class HelpChannels(Scheduler, commands.Cog): self.bot.stats.gauge("help.total.available", total_available) self.bot.stats.gauge("help.total.dormant", total_dormant) + @staticmethod + def is_claimant(member: discord.Member) -> bool: + """Return True if `member` has the 'Help Cooldown' role.""" + return any(constants.Roles.help_cooldown == role.id for role in member.roles) + def is_dormant_message(self, message: t.Optional[discord.Message]) -> bool: """Return True if the contents of the `message` match `DORMANT_MSG`.""" if not message or not message.embeds: -- cgit v1.2.3 From 427c954903a62fe75aa22cf0fde9a52d2d6f2287 Mon Sep 17 00:00:00 2001 From: MarkKoz Date: Mon, 13 Apr 2020 11:45:48 -0700 Subject: HelpChannels: clear roles when resetting permissions Claimants will have a special role that needs to be removed rather than using member overwrites for the category. --- bot/cogs/help_channels.py | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/bot/cogs/help_channels.py b/bot/cogs/help_channels.py index d47a42ca6..5dc90ee8e 100644 --- a/bot/cogs/help_channels.py +++ b/bot/cogs/help_channels.py @@ -632,18 +632,16 @@ class HelpChannels(Scheduler, commands.Cog): await category.set_permissions(member, **permissions) async def reset_send_permissions(self) -> None: - """Reset send permissions for members with it set to False in the Available category.""" + """Reset send permissions in the Available category for claimants.""" log.trace("Resetting send permissions in the Available category.") + guild = self.bot.get_guild(constants.Guild.id) - for member, overwrite in self.available_category.overwrites.items(): - if isinstance(member, discord.Member) and overwrite.send_messages is False: + # TODO: replace with a persistent cache cause checking every member is quite slow + for member in guild.members: + if self.is_claimant(member): log.trace(f"Resetting send permissions for {member} ({member.id}).") - - # We don't use the permissions helper function here as we may have to reset multiple overwrites - # and we don't want to enforce the permissions synchronization in each iteration. - await self.available_category.set_permissions(member, overwrite=None) - - log.trace(f"Ensuring channels in `Help: Available` are synchronized after permissions reset.") + role = discord.Object(constants.Roles.help_cooldown) + await member.remove_roles(role) async def reset_claimant_send_permission(self, channel: discord.TextChannel) -> None: """Reset send permissions in the Available category for the help `channel` claimant.""" -- cgit v1.2.3 From efc778f87f0b4a6fb83007629aa5f6f868da564b Mon Sep 17 00:00:00 2001 From: MarkKoz Date: Fri, 17 Apr 2020 09:18:26 -0700 Subject: HelpChannels: add/remove a cooldown role rather than using overwrites Overwrites had issues syncing with channels in the category. * Remove update_category_permissions; obsolete * Add constant for the cooldown role wrapped in a discord.Object --- bot/cogs/help_channels.py | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/bot/cogs/help_channels.py b/bot/cogs/help_channels.py index 5dc90ee8e..47e74a2e5 100644 --- a/bot/cogs/help_channels.py +++ b/bot/cogs/help_channels.py @@ -21,6 +21,7 @@ log = logging.getLogger(__name__) ASKING_GUIDE_URL = "https://pythondiscord.com/pages/asking-good-questions/" MAX_CHANNELS_PER_CATEGORY = 50 +COOLDOWN_ROLE = discord.Object(constants.Roles.help_cooldown) AVAILABLE_TOPIC = """ This channel is available. Feel free to ask a question in order to claim this channel! @@ -624,13 +625,6 @@ class HelpChannels(Scheduler, commands.Cog): # be put in the queue. await self.move_to_available() - async def update_category_permissions( - self, category: discord.CategoryChannel, member: discord.Member, **permissions - ) -> None: - """Update the permissions of the given `member` for the given `category` with `permissions` passed.""" - log.trace(f"Updating permissions for `{member}` in `{category}` with {permissions}.") - await category.set_permissions(member, **permissions) - async def reset_send_permissions(self) -> None: """Reset send permissions in the Available category for claimants.""" log.trace("Resetting send permissions in the Available category.") @@ -640,8 +634,7 @@ class HelpChannels(Scheduler, commands.Cog): for member in guild.members: if self.is_claimant(member): log.trace(f"Resetting send permissions for {member} ({member.id}).") - role = discord.Object(constants.Roles.help_cooldown) - await member.remove_roles(role) + await member.remove_roles(COOLDOWN_ROLE) async def reset_claimant_send_permission(self, channel: discord.TextChannel) -> None: """Reset send permissions in the Available category for the help `channel` claimant.""" @@ -649,11 +642,15 @@ class HelpChannels(Scheduler, commands.Cog): try: member = self.help_channel_claimants[channel] except KeyError: - log.trace(f"Channel #{channel.name} ({channel.id}) not in claimant cache, permissions unchanged.") + log.trace( + f"Channel #{channel.name} ({channel.id}) not in claimant cache, " + f"permissions unchanged." + ) return log.trace(f"Resetting send permissions for {member} ({member.id}).") - await self.update_category_permissions(self.available_category, member, overwrite=None) + await member.remove_roles(COOLDOWN_ROLE) + # Ignore missing task when claim cooldown has passed but the channel still isn't dormant. self.cancel_task(member.id, ignore_missing=True) @@ -668,14 +665,14 @@ class HelpChannels(Scheduler, commands.Cog): f"Revoking {member}'s ({member.id}) send message permissions in the Available category." ) - await self.update_category_permissions(self.available_category, member, send_messages=False) + await member.add_roles(COOLDOWN_ROLE) # Cancel the existing task, if any. # Would mean the user somehow bypassed the lack of permissions (e.g. user is guild owner). self.cancel_task(member.id, ignore_missing=True) timeout = constants.HelpChannels.claim_minutes * 60 - callback = self.update_category_permissions(self.available_category, member, overwrite=None) + callback = member.remove_roles(COOLDOWN_ROLE) log.trace(f"Scheduling {member}'s ({member.id}) send message permissions to be reinstated.") self.schedule_task(member.id, TaskData(timeout, callback)) -- cgit v1.2.3 From 244b23a4d36f0117e7a385979a1b03e4534cffb4 Mon Sep 17 00:00:00 2001 From: MarkKoz Date: Fri, 17 Apr 2020 09:23:57 -0700 Subject: HelpChannels: add info about cooldown role & dormant cmd to docstring --- bot/cogs/help_channels.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/bot/cogs/help_channels.py b/bot/cogs/help_channels.py index 47e74a2e5..589342098 100644 --- a/bot/cogs/help_channels.py +++ b/bot/cogs/help_channels.py @@ -89,12 +89,15 @@ class HelpChannels(Scheduler, commands.Cog): * If there are no more dormant channels, the bot will automatically create a new one * If there are no dormant channels to move, helpers will be notified (see `notify()`) * When a channel becomes available, the dormant embed will be edited to show `AVAILABLE_MSG` + * User can only claim a channel at an interval `constants.HelpChannels.claim_minutes` + * To keep track of cooldowns, user which claimed a channel will have a temporary role In Use Category * Contains all channels which are occupied by someone needing help * Channel moves to dormant category after `constants.HelpChannels.idle_minutes` of being idle * Command can prematurely mark a channel as dormant + * Channel claimant is allowed to use the command * Allowed roles for the command are configurable with `constants.HelpChannels.cmd_whitelist` * When a channel becomes dormant, an embed with `DORMANT_MSG` will be sent -- cgit v1.2.3 From 96a736b037bb0cb5aef6a381520e15fdb50676dc Mon Sep 17 00:00:00 2001 From: MarkKoz Date: Fri, 17 Apr 2020 09:27:27 -0700 Subject: HelpChannels: mention dormant cmd in available message embed Users should know they can close their own channels. --- bot/cogs/help_channels.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/bot/cogs/help_channels.py b/bot/cogs/help_channels.py index 589342098..149808473 100644 --- a/bot/cogs/help_channels.py +++ b/bot/cogs/help_channels.py @@ -40,8 +40,9 @@ channels in the Help: Available category. AVAILABLE_MSG = f""" This help channel is now **available**, which means that you can claim it by simply typing your \ question into it. Once claimed, the channel will move into the **Python Help: Occupied** category, \ -and will be yours until it has been inactive for {constants.HelpChannels.idle_minutes} minutes. When \ -that happens, it will be set to **dormant** and moved into the **Help: Dormant** category. +and will be yours until it has been inactive for {constants.HelpChannels.idle_minutes} minutes or \ +is closed manually with `!close`. When that happens, it will be set to **dormant** and moved into \ +the **Help: Dormant** category. You may claim a new channel once every {constants.HelpChannels.claim_minutes} minutes. If you \ currently cannot send a message in this channel, it means you are on cooldown and need to wait. -- cgit v1.2.3 From b209700d7e8d882b2ff3f4ca097c3644d089920c Mon Sep 17 00:00:00 2001 From: MarkKoz Date: Fri, 17 Apr 2020 09:59:50 -0700 Subject: HelpChannels: fix role not resetting after dormant command Resetting permissions relied on getting the member from the cache, but the member was already removed from the cache prior to resetting the role. Now the member is passed directly rather than relying on the cache. --- bot/cogs/help_channels.py | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/bot/cogs/help_channels.py b/bot/cogs/help_channels.py index 149808473..b4fc901cc 100644 --- a/bot/cogs/help_channels.py +++ b/bot/cogs/help_channels.py @@ -230,7 +230,7 @@ class HelpChannels(Scheduler, commands.Cog): del self.help_channel_claimants[ctx.channel] with suppress(discord.errors.HTTPException, discord.errors.NotFound): - await self.reset_claimant_send_permission(ctx.channel) + await self.reset_claimant_send_permission(ctx.author) await self.move_to_dormant(ctx.channel, "command") self.cancel_task(ctx.channel.id) @@ -640,18 +640,8 @@ class HelpChannels(Scheduler, commands.Cog): log.trace(f"Resetting send permissions for {member} ({member.id}).") await member.remove_roles(COOLDOWN_ROLE) - async def reset_claimant_send_permission(self, channel: discord.TextChannel) -> None: - """Reset send permissions in the Available category for the help `channel` claimant.""" - log.trace(f"Attempting to find claimant for #{channel.name} ({channel.id}).") - try: - member = self.help_channel_claimants[channel] - except KeyError: - log.trace( - f"Channel #{channel.name} ({channel.id}) not in claimant cache, " - f"permissions unchanged." - ) - return - + async def reset_claimant_send_permission(self, member: discord.Member) -> None: + """Reset send permissions in the Available category for `member`.""" log.trace(f"Resetting send permissions for {member} ({member.id}).") await member.remove_roles(COOLDOWN_ROLE) -- cgit v1.2.3 From 2b844d8bfbd686f1a56f1efc00dcca4558698016 Mon Sep 17 00:00:00 2001 From: MarkKoz Date: Fri, 17 Apr 2020 11:14:05 -0700 Subject: HelpChannels: handle errors when changing cooldown role A user may leave the guild before their role can be changed. Sometimes, there could also be role hierarchy issues or other network issues. It's not productive to halt everything and just dump these as exceptions to the loggers. The error handler provides a more graceful approach to these exceptions. * Add a wrapper function around `add_roles` & `remove_roles` which catches exceptions --- bot/cogs/help_channels.py | 47 +++++++++++++++++++++++++++++++++++------------ 1 file changed, 35 insertions(+), 12 deletions(-) diff --git a/bot/cogs/help_channels.py b/bot/cogs/help_channels.py index b4fc901cc..c70cb6ffb 100644 --- a/bot/cogs/help_channels.py +++ b/bot/cogs/help_channels.py @@ -229,8 +229,9 @@ class HelpChannels(Scheduler, commands.Cog): with suppress(KeyError): del self.help_channel_claimants[ctx.channel] - with suppress(discord.errors.HTTPException, discord.errors.NotFound): - await self.reset_claimant_send_permission(ctx.author) + await self.remove_cooldown_role(ctx.author) + # Ignore missing task when cooldown has passed but the channel still isn't dormant. + self.cancel_task(ctx.author.id, ignore_missing=True) await self.move_to_dormant(ctx.channel, "command") self.cancel_task(ctx.channel.id) @@ -637,16 +638,38 @@ class HelpChannels(Scheduler, commands.Cog): # TODO: replace with a persistent cache cause checking every member is quite slow for member in guild.members: if self.is_claimant(member): - log.trace(f"Resetting send permissions for {member} ({member.id}).") - await member.remove_roles(COOLDOWN_ROLE) + await self.remove_cooldown_role(member) - async def reset_claimant_send_permission(self, member: discord.Member) -> None: - """Reset send permissions in the Available category for `member`.""" - log.trace(f"Resetting send permissions for {member} ({member.id}).") - await member.remove_roles(COOLDOWN_ROLE) + @classmethod + async def add_cooldown_role(cls, member: discord.Member) -> None: + """Add the help cooldown role to `member`.""" + log.trace(f"Adding cooldown role for {member} ({member.id}).") + await cls._change_cooldown_role(member, member.add_roles(COOLDOWN_ROLE)) - # Ignore missing task when claim cooldown has passed but the channel still isn't dormant. - self.cancel_task(member.id, ignore_missing=True) + @classmethod + async def remove_cooldown_role(cls, member: discord.Member) -> None: + """Remove the help cooldown role from `member`.""" + log.trace(f"Removing cooldown role for {member} ({member.id}).") + await cls._change_cooldown_role(member, member.remove_roles(COOLDOWN_ROLE)) + + @staticmethod + async def _change_cooldown_role(member: discord.Member, coro: t.Awaitable) -> None: + """ + Change `member`'s cooldown role via awaiting `coro` and handle errors. + + `coro` is intended to be `discord.Member.add_roles` or `discord.Member.remove_roles`. + """ + try: + await coro + except discord.NotFound: + log.debug(f"Failed to change role for {member} ({member.id}): member not found") + except discord.Forbidden: + log.debug( + f"Forbidden to change role for {member} ({member.id}); " + f"possibly due to role hierarchy" + ) + except discord.HTTPException as e: + log.error(f"Failed to change role for {member} ({member.id}): {e.status} {e.code}") async def revoke_send_permissions(self, member: discord.Member) -> None: """ @@ -659,14 +682,14 @@ class HelpChannels(Scheduler, commands.Cog): f"Revoking {member}'s ({member.id}) send message permissions in the Available category." ) - await member.add_roles(COOLDOWN_ROLE) + await self.add_cooldown_role(member) # Cancel the existing task, if any. # Would mean the user somehow bypassed the lack of permissions (e.g. user is guild owner). self.cancel_task(member.id, ignore_missing=True) timeout = constants.HelpChannels.claim_minutes * 60 - callback = member.remove_roles(COOLDOWN_ROLE) + callback = self.remove_cooldown_role(member) log.trace(f"Scheduling {member}'s ({member.id}) send message permissions to be reinstated.") self.schedule_task(member.id, TaskData(timeout, callback)) -- cgit v1.2.3 From 383f5a71e6c941eaa932db7017fb1be27efb0e95 Mon Sep 17 00:00:00 2001 From: MarkKoz Date: Mon, 20 Apr 2020 11:21:14 -0700 Subject: HelpChannels: tidy up log messages * Remove obsolete log message * Shorten a log message which was the only line in the entire module over 100 characters --- bot/cogs/help_channels.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/bot/cogs/help_channels.py b/bot/cogs/help_channels.py index c70cb6ffb..875eb5330 100644 --- a/bot/cogs/help_channels.py +++ b/bot/cogs/help_channels.py @@ -269,7 +269,7 @@ class HelpChannels(Scheduler, commands.Cog): log.trace(f"The clean name for `{channel}` is `{name}`") except ValueError: # If, for some reason, the channel name does not contain "help-" fall back gracefully - log.info(f"Can't get clean name as `{channel}` does not follow the `{prefix}` naming convention.") + log.info(f"Can't get clean name because `{channel}` isn't prefixed by `{prefix}`.") name = channel.name return name @@ -488,10 +488,6 @@ class HelpChannels(Scheduler, commands.Cog): topic=AVAILABLE_TOPIC, ) - log.trace( - f"Ensuring that all channels in `{self.available_category}` have " - f"synchronized permissions after moving `{channel}` into it." - ) self.report_stats() async def move_to_dormant(self, channel: discord.TextChannel, caller: str) -> None: -- cgit v1.2.3 From b05b70453b5fc9f79b9434a8d9f9e49db7837856 Mon Sep 17 00:00:00 2001 From: MarkKoz Date: Mon, 20 Apr 2020 11:44:05 -0700 Subject: HelpChannels: pass coroutine func instead to `_change_cooldown_role` This will allow `_change_cooldown_role` to handle the role argument rather than putting that burden on the callers. --- bot/cogs/help_channels.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/bot/cogs/help_channels.py b/bot/cogs/help_channels.py index 875eb5330..30ef56f56 100644 --- a/bot/cogs/help_channels.py +++ b/bot/cogs/help_channels.py @@ -67,6 +67,8 @@ AVAILABLE_EMOJI = "✅" IN_USE_EMOJI = "⌛" NAME_SEPARATOR = "|" +CoroutineFunc = t.Callable[..., t.Coroutine] + class TaskData(t.NamedTuple): """Data for a scheduled task.""" @@ -640,23 +642,23 @@ class HelpChannels(Scheduler, commands.Cog): async def add_cooldown_role(cls, member: discord.Member) -> None: """Add the help cooldown role to `member`.""" log.trace(f"Adding cooldown role for {member} ({member.id}).") - await cls._change_cooldown_role(member, member.add_roles(COOLDOWN_ROLE)) + await cls._change_cooldown_role(member, member.add_roles) @classmethod async def remove_cooldown_role(cls, member: discord.Member) -> None: """Remove the help cooldown role from `member`.""" log.trace(f"Removing cooldown role for {member} ({member.id}).") - await cls._change_cooldown_role(member, member.remove_roles(COOLDOWN_ROLE)) + await cls._change_cooldown_role(member, member.remove_roles) @staticmethod - async def _change_cooldown_role(member: discord.Member, coro: t.Awaitable) -> None: + async def _change_cooldown_role(member: discord.Member, coro_func: CoroutineFunc) -> None: """ - Change `member`'s cooldown role via awaiting `coro` and handle errors. + Change `member`'s cooldown role via awaiting `coro_func` and handle errors. - `coro` is intended to be `discord.Member.add_roles` or `discord.Member.remove_roles`. + `coro_func` is intended to be `discord.Member.add_roles` or `discord.Member.remove_roles`. """ try: - await coro + await coro_func(COOLDOWN_ROLE) except discord.NotFound: log.debug(f"Failed to change role for {member} ({member.id}): member not found") except discord.Forbidden: -- cgit v1.2.3 From 7bb69f8ef15f03d355dc114181ce27df5aee7cfd Mon Sep 17 00:00:00 2001 From: MarkKoz Date: Mon, 20 Apr 2020 11:58:43 -0700 Subject: HelpChannels: check if the help cooldown role exists A NotFound error can be misleading since it may apply to the member or the role. The log message was not simply updated because each of the scenarios need to have different log levels: missing members is a normal thing but an invalid role is not. --- bot/cogs/help_channels.py | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/bot/cogs/help_channels.py b/bot/cogs/help_channels.py index 30ef56f56..5a1495a4d 100644 --- a/bot/cogs/help_channels.py +++ b/bot/cogs/help_channels.py @@ -21,7 +21,6 @@ log = logging.getLogger(__name__) ASKING_GUIDE_URL = "https://pythondiscord.com/pages/asking-good-questions/" MAX_CHANNELS_PER_CATEGORY = 50 -COOLDOWN_ROLE = discord.Object(constants.Roles.help_cooldown) AVAILABLE_TOPIC = """ This channel is available. Feel free to ask a question in order to claim this channel! @@ -638,27 +637,30 @@ class HelpChannels(Scheduler, commands.Cog): if self.is_claimant(member): await self.remove_cooldown_role(member) - @classmethod - async def add_cooldown_role(cls, member: discord.Member) -> None: + async def add_cooldown_role(self, member: discord.Member) -> None: """Add the help cooldown role to `member`.""" log.trace(f"Adding cooldown role for {member} ({member.id}).") - await cls._change_cooldown_role(member, member.add_roles) + await self._change_cooldown_role(member, member.add_roles) - @classmethod - async def remove_cooldown_role(cls, member: discord.Member) -> None: + async def remove_cooldown_role(self, member: discord.Member) -> None: """Remove the help cooldown role from `member`.""" log.trace(f"Removing cooldown role for {member} ({member.id}).") - await cls._change_cooldown_role(member, member.remove_roles) + await self._change_cooldown_role(member, member.remove_roles) - @staticmethod - async def _change_cooldown_role(member: discord.Member, coro_func: CoroutineFunc) -> None: + async def _change_cooldown_role(self, member: discord.Member, coro_func: CoroutineFunc) -> None: """ Change `member`'s cooldown role via awaiting `coro_func` and handle errors. `coro_func` is intended to be `discord.Member.add_roles` or `discord.Member.remove_roles`. """ + guild = self.bot.get_guild(constants.Guild.id) + role = guild.get_role(constants.Roles.help_cooldown) + if role is None: + log.warning(f"Help cooldown role ({constants.Roles.help_cooldown}) could not be found!") + return + try: - await coro_func(COOLDOWN_ROLE) + await coro_func(role) except discord.NotFound: log.debug(f"Failed to change role for {member} ({member.id}): member not found") except discord.Forbidden: -- cgit v1.2.3 From 7b0cba07953f7a74a0a0b57dfb5f38299adcdccd Mon Sep 17 00:00:00 2001 From: MarkKoz Date: Mon, 20 Apr 2020 13:47:12 -0700 Subject: HelpChannels: rename dormant command to close People are more familiar with the "close" alias than its actual name, "dormant". "close" also feels more natural. --- bot/cogs/help_channels.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/bot/cogs/help_channels.py b/bot/cogs/help_channels.py index 5a1495a4d..75f907602 100644 --- a/bot/cogs/help_channels.py +++ b/bot/cogs/help_channels.py @@ -215,8 +215,8 @@ class HelpChannels(Scheduler, commands.Cog): return role_check - @commands.command(name="dormant", aliases=["close"], enabled=False) - async def dormant_command(self, ctx: commands.Context) -> None: + @commands.command(name="close", aliases=["dormant"], enabled=False) + async def close_command(self, ctx: commands.Context) -> None: """ Make the current in-use help channel dormant. @@ -224,7 +224,7 @@ class HelpChannels(Scheduler, commands.Cog): delete the message that invoked this, and reset the send permissions cooldown for the user who started the session. """ - log.trace("dormant command invoked; checking if the channel is in-use.") + log.trace("close command invoked; checking if the channel is in-use.") if ctx.channel.category == self.in_use_category: if await self.dormant_check(ctx): with suppress(KeyError): @@ -400,7 +400,7 @@ class HelpChannels(Scheduler, commands.Cog): # The ready event wasn't used because channels could change categories between the time # the command is invoked and the cog is ready (e.g. if move_idle_channel wasn't called yet). # This may confuse users. So would potentially long delays for the cog to become ready. - self.dormant_command.enabled = True + self.close_command.enabled = True await self.init_available() -- cgit v1.2.3