Change some methods to use positional-only marker

Co-authored-by: Danny <Rapptz@users.noreply.github.com>
This commit is contained in:
Bryan Forbes
2022-03-22 17:52:25 -05:00
committed by GitHub
parent c6d113c843
commit 062f4d6f87
5 changed files with 369 additions and 79 deletions

View File

@ -92,10 +92,14 @@ T = TypeVar('T')
CFT = TypeVar('CFT', bound='CoroFunc')
def when_mentioned(bot: _Bot, msg: Message) -> List[str]:
def when_mentioned(bot: _Bot, msg: Message, /) -> List[str]:
"""A callable that implements a command prefix equivalent to being mentioned.
These are meant to be passed into the :attr:`.Bot.command_prefix` attribute.
.. versionchanged:: 2.0
``bot`` and ``msg`` parameters are now positional-only.
"""
# bot.user will never be None when this is called
return [f'<@{bot.user.id}> ', f'<@!{bot.user.id}> '] # type: ignore
@ -185,7 +189,7 @@ class BotBase(GroupMixin[None]):
# internal helpers
def dispatch(self, event_name: str, *args: Any, **kwargs: Any) -> None:
def dispatch(self, event_name: str, /, *args: Any, **kwargs: Any) -> None:
# super() will resolve to Client
super().dispatch(event_name, *args, **kwargs) # type: ignore
ev = 'on_' + event_name
@ -208,7 +212,7 @@ class BotBase(GroupMixin[None]):
await super().close() # type: ignore
async def on_command_error(self, context: Context[BotT], exception: errors.CommandError) -> None:
async def on_command_error(self, context: Context[BotT], exception: errors.CommandError, /) -> None:
"""|coro|
The default command error handler provided by the bot.
@ -217,6 +221,10 @@ class BotBase(GroupMixin[None]):
overridden to have a different implementation.
This only fires if you do not specify any listeners for command error.
.. versionchanged:: 2.0
``context`` and ``exception`` parameters are now positional-only.
"""
if self.extra_events.get('on_command_error', None):
return
@ -234,7 +242,7 @@ class BotBase(GroupMixin[None]):
# global check registration
def check(self, func: T) -> T:
def check(self, func: T, /) -> T:
r"""A decorator that adds a global check to the bot.
A global check is similar to a :func:`.check` that is applied
@ -258,6 +266,9 @@ class BotBase(GroupMixin[None]):
def check_commands(ctx):
return ctx.command.qualified_name in allowed_commands
.. versionchanged:: 2.0
``func`` parameter is now positional-only.
"""
# T was used instead of Check to ensure the type matches on return
self.add_check(func) # type: ignore
@ -312,7 +323,7 @@ class BotBase(GroupMixin[None]):
except ValueError:
pass
def check_once(self, func: CFT) -> CFT:
def check_once(self, func: CFT, /) -> CFT:
r"""A decorator that adds a "call once" global check to the bot.
Unlike regular global checks, this one is called only once
@ -346,11 +357,15 @@ class BotBase(GroupMixin[None]):
def whitelist(ctx):
return ctx.message.author.id in my_whitelist
.. versionchanged:: 2.0
``func`` parameter is now positional-only.
"""
self.add_check(func, call_once=True)
return func
async def can_run(self, ctx: Context[BotT], *, call_once: bool = False) -> bool:
async def can_run(self, ctx: Context[BotT], /, *, call_once: bool = False) -> bool:
data = self._check_once if call_once else self._checks
if len(data) == 0:
@ -359,7 +374,7 @@ class BotBase(GroupMixin[None]):
# type-checker doesn't distinguish between functions and methods
return await discord.utils.async_all(f(ctx) for f in data) # type: ignore
async def is_owner(self, user: User) -> bool:
async def is_owner(self, user: User, /) -> bool:
"""|coro|
Checks if a :class:`~discord.User` or :class:`~discord.Member` is the owner of
@ -372,6 +387,10 @@ class BotBase(GroupMixin[None]):
The function also checks if the application is team-owned if
:attr:`owner_ids` is not set.
.. versionchanged:: 2.0
``user`` parameter is now positional-only.
Parameters
-----------
user: :class:`.abc.User`
@ -397,7 +416,7 @@ class BotBase(GroupMixin[None]):
self.owner_id = owner_id = app.owner.id
return user.id == owner_id
def before_invoke(self, coro: CFT) -> CFT:
def before_invoke(self, coro: CFT, /) -> CFT:
"""A decorator that registers a coroutine as a pre-invoke hook.
A pre-invoke hook is called directly before the command is
@ -413,6 +432,10 @@ class BotBase(GroupMixin[None]):
without error. If any check or argument parsing procedures fail
then the hooks are not called.
.. versionchanged:: 2.0
``coro`` parameter is now positional-only.
Parameters
-----------
coro: :ref:`coroutine <coroutine>`
@ -429,7 +452,7 @@ class BotBase(GroupMixin[None]):
self._before_invoke = coro
return coro
def after_invoke(self, coro: CFT) -> CFT:
def after_invoke(self, coro: CFT, /) -> CFT:
r"""A decorator that registers a coroutine as a post-invoke hook.
A post-invoke hook is called directly after the command is
@ -446,6 +469,10 @@ class BotBase(GroupMixin[None]):
callback raising an error (i.e. :exc:`.CommandInvokeError`\).
This makes it ideal for clean-up scenarios.
.. versionchanged:: 2.0
``coro`` parameter is now positional-only.
Parameters
-----------
coro: :ref:`coroutine <coroutine>`
@ -464,9 +491,13 @@ class BotBase(GroupMixin[None]):
# listener registration
def add_listener(self, func: CoroFunc, name: str = MISSING) -> None:
def add_listener(self, func: CoroFunc, /, name: str = MISSING) -> None:
"""The non decorator alternative to :meth:`.listen`.
.. versionchanged:: 2.0
``func`` parameter is now positional-only.
Parameters
-----------
func: :ref:`coroutine <coroutine>`
@ -496,9 +527,13 @@ class BotBase(GroupMixin[None]):
else:
self.extra_events[name] = [func]
def remove_listener(self, func: CoroFunc, name: str = MISSING) -> None:
def remove_listener(self, func: CoroFunc, /, name: str = MISSING) -> None:
"""Removes a listener from the pool of listeners.
.. versionchanged:: 2.0
``func`` parameter is now positional-only.
Parameters
-----------
func
@ -1023,12 +1058,16 @@ class BotBase(GroupMixin[None]):
# command processing
async def get_prefix(self, message: Message) -> Union[List[str], str]:
async def get_prefix(self, message: Message, /) -> Union[List[str], str]:
"""|coro|
Retrieves the prefix the bot is listening to
with the message as a context.
.. versionchanged:: 2.0
``message`` parameter is now positional-only.
Parameters
-----------
message: :class:`discord.Message`
@ -1068,6 +1107,7 @@ class BotBase(GroupMixin[None]):
async def get_context(
self,
message: Message,
/,
) -> Context[Self]: # type: ignore
...
@ -1075,6 +1115,7 @@ class BotBase(GroupMixin[None]):
async def get_context(
self,
message: Message,
/,
*,
cls: Type[ContextT] = ...,
) -> ContextT:
@ -1083,6 +1124,7 @@ class BotBase(GroupMixin[None]):
async def get_context(
self,
message: Message,
/,
*,
cls: Type[ContextT] = MISSING,
) -> Any:
@ -1098,6 +1140,10 @@ class BotBase(GroupMixin[None]):
If the context is not valid then it is not a valid candidate to be
invoked under :meth:`~.Bot.invoke`.
.. versionchanged:: 2.0
``message`` parameter is now positional-only.
Parameters
-----------
message: :class:`discord.Message`
@ -1165,12 +1211,16 @@ class BotBase(GroupMixin[None]):
ctx.command = self.all_commands.get(invoker)
return ctx
async def invoke(self, ctx: Context[BotT]) -> None:
async def invoke(self, ctx: Context[BotT], /) -> None:
"""|coro|
Invokes the command given under the invocation context and
handles all the internal event dispatch mechanisms.
.. versionchanged:: 2.0
``ctx`` parameter is now positional-only.
Parameters
-----------
ctx: :class:`.Context`
@ -1191,7 +1241,7 @@ class BotBase(GroupMixin[None]):
exc = errors.CommandNotFound(f'Command "{ctx.invoked_with}" is not found')
self.dispatch('command_error', ctx, exc)
async def process_commands(self, message: Message) -> None:
async def process_commands(self, message: Message, /) -> None:
"""|coro|
This function processes the commands that have been registered
@ -1208,6 +1258,10 @@ class BotBase(GroupMixin[None]):
This also checks if the message's author is a bot and doesn't
call :meth:`~.Bot.get_context` or :meth:`~.Bot.invoke` if so.
.. versionchanged:: 2.0
``message`` parameter is now positional-only.
Parameters
-----------
message: :class:`discord.Message`
@ -1220,7 +1274,7 @@ class BotBase(GroupMixin[None]):
# the type of the invocation context's bot attribute will be correct
await self.invoke(ctx) # type: ignore
async def on_message(self, message: Message) -> None:
async def on_message(self, message: Message, /) -> None:
await self.process_commands(message)