Skip to content

Commit

Permalink
fix: Linting
Browse files Browse the repository at this point in the history
  • Loading branch information
DA-344 committed Feb 3, 2025
1 parent 5a99a35 commit 689e9bf
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 14 deletions.
9 changes: 6 additions & 3 deletions discord_tools/app_commands/checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ def max_concurrency(number: int, per: BucketType = BucketType.default):
def decorator(func):
async def predicate(interaction: Interaction[Any]) -> bool:
if interaction.command is None:
obj = getattr(func, '__discord_app_commands_max_concurrency__', None)
obj = getattr(func, "__discord_app_commands_max_concurrency__", None)
else:
obj = interaction.command.extras.get('__max_concurrency__')
obj = interaction.command.extras.get("__max_concurrency__")

if not isinstance(obj, MaxConcurrency):
return True
Expand All @@ -117,6 +117,9 @@ async def predicate(interaction: Interaction[Any]) -> bool:
if isinstance(func, (Command, Group)):
func.extras["__max_concurrency__"] = MaxConcurrency(number, per=per)
else:
func.__discord_app_commands_max_concurrency__ = MaxConcurrency(number, per=per)
func.__discord_app_commands_max_concurrency__ = MaxConcurrency(
number, per=per
)
return check(predicate)(func)

return decorator
11 changes: 6 additions & 5 deletions discord_tools/app_commands/transformers.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from __future__ import annotations

from functools import partial
from typing import TypeVar, Generic, Any
from typing import TypeVar, Any

import discord
from discord.app_commands import Transformer
Expand All @@ -52,16 +52,17 @@ class Greedy(Transformer):
.. versionadded:: 1.0
"""

def __init__(self, converter: T) -> None: # pyright: ignore[reportInvalidTypeVarUse]
def __init__(
self,
converter: T, # pyright: ignore[reportInvalidTypeVarUse]
) -> None:
if converter not in CONVERTER_MAPPING and not is_generic_type(converter):
raise ValueError(
f"Cannot set the Greedy converter to {converter.__class__.__name__}"
)
self._converter: T = converter

async def transform(
self, interaction: discord.Interaction, argument: str
) -> Any:
async def transform(self, interaction: discord.Interaction, argument: str) -> Any:
ctx = await Context.from_interaction(interaction) # type: ignore
assert ctx.current_parameter is not None
view = ctx.view.__class__(argument)
Expand Down
20 changes: 14 additions & 6 deletions examples/app_commands/max_concurrency.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,32 +20,40 @@
# we define a command
@tree.command()
# and we apply our max_concurrency decorator, limiting to 1 usage per user
@discord_tools.app_commands.max_concurrency(1, per=discord_tools.app_commands.BucketType.user)
@discord_tools.app_commands.max_concurrency(
1, per=discord_tools.app_commands.BucketType.user
)
async def some_long_operation(interaction: discord.Interaction) -> None:
await interaction.response.defer() # we defer so we can respond in more than 3 seconds
await asyncio.sleep(10) # simulate some long operation: db calls, web requests, etc.
await interaction.followup.send('Completed!') # we tell the user the operation has finished
await asyncio.sleep(
10
) # simulate some long operation: db calls, web requests, etc.
await interaction.followup.send(
"Completed!"
) # we tell the user the operation has finished


# we now define the error handling for our command
# this can be done in different ways, but in this case, we will use
# the @.error decorator
@some_long_operation.error
async def some_long_operation_error(interaction: discord.Interaction, error: discord.app_commands.AppCommandError):
async def some_long_operation_error(
interaction: discord.Interaction, error: discord.app_commands.AppCommandError
):
# we now check if our error is the one that max_concurrency raises
if isinstance(error, discord_tools.app_commands.MaxConcurrencyReached):
# now we tell the user that they have reached maximum concurrency in X bucket

# if the interaction has not been responded, we respond to it
if not interaction.response.is_done():
await interaction.response.send_message(
f'You have reached the {error.number} per {error.per.name} command usage',
f"You have reached the {error.number} per {error.per.name} command usage",
ephemeral=True,
)
# if it has, then we send a followup
else:
await interaction.followup.send(
f'You have reached the {error.number} per {error.per.name} command usage',
f"You have reached the {error.number} per {error.per.name} command usage",
ephemeral=True,
)
else:
Expand Down

0 comments on commit 689e9bf

Please sign in to comment.