From 2dab45c84f4454ccf2c2a2efd7e819e06f1551c0 Mon Sep 17 00:00:00 2001 From: Eneg <42005170+Enegg@users.noreply.github.com> Date: Fri, 29 Nov 2024 13:16:31 +0100 Subject: [PATCH] fix(cooldown): `BucketType.role` could be based on a `Thread` rather than `Role` (#1200) Signed-off-by: Eneg <42005170+Enegg@users.noreply.github.com> --- changelog/1200.bugfix.rst | 1 + disnake/ext/commands/cooldowns.py | 10 ++++------ 2 files changed, 5 insertions(+), 6 deletions(-) create mode 100644 changelog/1200.bugfix.rst diff --git a/changelog/1200.bugfix.rst b/changelog/1200.bugfix.rst new file mode 100644 index 0000000000..8d8387af1b --- /dev/null +++ b/changelog/1200.bugfix.rst @@ -0,0 +1 @@ +|commands| Fix usage of :attr:`~ext.commands.BucketType.role`\-type cooldowns in threads, which incorrectly operated on a per-channel basis instead. diff --git a/disnake/ext/commands/cooldowns.py b/disnake/ext/commands/cooldowns.py index 354754550a..cda127398a 100644 --- a/disnake/ext/commands/cooldowns.py +++ b/disnake/ext/commands/cooldowns.py @@ -7,8 +7,8 @@ from collections import deque from typing import TYPE_CHECKING, Any, Callable, Deque, Dict, Optional -from disnake.abc import PrivateChannel from disnake.enums import Enum +from disnake.member import Member from .errors import MaxConcurrencyReached @@ -47,11 +47,9 @@ def get_key(self, msg: Message) -> Any: elif self is BucketType.category: return (msg.channel.category or msg.channel).id # type: ignore elif self is BucketType.role: - # we return the channel id of a private-channel as there are only roles in guilds - # and that yields the same result as for a guild with only the @everyone role - # NOTE: PrivateChannel doesn't actually have an id attribute but we assume we are - # recieving a DMChannel or GroupChannel which inherit from PrivateChannel and do - return (msg.channel if isinstance(msg.channel, PrivateChannel) else msg.author.top_role).id # type: ignore + # if author is not a Member we are in a private-channel context; returning its id + # yields the same result as for a guild with only the @everyone role + return (msg.author.top_role if isinstance(msg.author, Member) else msg.channel).id def __call__(self, msg: Message) -> Any: return self.get_key(msg)