Skip to content

Commit 3ee0844

Browse files
committed
✨ Better typing in cooldown.py
1 parent add4c16 commit 3ee0844

File tree

2 files changed

+11
-10
lines changed

2 files changed

+11
-10
lines changed

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ extend-ignore = [
105105
"FBT001",
106106
"FBT002",
107107
"PLR2004",
108+
"PLR0913",
108109
"C901"
109110
]
110111

src/utils/cooldown.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@
88
from inspect import isawaitable
99
from typing import Any, Concatenate, cast
1010

11+
import discord
1112
from discord.ext import commands
1213

1314
from src import custom
1415

1516
type ReactiveCooldownSetting[T: Any] = T | Callable[[custom.Bot, custom.Context], T | Coroutine[Any, Any, T]]
16-
type CogCommandFunction[T: commands.Cog, **P] = Callable[Concatenate[T, custom.ApplicationContext, P], Awaitable[None]]
17+
type CogCommandFunction[T: commands.Cog, **P] = Callable[Concatenate[T, P], Awaitable[None]]
1718

1819

1920
class BucketType(Enum):
@@ -43,32 +44,30 @@ def __init__(self, retry_after: float, bucket_type: BucketType) -> None:
4344
super().__init__(f"You are on {bucket_type.value} cooldown")
4445

4546

46-
def get_bucket_key(ctx: custom.ApplicationContext, base_key: str, bucket_type: BucketType) -> str: # noqa: PLR0911
47+
def get_bucket_key(ctx: custom.Context, base_key: str, bucket_type: BucketType) -> str: # noqa: PLR0911
4748
"""Generate a cooldown key based on the bucket type."""
4849
match bucket_type:
4950
case BucketType.USER:
5051
return f"{base_key}:user:{ctx.author.id}"
5152
case BucketType.MEMBER:
52-
return (
53-
f"{base_key}:member:{ctx.guild_id}:{ctx.author.id}" if ctx.guild else f"{base_key}:user:{ctx.author.id}"
54-
)
53+
return f"{base_key}:member:{ctx.guild}:{ctx.author.id}" if ctx.guild else f"{base_key}:user:{ctx.author.id}"
5554
case BucketType.GUILD:
56-
return f"{base_key}:guild:{ctx.guild_id}" if ctx.guild else base_key
55+
return f"{base_key}:guild:{ctx.guild.id}" if ctx.guild else base_key
5756
case BucketType.CHANNEL:
5857
return f"{base_key}:channel:{ctx.channel.id}"
5958
case BucketType.CATEGORY:
6059
category_id = ctx.channel.category_id if hasattr(ctx.channel, "category_id") else None
6160
return f"{base_key}:category:{category_id}" if category_id else f"{base_key}:channel:{ctx.channel.id}"
6261
case BucketType.ROLE:
63-
if ctx.guild and hasattr(ctx.author, "roles"):
62+
if ctx.guild and hasattr(ctx.author, "roles") and isinstance(ctx.author, discord.Member):
6463
top_role_id = max((role.id for role in ctx.author.roles), default=0)
6564
return f"{base_key}:role:{top_role_id}"
6665
return f"{base_key}:user:{ctx.author.id}"
6766
case _: # BucketType.DEFAULT
6867
return base_key
6968

7069

71-
def cooldown[C: commands.Cog, **P]( # noqa: PLR0913
70+
def cooldown[C: commands.Cog, **P](
7271
key: ReactiveCooldownSetting[str],
7372
*,
7473
limit: ReactiveCooldownSetting[int],
@@ -91,7 +90,8 @@ def cooldown[C: commands.Cog, **P]( # noqa: PLR0913
9190

9291
def inner(func: CogCommandFunction[C, P]) -> CogCommandFunction[C, P]:
9392
@wraps(func)
94-
async def wrapper(self: C, ctx: custom.ApplicationContext, *args: P.args, **kwargs: P.kwargs) -> None:
93+
async def wrapper(self: C, *args: P.args, **kwargs: P.kwargs) -> None:
94+
ctx: custom.Context = args[0] # pyright: ignore [reportAssignmentType]
9595
cache = ctx.bot.botkit_cache
9696
key_value: str = await parse_reactive_setting(key, ctx.bot, ctx)
9797
limit_value: int = await parse_reactive_setting(limit, ctx.bot, ctx)
@@ -116,7 +116,7 @@ async def wrapper(self: C, ctx: custom.ApplicationContext, *args: P.args, **kwar
116116
if len(time_stamps) >= limit_value:
117117
raise cls_value(min(time_stamps) - now + per_value, bucket_type_value)
118118

119-
await func(self, ctx, *args, **kwargs)
119+
await func(self, *args, **kwargs)
120120

121121
return wrapper
122122

0 commit comments

Comments
 (0)