Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
elenakrittik committed Jun 13, 2023
1 parent 425a780 commit 85f55fc
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 51 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,14 @@ venvs/
.coverage*
coverage.xml
__pypackages__/
# .pdm.toml - pdm config used & produced by pdm <=2.4
.pdm.toml
# since .pdm.toml was an alternative to pyproject.toml
# store of pdm config (and also often only contained local
# python path), it was renamed to pdm.toml (which, unlike
# .pdm.toml, should be commited to VCS) and python path from it
# was extracted into a single-line .pdm-python since pdm 2.5.0b
.pdm-python
pdm.lock

!test_bot/locale/*.json
8 changes: 8 additions & 0 deletions disnake/ext/commands/_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from typing import TYPE_CHECKING, Any, Callable, Coroutine, TypeVar, Union

if TYPE_CHECKING:
from disnake import ApplicationCommandInteraction as AppCmdInter

from .cog import Cog
from .context import Context
from .errors import CommandError
Expand All @@ -13,6 +15,12 @@
MaybeCoro = Union[T, Coro[T]]
CoroFunc = Callable[..., Coro[Any]]

# This is explicitly "Global" to highlight the fact that this type
# should not be used with per-command checks
GlobalAppCheck = Callable[["AppCmdInter"], MaybeCoro[bool]]
AppCheck = Union[
Callable[["Cog", "AppCmdInter"], MaybeCoro[bool]], Callable[["AppCmdInter"], MaybeCoro[bool]]
]
Check = Union[
Callable[["Cog", "Context[Any]"], MaybeCoro[bool]], Callable[["Context[Any]"], MaybeCoro[bool]]
]
Expand Down
8 changes: 4 additions & 4 deletions disnake/ext/commands/base_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

from disnake.interactions import ApplicationCommandInteraction

from ._types import Check, Coro, Error, Hook
from ._types import AppCheck, Coro, Error, Hook
from .cog import Cog

ApplicationCommandInteractionT = TypeVar(
Expand Down Expand Up @@ -155,7 +155,7 @@ def __init__(self, func: CommandCallback, *, name: Optional[str] = None, **kwarg
except AttributeError:
checks = kwargs.get("checks", [])

self.checks: List[Check] = checks
self.checks: List[AppCheck] = checks

try:
cooldown = func.__commands_cooldown__
Expand Down Expand Up @@ -253,7 +253,7 @@ def default_member_permissions(self) -> Optional[Permissions]:
def callback(self) -> CommandCallback:
return self._callback

def add_check(self, func: Check) -> None:
def add_check(self, func: AppCheck) -> None:
"""Adds a check to the application command.
This is the non-decorator interface to :func:`.check`.
Expand All @@ -265,7 +265,7 @@ def add_check(self, func: Check) -> None:
"""
self.checks.append(func)

def remove_check(self, func: Check) -> None:
def remove_check(self, func: AppCheck) -> None:
"""Removes a check from the application command.
This function is idempotent and will not raise an exception
Expand Down
10 changes: 5 additions & 5 deletions disnake/ext/commands/bot_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

from disnake.message import Message

from ._types import Check, CoroFunc, MaybeCoro
from ._types import AppCheck, CoroFunc, MaybeCoro

__all__ = (
"when_mentioned",
Expand Down Expand Up @@ -162,8 +162,8 @@ def __init__(

self.command_prefix = command_prefix

self._checks: List[Check] = []
self._check_once: List[Check] = []
self._checks: List[AppCheck] = []
self._check_once: List[AppCheck] = []

self._before_invoke: Optional[CoroFunc] = None
self._after_invoke: Optional[CoroFunc] = None
Expand Down Expand Up @@ -211,7 +211,7 @@ async def on_command_error(self, context: Context, exception: errors.CommandErro

def add_check(
self,
func: Check,
func: AppCheck,
*,
call_once: bool = False,
) -> None:
Expand All @@ -236,7 +236,7 @@ def add_check(

def remove_check(
self,
func: Check,
func: AppCheck,
*,
call_once: bool = False,
) -> None:
Expand Down
24 changes: 12 additions & 12 deletions disnake/ext/commands/cog.py
Original file line number Diff line number Diff line change
Expand Up @@ -787,30 +787,30 @@ def _inject(self, bot: AnyBot) -> Self:

# Add application command checks
if cls.bot_slash_command_check is not Cog.bot_slash_command_check:
bot.add_app_command_check(self.bot_slash_command_check, slash_commands=True) # type: ignore
bot.add_app_command_check(self.bot_slash_command_check, slash_commands=True)

if cls.bot_user_command_check is not Cog.bot_user_command_check:
bot.add_app_command_check(self.bot_user_command_check, user_commands=True) # type: ignore
bot.add_app_command_check(self.bot_user_command_check, user_commands=True)

if cls.bot_message_command_check is not Cog.bot_message_command_check:
bot.add_app_command_check(self.bot_message_command_check, message_commands=True) # type: ignore
bot.add_app_command_check(self.bot_message_command_check, message_commands=True)

# Add app command one-off checks
if cls.bot_slash_command_check_once is not Cog.bot_slash_command_check_once:
bot.add_app_command_check(
self.bot_slash_command_check_once, # type: ignore
self.bot_slash_command_check_once,
call_once=True,
slash_commands=True,
)

if cls.bot_user_command_check_once is not Cog.bot_user_command_check_once:
bot.add_app_command_check(
self.bot_user_command_check_once, call_once=True, user_commands=True # type: ignore
self.bot_user_command_check_once, call_once=True, user_commands=True
)

if cls.bot_message_command_check_once is not Cog.bot_message_command_check_once:
bot.add_app_command_check(
self.bot_message_command_check_once, # type: ignore
self.bot_message_command_check_once,
call_once=True,
message_commands=True,
)
Expand Down Expand Up @@ -857,32 +857,32 @@ def _eject(self, bot: AnyBot) -> None:

# Remove application command checks
if cls.bot_slash_command_check is not Cog.bot_slash_command_check:
bot.remove_app_command_check(self.bot_slash_command_check, slash_commands=True) # type: ignore
bot.remove_app_command_check(self.bot_slash_command_check, slash_commands=True)

if cls.bot_user_command_check is not Cog.bot_user_command_check:
bot.remove_app_command_check(self.bot_user_command_check, user_commands=True) # type: ignore
bot.remove_app_command_check(self.bot_user_command_check, user_commands=True)

if cls.bot_message_command_check is not Cog.bot_message_command_check:
bot.remove_app_command_check(self.bot_message_command_check, message_commands=True) # type: ignore
bot.remove_app_command_check(self.bot_message_command_check, message_commands=True)

# Remove app command one-off checks
if cls.bot_slash_command_check_once is not Cog.bot_slash_command_check_once:
bot.remove_app_command_check(
self.bot_slash_command_check_once, # type: ignore
self.bot_slash_command_check_once,
call_once=True,
slash_commands=True,
)

if cls.bot_user_command_check_once is not Cog.bot_user_command_check_once:
bot.remove_app_command_check(
self.bot_user_command_check_once, # type: ignore
self.bot_user_command_check_once,
call_once=True,
user_commands=True,
)

if cls.bot_message_command_check_once is not Cog.bot_message_command_check_once:
bot.remove_app_command_check(
self.bot_message_command_check_once, # type: ignore
self.bot_message_command_check_once,
call_once=True,
message_commands=True,
)
Expand Down
51 changes: 21 additions & 30 deletions disnake/ext/commands/interaction_bot_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
)
from disnake.permissions import Permissions

from ._types import Check, CoroFunc
from ._types import CoroFunc, GlobalAppCheck
from .base_core import CogT, CommandCallback, InteractionCommandCallback

P = ParamSpec("P")
Expand Down Expand Up @@ -199,12 +199,12 @@ def __init__(
self._command_sync_flags = command_sync_flags
self._sync_queued: asyncio.Lock = asyncio.Lock()

self._slash_command_checks = []
self._slash_command_check_once = []
self._user_command_checks = []
self._user_command_check_once = []
self._message_command_checks = []
self._message_command_check_once = []
self._slash_command_checks: List[GlobalAppCheck] = []
self._slash_command_check_once: List[GlobalAppCheck] = []
self._user_command_checks: List[GlobalAppCheck] = []
self._user_command_check_once: List[GlobalAppCheck] = []
self._message_command_checks: List[GlobalAppCheck] = []
self._message_command_check_once: List[GlobalAppCheck] = []

self._before_slash_command_invoke = None
self._after_slash_command_invoke = None
Expand Down Expand Up @@ -991,7 +991,7 @@ async def on_message_command_error(

def add_app_command_check(
self,
func: Check,
func: GlobalAppCheck,
*,
call_once: bool = False,
slash_commands: bool = False,
Expand Down Expand Up @@ -1039,7 +1039,7 @@ def add_app_command_check(

def remove_app_command_check(
self,
func: Check,
func: GlobalAppCheck,
*,
call_once: bool = False,
slash_commands: bool = False,
Expand Down Expand Up @@ -1091,35 +1091,32 @@ def remove_app_command_check(
except ValueError:
pass

def slash_command_check(self, func: T) -> T:
def slash_command_check(self, func: GlobalAppCheck) -> GlobalAppCheck:
"""Similar to :meth:`.check` but for slash commands."""
# T was used instead of Check to ensure the type matches on return
self.add_app_command_check(func, slash_commands=True) # type: ignore
self.add_app_command_check(func, slash_commands=True)
return func

def slash_command_check_once(self, func: CFT) -> CFT:
def slash_command_check_once(self, func: GlobalAppCheck) -> GlobalAppCheck:
"""Similar to :meth:`.check_once` but for slash commands."""
self.add_app_command_check(func, call_once=True, slash_commands=True)
return func

def user_command_check(self, func: T) -> T:
def user_command_check(self, func: GlobalAppCheck) -> GlobalAppCheck:
"""Similar to :meth:`.check` but for user commands."""
# T was used instead of Check to ensure the type matches on return
self.add_app_command_check(func, user_commands=True) # type: ignore
self.add_app_command_check(func, user_commands=True)
return func

def user_command_check_once(self, func: CFT) -> CFT:
def user_command_check_once(self, func: GlobalAppCheck) -> GlobalAppCheck:
"""Similar to :meth:`.check_once` but for user commands."""
self.add_app_command_check(func, call_once=True, user_commands=True)
return func

def message_command_check(self, func: T) -> T:
def message_command_check(self, func: GlobalAppCheck) -> GlobalAppCheck:
"""Similar to :meth:`.check` but for message commands."""
# T was used instead of Check to ensure the type matches on return
self.add_app_command_check(func, message_commands=True) # type: ignore
self.add_app_command_check(func, message_commands=True)
return func

def message_command_check_once(self, func: CFT) -> CFT:
def message_command_check_once(self, func: GlobalAppCheck) -> GlobalAppCheck:
"""Similar to :meth:`.check_once` but for message commands."""
self.add_app_command_check(func, call_once=True, message_commands=True)
return func
Expand All @@ -1131,10 +1128,7 @@ def application_command_check(
slash_commands: bool = False,
user_commands: bool = False,
message_commands: bool = False,
) -> Callable[
[Callable[[ApplicationCommandInteraction], Any]],
Callable[[ApplicationCommandInteraction], Any],
]:
) -> Callable[[GlobalAppCheck], GlobalAppCheck]:
"""A decorator that adds a global application command check to the bot.
A global check is similar to a :func:`check` that is applied
Expand Down Expand Up @@ -1174,12 +1168,9 @@ def check_app_commands(inter):
user_commands = True
message_commands = True

def decorator(
func: Callable[[ApplicationCommandInteraction], Any]
) -> Callable[[ApplicationCommandInteraction], Any]:
# T was used instead of Check to ensure the type matches on return
def decorator(func: GlobalAppCheck) -> GlobalAppCheck:
self.add_app_command_check(
func, # type: ignore
func,
call_once=call_once,
slash_commands=slash_commands,
user_commands=user_commands,
Expand Down

0 comments on commit 85f55fc

Please sign in to comment.