On branch DiscordProfile
Initial commit
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
"""
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2015-2021 Rapptz
|
||||
Copyright (c) 2021-present Pycord Development
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a
|
||||
copy of this software and associated documentation files (the "Software"),
|
||||
to deal in the Software without restriction, including without limitation
|
||||
the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
and/or sell copies of the Software, and to permit persons to whom the
|
||||
Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
||||
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
DEALINGS IN THE SOFTWARE.
|
||||
"""
|
||||
|
||||
from .bot import *
|
||||
from .context import *
|
||||
from .core import *
|
||||
BIN
Binary file not shown.
Binary file not shown.
BIN
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,204 @@
|
||||
"""
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2015-2021 Rapptz
|
||||
Copyright (c) 2021-present Pycord Development
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a
|
||||
copy of this software and associated documentation files (the "Software"),
|
||||
to deal in the Software without restriction, including without limitation
|
||||
the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
and/or sell copies of the Software, and to permit persons to whom the
|
||||
Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
||||
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
DEALINGS IN THE SOFTWARE.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from abc import ABC
|
||||
from collections.abc import Iterator
|
||||
|
||||
from discord.commands import ApplicationContext
|
||||
from discord.errors import CheckFailure, DiscordException
|
||||
from discord.interactions import Interaction
|
||||
from discord.message import Message
|
||||
|
||||
from ..commands import AutoShardedBot as ExtAutoShardedBot
|
||||
from ..commands import Bot as ExtBot
|
||||
from ..commands import Context as ExtContext
|
||||
from ..commands import errors
|
||||
from .context import BridgeApplicationContext, BridgeExtContext
|
||||
from .core import (
|
||||
BridgeCommand,
|
||||
BridgeCommandGroup,
|
||||
BridgeExtCommand,
|
||||
BridgeSlashCommand,
|
||||
bridge_command,
|
||||
bridge_group,
|
||||
)
|
||||
|
||||
__all__ = ("Bot", "AutoShardedBot")
|
||||
|
||||
|
||||
class BotBase(ABC):
|
||||
_bridge_commands: list[BridgeCommand | BridgeCommandGroup]
|
||||
|
||||
@property
|
||||
def bridge_commands(self) -> list[BridgeCommand | BridgeCommandGroup]:
|
||||
"""Returns all of the bot's bridge commands."""
|
||||
|
||||
if not (cmds := getattr(self, "_bridge_commands", None)):
|
||||
self._bridge_commands = cmds = []
|
||||
|
||||
return cmds
|
||||
|
||||
def walk_bridge_commands(
|
||||
self,
|
||||
) -> Iterator[BridgeCommand | BridgeCommandGroup]:
|
||||
"""An iterator that recursively walks through all the bot's bridge commands.
|
||||
|
||||
Yields
|
||||
------
|
||||
Union[:class:`.BridgeCommand`, :class:`.BridgeCommandGroup`]
|
||||
A bridge command or bridge group of the bot.
|
||||
"""
|
||||
for cmd in self._bridge_commands:
|
||||
yield cmd
|
||||
if isinstance(cmd, BridgeCommandGroup):
|
||||
yield from cmd.walk_commands()
|
||||
|
||||
async def get_application_context(
|
||||
self, interaction: Interaction, cls=None
|
||||
) -> BridgeApplicationContext:
|
||||
cls = cls if cls is not None else BridgeApplicationContext
|
||||
# Ignore the type hinting error here. BridgeApplicationContext is a subclass of ApplicationContext, and since
|
||||
# we gave it cls, it will be used instead.
|
||||
return await super().get_application_context(interaction, cls=cls) # type: ignore
|
||||
|
||||
async def get_context(self, message: Message, cls=None) -> BridgeExtContext:
|
||||
cls = cls if cls is not None else BridgeExtContext
|
||||
# Ignore the type hinting error here. BridgeExtContext is a subclass of Context, and since we gave it cls, it
|
||||
# will be used instead.
|
||||
return await super().get_context(message, cls=cls) # type: ignore
|
||||
|
||||
def add_bridge_command(self, command: BridgeCommand):
|
||||
"""Takes a :class:`.BridgeCommand` and adds both a slash and traditional (prefix-based) version of the command
|
||||
to the bot.
|
||||
"""
|
||||
# Ignore the type hinting error here. All subclasses of BotBase pass the type checks.
|
||||
command.add_to(self) # type: ignore
|
||||
|
||||
self.bridge_commands.append(command)
|
||||
|
||||
def bridge_command(self, **kwargs):
|
||||
"""A shortcut decorator that invokes :func:`bridge_command` and adds it to
|
||||
the internal command list via :meth:`~.Bot.add_bridge_command`.
|
||||
|
||||
Returns
|
||||
-------
|
||||
Callable[..., :class:`BridgeCommand`]
|
||||
A decorator that converts the provided method into an :class:`.BridgeCommand`, adds both a slash and
|
||||
traditional (prefix-based) version of the command to the bot, and returns the :class:`.BridgeCommand`.
|
||||
"""
|
||||
|
||||
def decorator(func) -> BridgeCommand:
|
||||
result = bridge_command(**kwargs)(func)
|
||||
self.add_bridge_command(result)
|
||||
return result
|
||||
|
||||
return decorator
|
||||
|
||||
def bridge_group(self, **kwargs):
|
||||
"""A decorator that is used to wrap a function as a bridge command group.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
kwargs: Optional[Dict[:class:`str`, Any]]
|
||||
Keyword arguments that are directly passed to the respective command constructors. (:class:`.SlashCommandGroup` and :class:`.ext.commands.Group`)
|
||||
"""
|
||||
|
||||
def decorator(func) -> BridgeCommandGroup:
|
||||
result = bridge_group(**kwargs)(func)
|
||||
self.add_bridge_command(result)
|
||||
return result
|
||||
|
||||
return decorator
|
||||
|
||||
async def invoke(self, ctx: ExtContext | BridgeExtContext):
|
||||
if ctx.command is not None:
|
||||
self.dispatch("command", ctx)
|
||||
if isinstance(ctx.command, BridgeExtCommand):
|
||||
self.dispatch("bridge_command", ctx)
|
||||
try:
|
||||
if await self.can_run(ctx, call_once=True):
|
||||
await ctx.command.invoke(ctx)
|
||||
else:
|
||||
raise errors.CheckFailure("The global check once functions failed.")
|
||||
except errors.CommandError as exc:
|
||||
await ctx.command.dispatch_error(ctx, exc)
|
||||
else:
|
||||
self.dispatch("command_completion", ctx)
|
||||
if isinstance(ctx.command, BridgeExtCommand):
|
||||
self.dispatch("bridge_command_completion", ctx)
|
||||
elif ctx.invoked_with:
|
||||
exc = errors.CommandNotFound(f'Command "{ctx.invoked_with}" is not found')
|
||||
self.dispatch("command_error", ctx, exc)
|
||||
if isinstance(ctx.command, BridgeExtCommand):
|
||||
self.dispatch("bridge_command_error", ctx, exc)
|
||||
|
||||
async def invoke_application_command(
|
||||
self, ctx: ApplicationContext | BridgeApplicationContext
|
||||
) -> None:
|
||||
"""|coro|
|
||||
|
||||
Invokes the application command given under the invocation
|
||||
context and handles all the internal event dispatch mechanisms.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
ctx: :class:`.ApplicationCommand`
|
||||
The invocation context to invoke.
|
||||
"""
|
||||
self._bot.dispatch("application_command", ctx)
|
||||
if br_cmd := isinstance(ctx.command, BridgeSlashCommand):
|
||||
self._bot.dispatch("bridge_command", ctx)
|
||||
try:
|
||||
if await self._bot.can_run(ctx, call_once=True):
|
||||
await ctx.command.invoke(ctx)
|
||||
else:
|
||||
raise CheckFailure("The global check once functions failed.")
|
||||
except DiscordException as exc:
|
||||
await ctx.command.dispatch_error(ctx, exc)
|
||||
else:
|
||||
self._bot.dispatch("application_command_completion", ctx)
|
||||
if br_cmd:
|
||||
self._bot.dispatch("bridge_command_completion", ctx)
|
||||
|
||||
|
||||
class Bot(BotBase, ExtBot):
|
||||
"""Represents a discord bot, with support for cross-compatibility between command types.
|
||||
|
||||
This class is a subclass of :class:`.ext.commands.Bot` and as a result
|
||||
anything that you can do with a :class:`.ext.commands.Bot` you can do with
|
||||
this bot.
|
||||
|
||||
.. versionadded:: 2.0
|
||||
"""
|
||||
|
||||
|
||||
class AutoShardedBot(BotBase, ExtAutoShardedBot):
|
||||
"""This is similar to :class:`.Bot` except that it is inherited from
|
||||
:class:`.ext.commands.AutoShardedBot` instead.
|
||||
|
||||
.. versionadded:: 2.0
|
||||
"""
|
||||
@@ -0,0 +1,203 @@
|
||||
"""
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2015-2021 Rapptz
|
||||
Copyright (c) 2021-present Pycord Development
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a
|
||||
copy of this software and associated documentation files (the "Software"),
|
||||
to deal in the Software without restriction, including without limitation
|
||||
the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
and/or sell copies of the Software, and to permit persons to whom the
|
||||
Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
||||
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
DEALINGS IN THE SOFTWARE.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from abc import ABC, abstractmethod
|
||||
from typing import TYPE_CHECKING, Any, Union, overload
|
||||
|
||||
from discord.commands import ApplicationContext
|
||||
from discord.interactions import Interaction, InteractionMessage
|
||||
from discord.message import Message
|
||||
from discord.webhook import WebhookMessage
|
||||
|
||||
from ..commands import Context
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from .core import BridgeExtCommand, BridgeSlashCommand
|
||||
|
||||
|
||||
__all__ = ("BridgeContext", "BridgeExtContext", "BridgeApplicationContext", "Context")
|
||||
|
||||
|
||||
class BridgeContext(ABC):
|
||||
"""
|
||||
The base context class for compatibility commands. This class is an :term:`abstract base class` (also known as an
|
||||
``abc``), which is subclassed by :class:`BridgeExtContext` and :class:`BridgeApplicationContext`. The methods in
|
||||
this class are meant to give parity between the two contexts, while still allowing for all of their functionality.
|
||||
|
||||
When this is passed to a command, it will either be passed as :class:`BridgeExtContext`, or
|
||||
:class:`BridgeApplicationContext`. Since they are two separate classes, it's easy to use the :attr:`BridgeContext.is_app` attribute.
|
||||
to make different functionality for each context. For example, if you want to respond to a command with the command
|
||||
type that it was invoked with, you can do the following:
|
||||
|
||||
.. code-block:: python3
|
||||
|
||||
@bot.bridge_command()
|
||||
async def example(ctx: BridgeContext):
|
||||
if ctx.is_app:
|
||||
command_type = "Application command"
|
||||
else:
|
||||
command_type = "Traditional (prefix-based) command"
|
||||
await ctx.send(f"This command was invoked with a(n) {command_type}.")
|
||||
|
||||
.. versionadded:: 2.0
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
async def _respond(
|
||||
self, *args, **kwargs
|
||||
) -> Interaction | WebhookMessage | Message: ...
|
||||
|
||||
@abstractmethod
|
||||
async def _defer(self, *args, **kwargs) -> None: ...
|
||||
|
||||
@abstractmethod
|
||||
async def _edit(self, *args, **kwargs) -> InteractionMessage | Message: ...
|
||||
|
||||
@overload
|
||||
async def invoke(
|
||||
self, command: BridgeSlashCommand | BridgeExtCommand, *args, **kwargs
|
||||
) -> None: ...
|
||||
|
||||
async def respond(self, *args, **kwargs) -> Interaction | WebhookMessage | Message:
|
||||
"""|coro|
|
||||
|
||||
Responds to the command with the respective response type to the current context. In :class:`BridgeExtContext`,
|
||||
this will be :meth:`~.Context.reply` while in :class:`BridgeApplicationContext`, this will be
|
||||
:meth:`~.ApplicationContext.respond`.
|
||||
"""
|
||||
return await self._respond(*args, **kwargs)
|
||||
|
||||
async def reply(self, *args, **kwargs) -> Interaction | WebhookMessage | Message:
|
||||
"""|coro|
|
||||
|
||||
Alias for :meth:`~.BridgeContext.respond`.
|
||||
"""
|
||||
return await self.respond(*args, **kwargs)
|
||||
|
||||
async def defer(self, *args, **kwargs) -> None:
|
||||
"""|coro|
|
||||
|
||||
Defers the command with the respective approach to the current context. In :class:`BridgeExtContext`, this will
|
||||
be :meth:`~discord.abc.Messageable.trigger_typing` while in :class:`BridgeApplicationContext`, this will be
|
||||
:attr:`~.ApplicationContext.defer`.
|
||||
|
||||
.. note::
|
||||
There is no ``trigger_typing`` alias for this method. ``trigger_typing`` will always provide the same
|
||||
functionality across contexts.
|
||||
"""
|
||||
return await self._defer(*args, **kwargs)
|
||||
|
||||
async def edit(self, *args, **kwargs) -> InteractionMessage | Message:
|
||||
"""|coro|
|
||||
|
||||
Edits the original response message with the respective approach to the current context. In
|
||||
:class:`BridgeExtContext`, this will have a custom approach where :meth:`.respond` caches the message to be
|
||||
edited here. In :class:`BridgeApplicationContext`, this will be :attr:`~.ApplicationContext.edit`.
|
||||
"""
|
||||
return await self._edit(*args, **kwargs)
|
||||
|
||||
def _get_super(self, attr: str) -> Any:
|
||||
return getattr(super(), attr)
|
||||
|
||||
@property
|
||||
def is_app(self) -> bool:
|
||||
"""Whether the context is an :class:`BridgeApplicationContext` or not."""
|
||||
return isinstance(self, BridgeApplicationContext)
|
||||
|
||||
|
||||
class BridgeApplicationContext(BridgeContext, ApplicationContext):
|
||||
"""
|
||||
The application context class for compatibility commands. This class is a subclass of :class:`BridgeContext` and
|
||||
:class:`~.ApplicationContext`. This class is meant to be used with :class:`BridgeCommand`.
|
||||
|
||||
.. versionadded:: 2.0
|
||||
"""
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
# This is needed in order to represent the correct class init signature on the docs
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
async def _respond(self, *args, **kwargs) -> Interaction | WebhookMessage:
|
||||
return await self._get_super("respond")(*args, **kwargs)
|
||||
|
||||
async def _defer(self, *args, **kwargs) -> None:
|
||||
return await self._get_super("defer")(*args, **kwargs)
|
||||
|
||||
async def _edit(self, *args, **kwargs) -> InteractionMessage:
|
||||
return await self._get_super("edit")(*args, **kwargs)
|
||||
|
||||
|
||||
class BridgeExtContext(BridgeContext, Context):
|
||||
"""
|
||||
The ext.commands context class for compatibility commands. This class is a subclass of :class:`BridgeContext` and
|
||||
:class:`~.Context`. This class is meant to be used with :class:`BridgeCommand`.
|
||||
|
||||
.. versionadded:: 2.0
|
||||
"""
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self._original_response_message: Message | None = None
|
||||
|
||||
async def _respond(self, *args, **kwargs) -> Message:
|
||||
kwargs.pop("ephemeral", None)
|
||||
message = await self._get_super("reply")(*args, **kwargs)
|
||||
if self._original_response_message is None:
|
||||
self._original_response_message = message
|
||||
return message
|
||||
|
||||
async def _defer(self, *args, **kwargs) -> None:
|
||||
kwargs.pop("ephemeral", None)
|
||||
return await self._get_super("trigger_typing")(*args, **kwargs)
|
||||
|
||||
async def _edit(self, *args, **kwargs) -> Message | None:
|
||||
if self._original_response_message:
|
||||
return await self._original_response_message.edit(*args, **kwargs)
|
||||
|
||||
async def delete(
|
||||
self, *, delay: float | None = None, reason: str | None = None
|
||||
) -> None:
|
||||
"""|coro|
|
||||
|
||||
Deletes the original response message, if it exists.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
delay: Optional[:class:`float`]
|
||||
If provided, the number of seconds to wait before deleting the message.
|
||||
reason: Optional[:class:`str`]
|
||||
The reason for deleting the message. Shows up on the audit log.
|
||||
"""
|
||||
if self._original_response_message:
|
||||
await self._original_response_message.delete(delay=delay, reason=reason)
|
||||
|
||||
|
||||
Context = Union[BridgeExtContext, BridgeApplicationContext]
|
||||
"""
|
||||
A Union class for either :class:`BridgeExtContext` or :class:`BridgeApplicationContext`.
|
||||
Can be used as a type hint for Context for bridge commands.
|
||||
"""
|
||||
@@ -0,0 +1,689 @@
|
||||
"""
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2015-2021 Rapptz
|
||||
Copyright (c) 2021-present Pycord Development
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a
|
||||
copy of this software and associated documentation files (the "Software"),
|
||||
to deal in the Software without restriction, including without limitation
|
||||
the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
and/or sell copies of the Software, and to permit persons to whom the
|
||||
Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
||||
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
DEALINGS IN THE SOFTWARE.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import inspect
|
||||
from collections.abc import Iterator
|
||||
from typing import TYPE_CHECKING, Any, Callable
|
||||
|
||||
import discord.commands.options
|
||||
from discord import (
|
||||
ApplicationCommand,
|
||||
Attachment,
|
||||
Option,
|
||||
Permissions,
|
||||
SlashCommand,
|
||||
SlashCommandGroup,
|
||||
SlashCommandOptionType,
|
||||
)
|
||||
|
||||
from ...utils import MISSING, find, get, warn_deprecated
|
||||
from ..commands import (
|
||||
BadArgument,
|
||||
)
|
||||
from ..commands import Bot as ExtBot
|
||||
from ..commands import (
|
||||
Command,
|
||||
Context,
|
||||
Converter,
|
||||
Group,
|
||||
GuildChannelConverter,
|
||||
MemberConverter,
|
||||
RoleConverter,
|
||||
UserConverter,
|
||||
)
|
||||
from ..commands.converter import _convert_to_bool, run_converters
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from .context import BridgeApplicationContext, BridgeExtContext
|
||||
|
||||
|
||||
__all__ = (
|
||||
"BridgeCommand",
|
||||
"BridgeCommandGroup",
|
||||
"bridge_command",
|
||||
"bridge_group",
|
||||
"bridge_option",
|
||||
"BridgeExtCommand",
|
||||
"BridgeSlashCommand",
|
||||
"BridgeExtGroup",
|
||||
"BridgeSlashGroup",
|
||||
"BridgeOption",
|
||||
"map_to",
|
||||
"guild_only",
|
||||
"has_permissions",
|
||||
"is_nsfw",
|
||||
)
|
||||
|
||||
|
||||
class BridgeSlashCommand(SlashCommand):
|
||||
"""A subclass of :class:`.SlashCommand` that is used for bridge commands."""
|
||||
|
||||
def __init__(self, func, **kwargs):
|
||||
self.brief = kwargs.pop("brief", None)
|
||||
super().__init__(func, **kwargs)
|
||||
|
||||
async def dispatch_error(
|
||||
self, ctx: BridgeApplicationContext, error: Exception
|
||||
) -> None:
|
||||
await super().dispatch_error(ctx, error)
|
||||
ctx.bot.dispatch("bridge_command_error", ctx, error)
|
||||
|
||||
|
||||
class BridgeExtCommand(Command):
|
||||
"""A subclass of :class:`.ext.commands.Command` that is used for bridge commands."""
|
||||
|
||||
def __init__(self, func, **kwargs):
|
||||
super().__init__(func, **kwargs)
|
||||
|
||||
for option in self.params.values():
|
||||
if isinstance(option.annotation, Option) and not isinstance(
|
||||
option.annotation, BridgeOption
|
||||
):
|
||||
raise TypeError(
|
||||
f"{option.annotation.__class__.__name__} is not supported in bridge commands. Use BridgeOption instead."
|
||||
)
|
||||
|
||||
async def dispatch_error(self, ctx: BridgeExtContext, error: Exception) -> None:
|
||||
await super().dispatch_error(ctx, error)
|
||||
ctx.bot.dispatch("bridge_command_error", ctx, error)
|
||||
|
||||
async def transform(self, ctx: Context, param: inspect.Parameter) -> Any:
|
||||
if param.annotation is Attachment:
|
||||
# skip the parameter checks for bridge attachments
|
||||
return await run_converters(ctx, AttachmentConverter, None, param)
|
||||
else:
|
||||
return await super().transform(ctx, param)
|
||||
|
||||
|
||||
class BridgeSlashGroup(SlashCommandGroup):
|
||||
"""A subclass of :class:`.SlashCommandGroup` that is used for bridge commands."""
|
||||
|
||||
__slots__ = ("module",)
|
||||
|
||||
def __init__(self, callback, *args, **kwargs):
|
||||
if perms := getattr(callback, "__default_member_permissions__", None):
|
||||
kwargs["default_member_permissions"] = perms
|
||||
super().__init__(*args, **kwargs)
|
||||
self.callback = callback
|
||||
self.__original_kwargs__["callback"] = callback
|
||||
self.__command = None
|
||||
|
||||
async def _invoke(self, ctx: BridgeApplicationContext) -> None:
|
||||
if not (options := ctx.interaction.data.get("options")):
|
||||
if not self.__command:
|
||||
self.__command = BridgeSlashCommand(self.callback)
|
||||
ctx.command = self.__command
|
||||
return await ctx.command.invoke(ctx)
|
||||
option = options[0]
|
||||
resolved = ctx.interaction.data.get("resolved", None)
|
||||
command = find(lambda x: x.name == option["name"], self.subcommands)
|
||||
option["resolved"] = resolved
|
||||
ctx.interaction.data = option
|
||||
await command.invoke(ctx)
|
||||
|
||||
|
||||
class BridgeExtGroup(BridgeExtCommand, Group):
|
||||
"""A subclass of :class:`.ext.commands.Group` that is used for bridge commands."""
|
||||
|
||||
|
||||
class BridgeCommand:
|
||||
"""Compatibility class between prefixed-based commands and slash commands.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
callback: Callable[[:class:`.BridgeContext`, ...], Awaitable[Any]]
|
||||
The callback to invoke when the command is executed. The first argument will be a :class:`BridgeContext`,
|
||||
and any additional arguments will be passed to the callback. This callback must be a coroutine.
|
||||
parent: Optional[:class:`.BridgeCommandGroup`]:
|
||||
Parent of the BridgeCommand.
|
||||
kwargs: Optional[Dict[:class:`str`, Any]]
|
||||
Keyword arguments that are directly passed to the respective command constructors. (:class:`.SlashCommand` and :class:`.ext.commands.Command`)
|
||||
|
||||
Attributes
|
||||
----------
|
||||
slash_variant: :class:`.BridgeSlashCommand`
|
||||
The slash command version of this bridge command.
|
||||
ext_variant: :class:`.BridgeExtCommand`
|
||||
The prefix-based version of this bridge command.
|
||||
"""
|
||||
|
||||
__bridge__: bool = True
|
||||
|
||||
__special_attrs__ = ["slash_variant", "ext_variant", "parent"]
|
||||
|
||||
def __init__(self, callback, **kwargs):
|
||||
self.parent = kwargs.pop("parent", None)
|
||||
self.slash_variant: BridgeSlashCommand = kwargs.pop(
|
||||
"slash_variant", None
|
||||
) or BridgeSlashCommand(callback, **kwargs)
|
||||
self.ext_variant: BridgeExtCommand = kwargs.pop(
|
||||
"ext_variant", None
|
||||
) or BridgeExtCommand(callback, **kwargs)
|
||||
|
||||
@property
|
||||
def name_localizations(self) -> dict[str, str] | None:
|
||||
"""Returns name_localizations from :attr:`slash_variant`
|
||||
You can edit/set name_localizations directly with
|
||||
|
||||
.. code-block:: python3
|
||||
|
||||
bridge_command.name_localizations["en-UK"] = ... # or any other locale
|
||||
# or
|
||||
bridge_command.name_localizations = {"en-UK": ..., "fr-FR": ...}
|
||||
"""
|
||||
return self.slash_variant.name_localizations
|
||||
|
||||
@name_localizations.setter
|
||||
def name_localizations(self, value):
|
||||
self.slash_variant.name_localizations = value
|
||||
|
||||
@property
|
||||
def description_localizations(self) -> dict[str, str] | None:
|
||||
"""Returns description_localizations from :attr:`slash_variant`
|
||||
You can edit/set description_localizations directly with
|
||||
|
||||
.. code-block:: python3
|
||||
|
||||
bridge_command.description_localizations["en-UK"] = ... # or any other locale
|
||||
# or
|
||||
bridge_command.description_localizations = {"en-UK": ..., "fr-FR": ...}
|
||||
"""
|
||||
return self.slash_variant.description_localizations
|
||||
|
||||
@description_localizations.setter
|
||||
def description_localizations(self, value):
|
||||
self.slash_variant.description_localizations = value
|
||||
|
||||
def __getattribute__(self, name):
|
||||
try:
|
||||
# first, look for the attribute on the bridge command
|
||||
return super().__getattribute__(name)
|
||||
except AttributeError as e:
|
||||
# if it doesn't exist, check this list, if the name of
|
||||
# the parameter is here
|
||||
if name in self.__special_attrs__:
|
||||
raise e
|
||||
|
||||
# looks up the result in the variants.
|
||||
# slash cmd prioritized
|
||||
result = getattr(self.slash_variant, name, MISSING)
|
||||
try:
|
||||
if result is MISSING:
|
||||
return getattr(self.ext_variant, name)
|
||||
return result
|
||||
except AttributeError:
|
||||
raise AttributeError(
|
||||
f"'{self.__class__.__name__}' object has no attribute '{name}'"
|
||||
)
|
||||
|
||||
def __setattr__(self, name, value) -> None:
|
||||
if name not in self.__special_attrs__:
|
||||
setattr(self.slash_variant, name, value)
|
||||
setattr(self.ext_variant, name, value)
|
||||
|
||||
return super().__setattr__(name, value)
|
||||
|
||||
def add_to(self, bot: ExtBot) -> None:
|
||||
"""Adds the command to a bot. This method is inherited by :class:`.BridgeCommandGroup`.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
bot: Union[:class:`.Bot`, :class:`.AutoShardedBot`]
|
||||
The bot to add the command to.
|
||||
"""
|
||||
bot.add_application_command(self.slash_variant)
|
||||
bot.add_command(self.ext_variant)
|
||||
|
||||
async def invoke(
|
||||
self, ctx: BridgeExtContext | BridgeApplicationContext, /, *args, **kwargs
|
||||
):
|
||||
if ctx.is_app:
|
||||
return await self.slash_variant.invoke(ctx)
|
||||
return await self.ext_variant.invoke(ctx)
|
||||
|
||||
def error(self, coro):
|
||||
"""A decorator that registers a coroutine as a local error handler.
|
||||
|
||||
This error handler is limited to the command it is defined to.
|
||||
However, higher scope handlers (per-cog and global) are still
|
||||
invoked afterwards as a catch-all. This handler also functions as
|
||||
the handler for both the prefixed and slash versions of the command.
|
||||
|
||||
This error handler takes two parameters, a :class:`.BridgeContext` and
|
||||
a :class:`~discord.DiscordException`.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
coro: :ref:`coroutine <coroutine>`
|
||||
The coroutine to register as the local error handler.
|
||||
|
||||
Raises
|
||||
------
|
||||
TypeError
|
||||
The coroutine passed is not actually a coroutine.
|
||||
"""
|
||||
self.slash_variant.error(coro)
|
||||
self.ext_variant.on_error = coro
|
||||
|
||||
return coro
|
||||
|
||||
def before_invoke(self, coro):
|
||||
"""A decorator that registers a coroutine as a pre-invoke hook.
|
||||
|
||||
This hook is called directly before the command is called, making
|
||||
it useful for any sort of set up required. This hook is called
|
||||
for both the prefixed and slash versions of the command.
|
||||
|
||||
This pre-invoke hook takes a sole parameter, a :class:`.BridgeContext`.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
coro: :ref:`coroutine <coroutine>`
|
||||
The coroutine to register as the pre-invoke hook.
|
||||
|
||||
Raises
|
||||
------
|
||||
TypeError
|
||||
The coroutine passed is not actually a coroutine.
|
||||
"""
|
||||
self.slash_variant.before_invoke(coro)
|
||||
self.ext_variant._before_invoke = coro
|
||||
|
||||
return coro
|
||||
|
||||
def after_invoke(self, coro):
|
||||
"""A decorator that registers a coroutine as a post-invoke hook.
|
||||
|
||||
This hook is called directly after the command is called, making it
|
||||
useful for any sort of clean up required. This hook is called for
|
||||
both the prefixed and slash versions of the command.
|
||||
|
||||
This post-invoke hook takes a sole parameter, a :class:`.BridgeContext`.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
coro: :ref:`coroutine <coroutine>`
|
||||
The coroutine to register as the post-invoke hook.
|
||||
|
||||
Raises
|
||||
------
|
||||
TypeError
|
||||
The coroutine passed is not actually a coroutine.
|
||||
"""
|
||||
self.slash_variant.after_invoke(coro)
|
||||
self.ext_variant._after_invoke = coro
|
||||
|
||||
return coro
|
||||
|
||||
|
||||
class BridgeCommandGroup(BridgeCommand):
|
||||
"""Compatibility class between prefixed-based commands and slash commands.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
callback: Callable[[:class:`.BridgeContext`, ...], Awaitable[Any]]
|
||||
The callback to invoke when the command is executed. The first argument will be a :class:`BridgeContext`,
|
||||
and any additional arguments will be passed to the callback. This callback must be a coroutine.
|
||||
kwargs: Optional[Dict[:class:`str`, Any]]
|
||||
Keyword arguments that are directly passed to the respective command constructors. (:class:`.SlashCommand` and :class:`.ext.commands.Command`)
|
||||
|
||||
Attributes
|
||||
----------
|
||||
slash_variant: :class:`.SlashCommandGroup`
|
||||
The slash command version of this command group.
|
||||
ext_variant: :class:`.ext.commands.Group`
|
||||
The prefix-based version of this command group.
|
||||
subcommands: List[:class:`.BridgeCommand`]
|
||||
List of bridge commands in this group
|
||||
mapped: Optional[:class:`.SlashCommand`]
|
||||
If :func:`map_to` is used, the mapped slash command.
|
||||
"""
|
||||
|
||||
__special_attrs__ = [
|
||||
"slash_variant",
|
||||
"ext_variant",
|
||||
"parent",
|
||||
"subcommands",
|
||||
"mapped",
|
||||
]
|
||||
|
||||
ext_variant: BridgeExtGroup
|
||||
slash_variant: BridgeSlashGroup
|
||||
|
||||
def __init__(self, callback, *args, **kwargs):
|
||||
ext_var = BridgeExtGroup(callback, *args, **kwargs)
|
||||
kwargs.update({"name": ext_var.name})
|
||||
super().__init__(
|
||||
callback,
|
||||
ext_variant=ext_var,
|
||||
slash_variant=BridgeSlashGroup(callback, *args, **kwargs),
|
||||
parent=kwargs.pop("parent", None),
|
||||
)
|
||||
|
||||
self.subcommands: list[BridgeCommand] = []
|
||||
|
||||
self.mapped: SlashCommand | None = None
|
||||
if map_to := getattr(callback, "__custom_map_to__", None):
|
||||
kwargs.update(map_to)
|
||||
self.mapped = self.slash_variant.command(**kwargs)(callback)
|
||||
|
||||
def walk_commands(self) -> Iterator[BridgeCommand]:
|
||||
"""An iterator that recursively walks through all the bridge group's subcommands.
|
||||
|
||||
Yields
|
||||
------
|
||||
:class:`.BridgeCommand`
|
||||
A bridge command of this bridge group.
|
||||
"""
|
||||
yield from self.subcommands
|
||||
|
||||
def command(self, *args, **kwargs):
|
||||
"""A decorator to register a function as a subcommand.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
kwargs: Optional[Dict[:class:`str`, Any]]
|
||||
Keyword arguments that are directly passed to the respective command constructors. (:class:`.SlashCommand` and :class:`.ext.commands.Command`)
|
||||
"""
|
||||
|
||||
def wrap(callback):
|
||||
slash = self.slash_variant.command(
|
||||
*args,
|
||||
**kwargs,
|
||||
cls=BridgeSlashCommand,
|
||||
)(callback)
|
||||
ext = self.ext_variant.command(
|
||||
*args,
|
||||
**kwargs,
|
||||
cls=BridgeExtCommand,
|
||||
)(callback)
|
||||
command = BridgeCommand(
|
||||
callback, parent=self, slash_variant=slash, ext_variant=ext
|
||||
)
|
||||
self.subcommands.append(command)
|
||||
return command
|
||||
|
||||
return wrap
|
||||
|
||||
|
||||
def bridge_command(**kwargs):
|
||||
"""A decorator that is used to wrap a function as a bridge command.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
kwargs: Optional[Dict[:class:`str`, Any]]
|
||||
Keyword arguments that are directly passed to the respective command constructors. (:class:`.SlashCommand` and :class:`.ext.commands.Command`)
|
||||
"""
|
||||
|
||||
def decorator(callback):
|
||||
return BridgeCommand(callback, **kwargs)
|
||||
|
||||
return decorator
|
||||
|
||||
|
||||
def bridge_group(**kwargs):
|
||||
"""A decorator that is used to wrap a function as a bridge command group.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
kwargs: Optional[Dict[:class:`str`, Any]]
|
||||
Keyword arguments that are directly passed to the respective command constructors (:class:`.SlashCommandGroup` and :class:`.ext.commands.Group`).
|
||||
"""
|
||||
|
||||
def decorator(callback):
|
||||
return BridgeCommandGroup(callback, **kwargs)
|
||||
|
||||
return decorator
|
||||
|
||||
|
||||
def map_to(name, description=None):
|
||||
"""To be used with bridge command groups, map the main command to a slash subcommand.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
name: :class:`str`
|
||||
The new name of the mapped command.
|
||||
description: Optional[:class:`str`]
|
||||
The new description of the mapped command.
|
||||
|
||||
Example
|
||||
-------
|
||||
|
||||
.. code-block:: python3
|
||||
|
||||
@bot.bridge_group()
|
||||
@bridge.map_to("show")
|
||||
async def config(ctx: BridgeContext):
|
||||
...
|
||||
|
||||
@config.command()
|
||||
async def toggle(ctx: BridgeContext):
|
||||
...
|
||||
|
||||
Prefixed commands will not be affected, but slash commands will appear as:
|
||||
|
||||
.. code-block::
|
||||
|
||||
/config show
|
||||
/config toggle
|
||||
"""
|
||||
|
||||
def decorator(callback):
|
||||
callback.__custom_map_to__ = {"name": name, "description": description}
|
||||
return callback
|
||||
|
||||
return decorator
|
||||
|
||||
|
||||
def guild_only():
|
||||
"""Intended to work with :class:`.ApplicationCommand` and :class:`BridgeCommand`, adds a :func:`~ext.commands.check`
|
||||
that locks the command to only run in guilds, and also registers the command as guild only client-side (on discord).
|
||||
|
||||
Basically a utility function that wraps both :func:`discord.ext.commands.guild_only` and :func:`discord.commands.guild_only`.
|
||||
"""
|
||||
|
||||
def predicate(func: Callable | ApplicationCommand):
|
||||
if isinstance(func, ApplicationCommand):
|
||||
func.guild_only = True
|
||||
else:
|
||||
func.__guild_only__ = True
|
||||
|
||||
from ..commands import guild_only
|
||||
|
||||
return guild_only()(func)
|
||||
|
||||
return predicate
|
||||
|
||||
|
||||
def is_nsfw():
|
||||
"""Intended to work with :class:`.ApplicationCommand` and :class:`BridgeCommand`, adds a :func:`~ext.commands.check`
|
||||
that locks the command to only run in nsfw contexts, and also registers the command as nsfw client-side (on discord).
|
||||
|
||||
Basically a utility function that wraps both :func:`discord.ext.commands.is_nsfw` and :func:`discord.commands.is_nsfw`.
|
||||
|
||||
.. warning::
|
||||
|
||||
In DMs, the prefixed-based command will always run as the user's privacy settings cannot be checked directly.
|
||||
"""
|
||||
|
||||
def predicate(func: Callable | ApplicationCommand):
|
||||
if isinstance(func, ApplicationCommand):
|
||||
func.nsfw = True
|
||||
else:
|
||||
func.__nsfw__ = True
|
||||
|
||||
from ..commands import is_nsfw
|
||||
|
||||
return is_nsfw()(func)
|
||||
|
||||
return predicate
|
||||
|
||||
|
||||
def has_permissions(**perms: bool):
|
||||
r"""Intended to work with :class:`.SlashCommand` and :class:`BridgeCommand`, adds a
|
||||
:func:`~ext.commands.check` that locks the command to be run by people with certain
|
||||
permissions inside guilds, and also registers the command as locked behind said permissions.
|
||||
|
||||
Basically a utility function that wraps both :func:`discord.ext.commands.has_permissions`
|
||||
and :func:`discord.commands.default_permissions`.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
\*\*perms: Dict[:class:`str`, :class:`bool`]
|
||||
An argument list of permissions to check for.
|
||||
"""
|
||||
|
||||
def predicate(func: Callable | ApplicationCommand):
|
||||
from ..commands import has_permissions
|
||||
|
||||
func = has_permissions(**perms)(func)
|
||||
_perms = Permissions(**perms)
|
||||
if isinstance(func, ApplicationCommand):
|
||||
func.default_member_permissions = _perms
|
||||
else:
|
||||
func.__default_member_permissions__ = _perms
|
||||
|
||||
return func
|
||||
|
||||
return predicate
|
||||
|
||||
|
||||
class MentionableConverter(Converter):
|
||||
"""A converter that can convert a mention to a member, a user or a role."""
|
||||
|
||||
async def convert(self, ctx, argument):
|
||||
try:
|
||||
return await RoleConverter().convert(ctx, argument)
|
||||
except BadArgument:
|
||||
pass
|
||||
|
||||
if ctx.guild:
|
||||
try:
|
||||
return await MemberConverter().convert(ctx, argument)
|
||||
except BadArgument:
|
||||
pass
|
||||
|
||||
return await UserConverter().convert(ctx, argument)
|
||||
|
||||
|
||||
class AttachmentConverter(Converter):
|
||||
async def convert(self, ctx: Context, arg: str):
|
||||
try:
|
||||
attach = ctx.message.attachments[0]
|
||||
except IndexError:
|
||||
raise BadArgument("At least 1 attachment is needed")
|
||||
else:
|
||||
return attach
|
||||
|
||||
|
||||
class BooleanConverter(Converter):
|
||||
async def convert(self, ctx, arg: bool):
|
||||
return _convert_to_bool(str(arg))
|
||||
|
||||
|
||||
BRIDGE_CONVERTER_MAPPING = {
|
||||
SlashCommandOptionType.string: str,
|
||||
SlashCommandOptionType.integer: int,
|
||||
SlashCommandOptionType.boolean: BooleanConverter,
|
||||
SlashCommandOptionType.user: UserConverter,
|
||||
SlashCommandOptionType.channel: GuildChannelConverter,
|
||||
SlashCommandOptionType.role: RoleConverter,
|
||||
SlashCommandOptionType.mentionable: MentionableConverter,
|
||||
SlashCommandOptionType.number: float,
|
||||
SlashCommandOptionType.attachment: AttachmentConverter,
|
||||
discord.Member: MemberConverter,
|
||||
}
|
||||
|
||||
|
||||
class BridgeOption(Option, Converter):
|
||||
"""A subclass of :class:`discord.Option` which represents a selectable slash
|
||||
command option and a prefixed command argument for bridge commands.
|
||||
"""
|
||||
|
||||
def __init__(self, input_type, *args, **kwargs):
|
||||
self.converter = kwargs.pop("converter", None)
|
||||
super().__init__(input_type, *args, **kwargs)
|
||||
|
||||
self.converter = self.converter or BRIDGE_CONVERTER_MAPPING.get(input_type)
|
||||
|
||||
async def convert(self, ctx, argument: str) -> Any:
|
||||
try:
|
||||
if self.converter is not None:
|
||||
converted = await self.converter().convert(ctx, argument)
|
||||
else:
|
||||
converter = BRIDGE_CONVERTER_MAPPING.get(self.input_type)
|
||||
if isinstance(converter, type) and issubclass(converter, Converter):
|
||||
converted = await converter().convert(ctx, argument) # type: ignore # protocol class
|
||||
elif callable(converter):
|
||||
converted = converter(argument)
|
||||
else:
|
||||
raise TypeError(f"Invalid converter: {converter}")
|
||||
|
||||
if self.choices:
|
||||
choices_names: list[str | int | float] = [
|
||||
choice.name for choice in self.choices
|
||||
]
|
||||
if converted in choices_names and (
|
||||
choice := get(self.choices, name=converted)
|
||||
):
|
||||
converted = choice.value
|
||||
else:
|
||||
choices = [choice.value for choice in self.choices]
|
||||
if converted not in choices:
|
||||
raise ValueError(
|
||||
f"{argument} is not a valid choice. Valid choices:"
|
||||
f" {list(set(choices_names + choices))}"
|
||||
)
|
||||
|
||||
return converted
|
||||
except ValueError as exc:
|
||||
raise BadArgument() from exc
|
||||
|
||||
|
||||
def bridge_option(name, input_type=None, **kwargs):
|
||||
"""A decorator that can be used instead of typehinting :class:`.BridgeOption`.
|
||||
|
||||
.. versionadded:: 2.6
|
||||
|
||||
Attributes
|
||||
----------
|
||||
parameter_name: :class:`str`
|
||||
The name of the target function parameter this option is mapped to.
|
||||
This allows you to have a separate UI ``name`` and parameter name.
|
||||
"""
|
||||
|
||||
def decorator(func):
|
||||
resolved_name = kwargs.pop("parameter_name", None) or name
|
||||
itype = (
|
||||
kwargs.pop("type", None)
|
||||
or input_type
|
||||
or func.__annotations__.get(resolved_name, str)
|
||||
)
|
||||
func.__annotations__[resolved_name] = BridgeOption(itype, name=name, **kwargs)
|
||||
return func
|
||||
|
||||
return decorator
|
||||
@@ -0,0 +1,19 @@
|
||||
"""
|
||||
discord.ext.commands
|
||||
~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
An extension module to facilitate creation of bot commands.
|
||||
|
||||
:copyright: (c) 2015-2021 Rapptz & (c) 2021-present Pycord Development
|
||||
:license: MIT, see LICENSE for more details.
|
||||
"""
|
||||
|
||||
from .bot import *
|
||||
from .cog import *
|
||||
from .context import *
|
||||
from .converter import *
|
||||
from .cooldowns import *
|
||||
from .core import *
|
||||
from .errors import *
|
||||
from .flags import *
|
||||
from .help import *
|
||||
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
@@ -0,0 +1,49 @@
|
||||
"""
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2015-2021 Rapptz
|
||||
Copyright (c) 2021-present Pycord Development
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a
|
||||
copy of this software and associated documentation files (the "Software"),
|
||||
to deal in the Software without restriction, including without limitation
|
||||
the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
and/or sell copies of the Software, and to permit persons to whom the
|
||||
Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
||||
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
DEALINGS IN THE SOFTWARE.
|
||||
"""
|
||||
|
||||
from typing import TYPE_CHECKING, Any, Callable, Coroutine, TypeVar, Union
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from .cog import Cog
|
||||
from .context import Context
|
||||
from .errors import CommandError
|
||||
|
||||
T = TypeVar("T")
|
||||
|
||||
Coro = Coroutine[Any, Any, T]
|
||||
MaybeCoro = Union[T, Coro[T]]
|
||||
CoroFunc = Callable[..., Coro[Any]]
|
||||
|
||||
Check = Union[
|
||||
Callable[["Cog", "Context[Any]"], MaybeCoro[bool]],
|
||||
Callable[["Context[Any]"], MaybeCoro[bool]],
|
||||
]
|
||||
Hook = Union[
|
||||
Callable[["Cog", "Context[Any]"], Coro[Any]], Callable[["Context[Any]"], Coro[Any]]
|
||||
]
|
||||
Error = Union[
|
||||
Callable[["Cog", "Context[Any]", "CommandError"], Coro[Any]],
|
||||
Callable[["Context[Any]", "CommandError"], Coro[Any]],
|
||||
]
|
||||
@@ -0,0 +1,457 @@
|
||||
"""
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2015-2021 Rapptz
|
||||
Copyright (c) 2021-present Pycord Development
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a
|
||||
copy of this software and associated documentation files (the "Software"),
|
||||
to deal in the Software without restriction, including without limitation
|
||||
the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
and/or sell copies of the Software, and to permit persons to whom the
|
||||
Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
||||
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
DEALINGS IN THE SOFTWARE.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import collections
|
||||
import collections.abc
|
||||
import sys
|
||||
import traceback
|
||||
from typing import TYPE_CHECKING, Any, Callable, Coroutine, Iterable, TypeVar
|
||||
|
||||
import discord
|
||||
|
||||
from . import errors
|
||||
from .context import Context
|
||||
from .core import GroupMixin
|
||||
from .help import DefaultHelpCommand, HelpCommand
|
||||
from .view import StringView
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from discord.message import Message
|
||||
|
||||
from ._types import CoroFunc
|
||||
|
||||
__all__ = (
|
||||
"when_mentioned",
|
||||
"when_mentioned_or",
|
||||
"Bot",
|
||||
"AutoShardedBot",
|
||||
)
|
||||
|
||||
MISSING: Any = discord.utils.MISSING
|
||||
|
||||
T = TypeVar("T")
|
||||
CFT = TypeVar("CFT", bound="CoroFunc")
|
||||
CXT = TypeVar("CXT", bound="Context")
|
||||
|
||||
|
||||
def when_mentioned(bot: Bot | AutoShardedBot, 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.
|
||||
"""
|
||||
# bot.user will never be None when this is called
|
||||
return [f"<@{bot.user.id}> ", f"<@!{bot.user.id}> "] # type: ignore
|
||||
|
||||
|
||||
def when_mentioned_or(
|
||||
*prefixes: str,
|
||||
) -> Callable[[Bot | AutoShardedBot, Message], list[str]]:
|
||||
"""A callable that implements when mentioned or other prefixes provided.
|
||||
|
||||
These are meant to be passed into the :attr:`.Bot.command_prefix` attribute.
|
||||
|
||||
See Also
|
||||
--------
|
||||
:func:`.when_mentioned`
|
||||
|
||||
Example
|
||||
-------
|
||||
|
||||
.. code-block:: python3
|
||||
|
||||
bot = commands.Bot(command_prefix=commands.when_mentioned_or('!'))
|
||||
|
||||
.. note::
|
||||
|
||||
This callable returns another callable, so if this is done inside a custom
|
||||
callable, you must call the returned callable, for example:
|
||||
|
||||
.. code-block:: python3
|
||||
|
||||
async def get_prefix(bot, message):
|
||||
extras = await prefixes_for(message.guild) # returns a list
|
||||
return commands.when_mentioned_or(*extras)(bot, message)
|
||||
"""
|
||||
|
||||
def inner(bot, msg):
|
||||
r = list(prefixes)
|
||||
r = when_mentioned(bot, msg) + r
|
||||
return r
|
||||
|
||||
return inner
|
||||
|
||||
|
||||
def _is_submodule(parent: str, child: str) -> bool:
|
||||
return parent == child or child.startswith(f"{parent}.")
|
||||
|
||||
|
||||
class BotBase(GroupMixin, discord.cog.CogMixin):
|
||||
_help_command = None
|
||||
_supports_prefixed_commands = True
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
command_prefix: (
|
||||
str
|
||||
| Iterable[str]
|
||||
| Callable[
|
||||
[Bot | AutoShardedBot, Message],
|
||||
str | Iterable[str] | Coroutine[Any, Any, str | Iterable[str]],
|
||||
]
|
||||
) = when_mentioned,
|
||||
help_command: HelpCommand | None = MISSING,
|
||||
**options,
|
||||
):
|
||||
super().__init__(**options)
|
||||
self.command_prefix = command_prefix
|
||||
self.help_command = (
|
||||
DefaultHelpCommand() if help_command is MISSING else help_command
|
||||
)
|
||||
self.strip_after_prefix = options.get("strip_after_prefix", False)
|
||||
|
||||
@discord.utils.copy_doc(discord.Client.close)
|
||||
async def close(self) -> None:
|
||||
for extension in tuple(self.__extensions):
|
||||
try:
|
||||
self.unload_extension(extension)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
for cog in tuple(self.__cogs):
|
||||
try:
|
||||
self.remove_cog(cog)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
await super().close() # type: ignore
|
||||
|
||||
async def on_command_error(
|
||||
self, context: Context, exception: errors.CommandError
|
||||
) -> None:
|
||||
"""|coro|
|
||||
|
||||
The default command error handler provided by the bot.
|
||||
|
||||
By default, this prints to :data:`sys.stderr` however it could be
|
||||
overridden to have a different implementation.
|
||||
|
||||
This only fires if you do not specify any listeners for command error.
|
||||
"""
|
||||
if self._event_handlers.get("on_command_error", None):
|
||||
return
|
||||
|
||||
command = context.command
|
||||
if command and command.has_error_handler():
|
||||
return
|
||||
|
||||
cog = context.cog
|
||||
if cog and cog.has_error_handler():
|
||||
return
|
||||
|
||||
print(f"Ignoring exception in command {context.command}:", file=sys.stderr)
|
||||
traceback.print_exception(
|
||||
type(exception), exception, exception.__traceback__, file=sys.stderr
|
||||
)
|
||||
|
||||
async def can_run(self, ctx: Context, *, call_once: bool = False) -> bool:
|
||||
data = self._check_once if call_once else self._checks
|
||||
|
||||
if len(data) == 0:
|
||||
return True
|
||||
|
||||
# type-checker doesn't distinguish between functions and methods
|
||||
return await discord.utils.async_all(f(ctx) for f in data) # type: ignore
|
||||
|
||||
# help command stuff
|
||||
|
||||
@property
|
||||
def help_command(self) -> HelpCommand | None:
|
||||
return self._help_command
|
||||
|
||||
@help_command.setter
|
||||
def help_command(self, value: HelpCommand | None) -> None:
|
||||
if value is not None:
|
||||
if not isinstance(value, HelpCommand):
|
||||
raise TypeError("help_command must be a subclass of HelpCommand")
|
||||
if self._help_command is not None:
|
||||
self._help_command._remove_from_bot(self)
|
||||
self._help_command = value
|
||||
value._add_to_bot(self)
|
||||
elif self._help_command is not None:
|
||||
self._help_command._remove_from_bot(self)
|
||||
self._help_command = None
|
||||
else:
|
||||
self._help_command = None
|
||||
|
||||
# command processing
|
||||
|
||||
async def get_prefix(self, message: Message) -> list[str] | str:
|
||||
"""|coro|
|
||||
|
||||
Retrieves the prefix the bot is listening to
|
||||
with the message as a context.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
message: :class:`discord.Message`
|
||||
The message context to get the prefix of.
|
||||
|
||||
Returns
|
||||
-------
|
||||
Union[List[:class:`str`], :class:`str`]
|
||||
A list of prefixes or a single prefix that the bot is
|
||||
listening for.
|
||||
"""
|
||||
prefix = ret = self.command_prefix
|
||||
if callable(prefix):
|
||||
ret = await discord.utils.maybe_coroutine(prefix, self, message)
|
||||
|
||||
if not isinstance(ret, str):
|
||||
try:
|
||||
ret = list(ret)
|
||||
except TypeError:
|
||||
# It's possible that a generator raised this exception. Don't
|
||||
# replace it with our own error if that's the case.
|
||||
if isinstance(ret, collections.abc.Iterable):
|
||||
raise
|
||||
|
||||
raise TypeError(
|
||||
"command_prefix must be plain string, iterable of strings, or"
|
||||
f" callable returning either of these, not {ret.__class__.__name__}"
|
||||
)
|
||||
|
||||
if not ret:
|
||||
raise ValueError(
|
||||
"Iterable command_prefix must contain at least one prefix"
|
||||
)
|
||||
|
||||
return ret
|
||||
|
||||
async def get_context(self, message: Message, *, cls: type[CXT] = Context) -> CXT:
|
||||
r"""|coro|
|
||||
|
||||
Returns the invocation context from the message.
|
||||
|
||||
This is a more low-level counter-part for :meth:`.process_commands`
|
||||
to allow users more fine-grained control over the processing.
|
||||
|
||||
The returned context is not guaranteed to be a valid invocation
|
||||
context, :attr:`.Context.valid` must be checked to make sure it is.
|
||||
If the context is not valid then it is not a valid candidate to be
|
||||
invoked under :meth:`~.Bot.invoke`.
|
||||
|
||||
Parameters
|
||||
-----------
|
||||
message: :class:`discord.Message`
|
||||
The message to get the invocation context from.
|
||||
cls
|
||||
The factory class that will be used to create the context.
|
||||
By default, this is :class:`.Context`. Should a custom
|
||||
class be provided, it must be similar enough to :class:`.Context`\'s
|
||||
interface.
|
||||
|
||||
Returns
|
||||
--------
|
||||
:class:`.Context`
|
||||
The invocation context. The type of this can change via the
|
||||
``cls`` parameter.
|
||||
"""
|
||||
|
||||
view = StringView(message.content)
|
||||
ctx = cls(prefix=None, view=view, bot=self, message=message)
|
||||
|
||||
if message.author.id == self.user.id: # type: ignore
|
||||
return ctx
|
||||
|
||||
prefix = await self.get_prefix(message)
|
||||
invoked_prefix = prefix
|
||||
|
||||
if isinstance(prefix, str):
|
||||
if not view.skip_string(prefix):
|
||||
return ctx
|
||||
else:
|
||||
try:
|
||||
# if the context class' __init__ consumes something from the view this
|
||||
# will be wrong. That seems unreasonable though.
|
||||
if message.content.startswith(tuple(prefix)):
|
||||
invoked_prefix = discord.utils.find(view.skip_string, prefix)
|
||||
else:
|
||||
return ctx
|
||||
|
||||
except TypeError:
|
||||
if not isinstance(prefix, list):
|
||||
raise TypeError(
|
||||
"get_prefix must return either a string or a list of string, "
|
||||
f"not {prefix.__class__.__name__}"
|
||||
)
|
||||
|
||||
# It's possible a bad command_prefix got us here.
|
||||
for value in prefix:
|
||||
if not isinstance(value, str):
|
||||
raise TypeError(
|
||||
"Iterable command_prefix or list returned from get_prefix"
|
||||
" must contain only strings, not"
|
||||
f" {value.__class__.__name__}"
|
||||
)
|
||||
|
||||
# Getting here shouldn't happen
|
||||
raise
|
||||
|
||||
if self.strip_after_prefix:
|
||||
view.skip_ws()
|
||||
|
||||
invoker = view.get_word()
|
||||
ctx.invoked_with = invoker
|
||||
# type-checker fails to narrow invoked_prefix type.
|
||||
ctx.prefix = invoked_prefix # type: ignore
|
||||
ctx.command = self.prefixed_commands.get(invoker)
|
||||
return ctx
|
||||
|
||||
async def invoke(self, ctx: Context) -> None:
|
||||
"""|coro|
|
||||
|
||||
Invokes the command given under the invocation context and
|
||||
handles all the internal event dispatch mechanisms.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
ctx: :class:`.Context`
|
||||
The invocation context to invoke.
|
||||
"""
|
||||
if ctx.command is not None:
|
||||
self.dispatch("command", ctx)
|
||||
try:
|
||||
if await self.can_run(ctx, call_once=True):
|
||||
await ctx.command.invoke(ctx)
|
||||
else:
|
||||
raise errors.CheckFailure("The global check once functions failed.")
|
||||
except errors.CommandError as exc:
|
||||
await ctx.command.dispatch_error(ctx, exc)
|
||||
else:
|
||||
self.dispatch("command_completion", ctx)
|
||||
elif ctx.invoked_with:
|
||||
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:
|
||||
"""|coro|
|
||||
|
||||
This function processes the commands that have been registered
|
||||
to the bot and other groups. Without this coroutine, none of the
|
||||
commands will be triggered.
|
||||
|
||||
By default, this coroutine is called inside the :func:`.on_message`
|
||||
event. If you choose to override the :func:`.on_message` event, then
|
||||
you should invoke this coroutine as well.
|
||||
|
||||
This is built using other low level tools, and is equivalent to a
|
||||
call to :meth:`~.Bot.get_context` followed by a call to :meth:`~.Bot.invoke`.
|
||||
|
||||
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.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
message: :class:`discord.Message`
|
||||
The message to process commands for.
|
||||
"""
|
||||
if message.author.bot:
|
||||
return
|
||||
|
||||
ctx = await self.get_context(message)
|
||||
await self.invoke(ctx)
|
||||
|
||||
async def on_message(self, message):
|
||||
await self.process_commands(message)
|
||||
|
||||
|
||||
class Bot(BotBase, discord.Bot):
|
||||
"""Represents a discord bot.
|
||||
|
||||
This class is a subclass of :class:`discord.Bot` and as a result
|
||||
anything that you can do with a :class:`discord.Bot` you can do with
|
||||
this bot.
|
||||
|
||||
This class also subclasses :class:`.GroupMixin` to provide the functionality
|
||||
to manage commands.
|
||||
|
||||
.. note::
|
||||
|
||||
Using prefixed commands requires :attr:`discord.Intents.message_content` to be enabled.
|
||||
|
||||
Attributes
|
||||
----------
|
||||
command_prefix
|
||||
The command prefix is what the message content must contain initially
|
||||
to have a command invoked. This prefix could either be a string to
|
||||
indicate what the prefix should be, or a callable that takes in the bot
|
||||
as its first parameter and :class:`discord.Message` as its second
|
||||
parameter and returns the prefix. This is to facilitate "dynamic"
|
||||
command prefixes. This callable can be either a regular function or
|
||||
a coroutine.
|
||||
|
||||
An empty string as the prefix always matches, enabling prefix-less
|
||||
command invocation. While this may be useful in DMs it should be avoided
|
||||
in servers, as it's likely to cause performance issues and unintended
|
||||
command invocations.
|
||||
|
||||
The command prefix could also be an iterable of strings indicating that
|
||||
multiple checks for the prefix should be used and the first one to
|
||||
match will be the invocation prefix. You can get this prefix via
|
||||
:attr:`.Context.prefix`. To avoid confusion empty iterables are not
|
||||
allowed.
|
||||
|
||||
.. note::
|
||||
|
||||
When passing multiple prefixes be careful to not pass a prefix
|
||||
that matches a longer prefix occurring later in the sequence. For
|
||||
example, if the command prefix is ``('!', '!?')`` the ``'!?'``
|
||||
prefix will never be matched to any message as the previous one
|
||||
matches messages starting with ``!?``. This is especially important
|
||||
when passing an empty string, it should always be last as no prefix
|
||||
after it will be matched.
|
||||
case_insensitive: :class:`bool`
|
||||
Whether the commands should be case-insensitive. Defaults to ``False``. This
|
||||
attribute does not carry over to groups. You must set it to every group if
|
||||
you require group commands to be case-insensitive as well.
|
||||
help_command: Optional[:class:`.HelpCommand`]
|
||||
The help command implementation to use. This can be dynamically
|
||||
set at runtime. To remove the help command pass ``None``. For more
|
||||
information on implementing a help command, see :ref:`ext_commands_help_command`.
|
||||
strip_after_prefix: :class:`bool`
|
||||
Whether to strip whitespace characters after encountering the command
|
||||
prefix. This allows for ``! hello`` and ``!hello`` to both work if
|
||||
the ``command_prefix`` is set to ``!``. Defaults to ``False``.
|
||||
|
||||
.. versionadded:: 1.7
|
||||
"""
|
||||
|
||||
|
||||
class AutoShardedBot(BotBase, discord.AutoShardedBot):
|
||||
"""This is similar to :class:`.Bot` except that it is inherited from
|
||||
:class:`discord.AutoShardedBot` instead.
|
||||
"""
|
||||
@@ -0,0 +1,85 @@
|
||||
"""
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2015-2021 Rapptz
|
||||
Copyright (c) 2021-present Pycord Development
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a
|
||||
copy of this software and associated documentation files (the "Software"),
|
||||
to deal in the Software without restriction, including without limitation
|
||||
the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
and/or sell copies of the Software, and to permit persons to whom the
|
||||
Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
||||
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
DEALINGS IN THE SOFTWARE.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import TYPE_CHECKING, Any, Callable, Generator, TypeVar
|
||||
|
||||
import discord
|
||||
|
||||
from ...cog import Cog
|
||||
from ...commands import ApplicationCommand, SlashCommandGroup
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from .core import Command
|
||||
|
||||
__all__ = ("Cog",)
|
||||
|
||||
CogT = TypeVar("CogT", bound="Cog")
|
||||
FuncT = TypeVar("FuncT", bound=Callable[..., Any])
|
||||
|
||||
MISSING: Any = discord.utils.MISSING
|
||||
|
||||
|
||||
class Cog(Cog):
|
||||
def __new__(cls: type[CogT], *args: Any, **kwargs: Any) -> CogT:
|
||||
# For issue 426, we need to store a copy of the command objects
|
||||
# since we modify them to inject `self` to them.
|
||||
# To do this, we need to interfere with the Cog creation process.
|
||||
return super().__new__(cls)
|
||||
|
||||
def walk_commands(self) -> Generator[Command]:
|
||||
"""An iterator that recursively walks through this cog's commands and subcommands.
|
||||
|
||||
Yields
|
||||
------
|
||||
Union[:class:`.Command`, :class:`.Group`]
|
||||
A command or group from the cog.
|
||||
"""
|
||||
from .core import GroupMixin
|
||||
|
||||
for command in self.__cog_commands__:
|
||||
if not isinstance(command, ApplicationCommand):
|
||||
if command.parent is None:
|
||||
yield command
|
||||
if isinstance(command, GroupMixin):
|
||||
yield from command.walk_commands()
|
||||
elif isinstance(command, SlashCommandGroup):
|
||||
yield from command.walk_commands()
|
||||
else:
|
||||
yield command
|
||||
|
||||
def get_commands(self) -> list[ApplicationCommand | Command]:
|
||||
r"""
|
||||
Returns
|
||||
--------
|
||||
List[Union[:class:`~discord.ApplicationCommand`, :class:`.Command`]]
|
||||
A :class:`list` of commands that are defined inside this cog.
|
||||
|
||||
.. note::
|
||||
|
||||
This does not include subcommands.
|
||||
"""
|
||||
return [c for c in self.__cog_commands__ if c.parent is None]
|
||||
@@ -0,0 +1,411 @@
|
||||
"""
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2015-2021 Rapptz
|
||||
Copyright (c) 2021-present Pycord Development
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a
|
||||
copy of this software and associated documentation files (the "Software"),
|
||||
to deal in the Software without restriction, including without limitation
|
||||
the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
and/or sell copies of the Software, and to permit persons to whom the
|
||||
Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
||||
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
DEALINGS IN THE SOFTWARE.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import inspect
|
||||
import re
|
||||
from typing import TYPE_CHECKING, Any, Generic, TypeVar, Union
|
||||
|
||||
import discord.abc
|
||||
import discord.utils
|
||||
from discord.message import Message
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from typing_extensions import ParamSpec
|
||||
|
||||
from discord.abc import MessageableChannel
|
||||
from discord.guild import Guild
|
||||
from discord.member import Member
|
||||
from discord.state import ConnectionState
|
||||
from discord.user import ClientUser, User
|
||||
from discord.voice import VoiceProtocol
|
||||
|
||||
from .bot import AutoShardedBot, Bot
|
||||
from .cog import Cog
|
||||
from .core import Command
|
||||
from .view import StringView
|
||||
|
||||
__all__ = ("Context",)
|
||||
|
||||
MISSING: Any = discord.utils.MISSING
|
||||
|
||||
|
||||
T = TypeVar("T")
|
||||
BotT = TypeVar("BotT", bound="Union[Bot, AutoShardedBot]")
|
||||
CogT = TypeVar("CogT", bound="Cog")
|
||||
|
||||
if TYPE_CHECKING:
|
||||
P = ParamSpec("P")
|
||||
else:
|
||||
P = TypeVar("P")
|
||||
|
||||
|
||||
class Context(discord.abc.Messageable, Generic[BotT]):
|
||||
r"""Represents the context in which a command is being invoked under.
|
||||
|
||||
This class contains a lot of metadata to help you understand more about
|
||||
the invocation context. This class is not created manually and is instead
|
||||
passed around to commands as the first parameter.
|
||||
|
||||
This class implements the :class:`~discord.abc.Messageable` ABC.
|
||||
|
||||
Attributes
|
||||
-----------
|
||||
message: :class:`.Message`
|
||||
The message that triggered the command being executed.
|
||||
bot: :class:`.Bot`
|
||||
The bot that contains the command being executed.
|
||||
args: :class:`list`
|
||||
The list of transformed arguments that were passed into the command.
|
||||
If this is accessed during the :func:`.on_command_error` event
|
||||
then this list could be incomplete.
|
||||
kwargs: :class:`dict`
|
||||
A dictionary of transformed arguments that were passed into the command.
|
||||
Similar to :attr:`args`\, if this is accessed in the
|
||||
:func:`.on_command_error` event then this dict could be incomplete.
|
||||
current_parameter: Optional[:class:`inspect.Parameter`]
|
||||
The parameter that is currently being inspected and converted.
|
||||
This is only of use for within converters.
|
||||
|
||||
.. versionadded:: 2.0
|
||||
prefix: Optional[:class:`str`]
|
||||
The prefix that was used to invoke the command.
|
||||
command: Optional[:class:`Command`]
|
||||
The command that is being invoked currently.
|
||||
invoked_with: Optional[:class:`str`]
|
||||
The command name that triggered this invocation. Useful for finding out
|
||||
which alias called the command.
|
||||
invoked_parents: List[:class:`str`]
|
||||
The command names of the parents that triggered this invocation. Useful for
|
||||
finding out which aliases called the command.
|
||||
|
||||
For example in commands ``?a b c test``, the invoked parents are ``['a', 'b', 'c']``.
|
||||
|
||||
.. versionadded:: 1.7
|
||||
|
||||
invoked_subcommand: Optional[:class:`Command`]
|
||||
The subcommand that was invoked.
|
||||
If no valid subcommand was invoked then this is equal to ``None``.
|
||||
subcommand_passed: Optional[:class:`str`]
|
||||
The string that was attempted to call a subcommand. This does not have
|
||||
to point to a valid registered subcommand and could just point to a
|
||||
nonsense string. If nothing was passed to attempt a call to a
|
||||
subcommand then this is set to ``None``.
|
||||
command_failed: :class:`bool`
|
||||
A boolean that indicates if the command failed to be parsed, checked,
|
||||
or invoked.
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
*,
|
||||
message: Message,
|
||||
bot: BotT,
|
||||
view: StringView,
|
||||
args: list[Any] = MISSING,
|
||||
kwargs: dict[str, Any] = MISSING,
|
||||
prefix: str | None = None,
|
||||
command: Command | None = None,
|
||||
invoked_with: str | None = None,
|
||||
invoked_parents: list[str] = MISSING,
|
||||
invoked_subcommand: Command | None = None,
|
||||
subcommand_passed: str | None = None,
|
||||
command_failed: bool = False,
|
||||
current_parameter: inspect.Parameter | None = None,
|
||||
):
|
||||
self.message: Message = message
|
||||
self.bot: BotT = bot
|
||||
self.args: list[Any] = args or []
|
||||
self.kwargs: dict[str, Any] = kwargs or {}
|
||||
self.prefix: str | None = prefix
|
||||
self.command: Command | None = command
|
||||
self.view: StringView = view
|
||||
self.invoked_with: str | None = invoked_with
|
||||
self.invoked_parents: list[str] = invoked_parents or []
|
||||
self.invoked_subcommand: Command | None = invoked_subcommand
|
||||
self.subcommand_passed: str | None = subcommand_passed
|
||||
self.command_failed: bool = command_failed
|
||||
self.current_parameter: inspect.Parameter | None = current_parameter
|
||||
self._state: ConnectionState = self.message._state
|
||||
|
||||
async def invoke(
|
||||
self, command: Command[CogT, P, T], /, *args: P.args, **kwargs: P.kwargs
|
||||
) -> T:
|
||||
r"""|coro|
|
||||
|
||||
Calls a command with the arguments given.
|
||||
|
||||
This is useful if you want to just call the callback that a
|
||||
:class:`.Command` holds internally.
|
||||
|
||||
.. note::
|
||||
|
||||
This does not handle converters, checks, cooldowns, pre-invoke,
|
||||
or after-invoke hooks in any matter. It calls the internal callback
|
||||
directly as-if it was a regular function.
|
||||
|
||||
You must take care in passing the proper arguments when
|
||||
using this function.
|
||||
|
||||
Parameters
|
||||
-----------
|
||||
command: :class:`.Command`
|
||||
The command that is going to be called.
|
||||
\*args
|
||||
The arguments to use.
|
||||
\*\*kwargs
|
||||
The keyword arguments to use.
|
||||
|
||||
Raises
|
||||
-------
|
||||
TypeError
|
||||
The command argument to invoke is missing.
|
||||
"""
|
||||
return await command(self, *args, **kwargs)
|
||||
|
||||
async def reinvoke(self, *, call_hooks: bool = False, restart: bool = True) -> None:
|
||||
"""|coro|
|
||||
|
||||
Calls the command again.
|
||||
|
||||
This is similar to :meth:`~.Context.invoke` except that it bypasses
|
||||
checks, cooldowns, and error handlers.
|
||||
|
||||
.. note::
|
||||
|
||||
If you want to bypass :exc:`.UserInputError` derived exceptions,
|
||||
it is recommended to use the regular :meth:`~.Context.invoke`
|
||||
as it will work more naturally. After all, this will end up
|
||||
using the old arguments the user has used and will thus just
|
||||
fail again.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
call_hooks: :class:`bool`
|
||||
Whether to call the before and after invoke hooks.
|
||||
restart: :class:`bool`
|
||||
Whether to start the call chain from the very beginning
|
||||
or where we left off (i.e. the command that caused the error).
|
||||
The default is to start where we left off.
|
||||
|
||||
Raises
|
||||
------
|
||||
ValueError
|
||||
The context to reinvoke is not valid.
|
||||
"""
|
||||
cmd = self.command
|
||||
view = self.view
|
||||
if cmd is None:
|
||||
raise ValueError("This context is not valid.")
|
||||
|
||||
# some state to revert to when we're done
|
||||
index, previous = view.index, view.previous
|
||||
invoked_with = self.invoked_with
|
||||
invoked_subcommand = self.invoked_subcommand
|
||||
invoked_parents = self.invoked_parents
|
||||
subcommand_passed = self.subcommand_passed
|
||||
|
||||
if restart:
|
||||
to_call = cmd.root_parent or cmd
|
||||
view.index = len(self.prefix or "")
|
||||
view.previous = 0
|
||||
self.invoked_parents = []
|
||||
self.invoked_with = view.get_word() # advance to get the root command
|
||||
else:
|
||||
to_call = cmd
|
||||
|
||||
try:
|
||||
await to_call.reinvoke(self, call_hooks=call_hooks)
|
||||
finally:
|
||||
self.command = cmd
|
||||
view.index = index
|
||||
view.previous = previous
|
||||
self.invoked_with = invoked_with
|
||||
self.invoked_subcommand = invoked_subcommand
|
||||
self.invoked_parents = invoked_parents
|
||||
self.subcommand_passed = subcommand_passed
|
||||
|
||||
@property
|
||||
def valid(self) -> bool:
|
||||
"""Checks if the invocation context is valid to be invoked with."""
|
||||
return self.prefix is not None and self.command is not None
|
||||
|
||||
async def _get_channel(self) -> discord.abc.Messageable:
|
||||
return self.channel
|
||||
|
||||
@property
|
||||
def clean_prefix(self) -> str:
|
||||
"""The cleaned up invoke prefix. i.e. mentions are ``@name`` instead of ``<@id>``.
|
||||
|
||||
.. versionadded:: 2.0
|
||||
"""
|
||||
if self.prefix is None:
|
||||
return ""
|
||||
|
||||
user = self.me
|
||||
# this breaks if the prefix mention is not the bot itself, but I
|
||||
# consider this to be an *incredibly* strange use case. I'd rather go
|
||||
# for this common use case rather than waste performance for the
|
||||
# odd one.
|
||||
pattern = re.compile(r"<@!?%s>" % user.id)
|
||||
return pattern.sub("@%s" % user.display_name.replace("\\", r"\\"), self.prefix)
|
||||
|
||||
@property
|
||||
def cog(self) -> Cog | None:
|
||||
"""Returns the cog associated with this context's command.
|
||||
None if it does not exist.
|
||||
"""
|
||||
|
||||
if self.command is None:
|
||||
return None
|
||||
return self.command.cog
|
||||
|
||||
@property
|
||||
def guild(self) -> Guild | None:
|
||||
"""Returns the guild associated with this context's command.
|
||||
None if not available.
|
||||
"""
|
||||
return self.message.guild
|
||||
|
||||
@property
|
||||
def channel(self) -> MessageableChannel:
|
||||
"""Returns the channel associated with this context's command.
|
||||
Shorthand for :attr:`.Message.channel`.
|
||||
"""
|
||||
return self.message.channel
|
||||
|
||||
@property
|
||||
def author(self) -> User | Member:
|
||||
"""Union[:class:`~discord.User`, :class:`.Member`]:
|
||||
Returns the author associated with this context's command. Shorthand for :attr:`.Message.author`
|
||||
"""
|
||||
return self.message.author
|
||||
|
||||
@property
|
||||
def me(self) -> Member | ClientUser:
|
||||
"""Union[:class:`.Member`, :class:`.ClientUser`]:
|
||||
Similar to :attr:`.Guild.me` except it may return the :class:`.ClientUser` in private message
|
||||
message contexts, or when :meth:`Intents.guilds` is absent.
|
||||
"""
|
||||
# bot.user will never be None at this point.
|
||||
return self.guild.me if self.guild is not None and self.guild.me is not None else self.bot.user # type: ignore
|
||||
|
||||
@property
|
||||
def voice_client(self) -> VoiceProtocol | None:
|
||||
r"""A shortcut to :attr:`.Guild.voice_client`\, if applicable."""
|
||||
g = self.guild
|
||||
return g.voice_client if g else None
|
||||
|
||||
async def send_help(self, *args: Any) -> Any:
|
||||
"""send_help(entity=<bot>)
|
||||
|
||||
|coro|
|
||||
|
||||
Shows the help command for the specified entity if given.
|
||||
The entity can be a command or a cog.
|
||||
|
||||
If no entity is given, then it'll show help for the
|
||||
entire bot.
|
||||
|
||||
If the entity is a string, then it looks up whether it's a
|
||||
:class:`Cog` or a :class:`Command`.
|
||||
|
||||
.. note::
|
||||
|
||||
Due to the way this function works, instead of returning
|
||||
something similar to :meth:`~.commands.HelpCommand.command_not_found`
|
||||
this returns :class:`None` on bad input or no help command.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
entity: Optional[Union[:class:`Command`, :class:`Cog`, :class:`str`]]
|
||||
The entity to show help for.
|
||||
|
||||
Returns
|
||||
-------
|
||||
Any
|
||||
The result of the help command, if any.
|
||||
"""
|
||||
from .core import Command, Group, wrap_callback
|
||||
from .errors import CommandError
|
||||
|
||||
bot = self.bot
|
||||
cmd = bot.help_command
|
||||
|
||||
if cmd is None:
|
||||
return None
|
||||
|
||||
cmd = cmd.copy()
|
||||
cmd.context = self
|
||||
if len(args) == 0:
|
||||
await cmd.prepare_help_command(self, None)
|
||||
mapping = cmd.get_bot_mapping()
|
||||
injected = wrap_callback(cmd.send_bot_help)
|
||||
try:
|
||||
return await injected(mapping)
|
||||
except CommandError as e:
|
||||
await cmd.on_help_command_error(self, e)
|
||||
return None
|
||||
|
||||
entity = args[0]
|
||||
if isinstance(entity, str):
|
||||
entity = bot.get_cog(entity) or bot.get_command(entity)
|
||||
|
||||
if entity is None:
|
||||
return None
|
||||
|
||||
try:
|
||||
entity.qualified_name
|
||||
except AttributeError:
|
||||
# if we're here then it's not a cog, group, or command.
|
||||
return None
|
||||
|
||||
await cmd.prepare_help_command(self, entity.qualified_name)
|
||||
|
||||
try:
|
||||
if hasattr(entity, "__cog_commands__"):
|
||||
injected = wrap_callback(cmd.send_cog_help)
|
||||
return await injected(entity)
|
||||
elif isinstance(entity, Group):
|
||||
injected = wrap_callback(cmd.send_group_help)
|
||||
return await injected(entity)
|
||||
elif isinstance(entity, Command):
|
||||
injected = wrap_callback(cmd.send_command_help)
|
||||
return await injected(entity)
|
||||
else:
|
||||
return None
|
||||
except CommandError as e:
|
||||
await cmd.on_help_command_error(self, e)
|
||||
|
||||
@discord.utils.copy_doc(Message.reply)
|
||||
async def reply(self, content: str | None = None, **kwargs: Any) -> Message:
|
||||
return await self.message.reply(content, **kwargs)
|
||||
|
||||
@discord.utils.copy_doc(Message.forward_to)
|
||||
async def forward_to(
|
||||
self, channel: discord.abc.Messageable, **kwargs: Any
|
||||
) -> Message:
|
||||
return await self.message.forward_to(channel, **kwargs)
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,409 @@
|
||||
"""
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2015-2021 Rapptz
|
||||
Copyright (c) 2021-present Pycord Development
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a
|
||||
copy of this software and associated documentation files (the "Software"),
|
||||
to deal in the Software without restriction, including without limitation
|
||||
the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
and/or sell copies of the Software, and to permit persons to whom the
|
||||
Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
||||
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
DEALINGS IN THE SOFTWARE.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import time
|
||||
from collections import deque
|
||||
from typing import TYPE_CHECKING, Any, Callable, Deque, TypeVar
|
||||
|
||||
import discord.abc
|
||||
from discord.enums import Enum
|
||||
from discord.utils import _get_event_loop
|
||||
|
||||
from ...abc import PrivateChannel
|
||||
from .errors import MaxConcurrencyReached
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from ...message import Message
|
||||
|
||||
__all__ = (
|
||||
"BucketType",
|
||||
"Cooldown",
|
||||
"CooldownMapping",
|
||||
"DynamicCooldownMapping",
|
||||
"MaxConcurrency",
|
||||
)
|
||||
|
||||
C = TypeVar("C", bound="CooldownMapping")
|
||||
MC = TypeVar("MC", bound="MaxConcurrency")
|
||||
|
||||
|
||||
class BucketType(Enum):
|
||||
default = 0
|
||||
user = 1
|
||||
guild = 2
|
||||
channel = 3
|
||||
member = 4
|
||||
category = 5
|
||||
role = 6
|
||||
|
||||
def get_key(self, msg: Message) -> Any:
|
||||
if self is BucketType.user:
|
||||
return msg.author.id
|
||||
elif self is BucketType.guild:
|
||||
return (msg.guild or msg.author).id
|
||||
elif self is BucketType.channel:
|
||||
return msg.channel.id
|
||||
elif self is BucketType.member:
|
||||
return (msg.guild and msg.guild.id), msg.author.id
|
||||
elif self is BucketType.category:
|
||||
return (
|
||||
msg.channel.category.id
|
||||
if isinstance(msg.channel, discord.abc.GuildChannel)
|
||||
and msg.channel.category
|
||||
else msg.channel.id
|
||||
)
|
||||
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
|
||||
# receiving 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
|
||||
|
||||
def __call__(self, msg: Message) -> Any:
|
||||
return self.get_key(msg)
|
||||
|
||||
|
||||
class Cooldown:
|
||||
"""Represents a cooldown for a command.
|
||||
|
||||
Attributes
|
||||
----------
|
||||
rate: :class:`int`
|
||||
The total number of tokens available per :attr:`per` seconds.
|
||||
per: :class:`float`
|
||||
The length of the cooldown period in seconds.
|
||||
"""
|
||||
|
||||
__slots__ = ("rate", "per", "_window", "_tokens", "_last")
|
||||
|
||||
def __init__(self, rate: float, per: float) -> None:
|
||||
self.rate: int = int(rate)
|
||||
self.per: float = float(per)
|
||||
self._window: float = 0.0
|
||||
self._tokens: int = self.rate
|
||||
self._last: float = 0.0
|
||||
|
||||
def get_tokens(self, current: float | None = None) -> int:
|
||||
"""Returns the number of available tokens before rate limiting is applied.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
current: Optional[:class:`float`]
|
||||
The time in seconds since Unix epoch to calculate tokens at.
|
||||
If not supplied then :func:`time.time()` is used.
|
||||
|
||||
Returns
|
||||
-------
|
||||
:class:`int`
|
||||
The number of tokens available before the cooldown is to be applied.
|
||||
"""
|
||||
if not current:
|
||||
current = time.time()
|
||||
|
||||
tokens = self._tokens
|
||||
|
||||
if current > self._window + self.per:
|
||||
tokens = self.rate
|
||||
return tokens
|
||||
|
||||
def get_retry_after(self, current: float | None = None) -> float:
|
||||
"""Returns the time in seconds until the cooldown will be reset.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
current: Optional[:class:`float`]
|
||||
The current time in seconds since Unix epoch.
|
||||
If not supplied, then :func:`time.time()` is used.
|
||||
|
||||
Returns
|
||||
-------
|
||||
:class:`float`
|
||||
The number of seconds to wait before this cooldown will be reset.
|
||||
"""
|
||||
current = current or time.time()
|
||||
tokens = self.get_tokens(current)
|
||||
|
||||
if tokens == 0:
|
||||
return self.per - (current - self._window)
|
||||
|
||||
return 0.0
|
||||
|
||||
def update_rate_limit(self, current: float | None = None) -> float | None:
|
||||
"""Updates the cooldown rate limit.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
current: Optional[:class:`float`]
|
||||
The time in seconds since Unix epoch to update the rate limit at.
|
||||
If not supplied, then :func:`time.time()` is used.
|
||||
|
||||
Returns
|
||||
-------
|
||||
Optional[:class:`float`]
|
||||
The retry-after time in seconds if rate limited.
|
||||
"""
|
||||
current = current or time.time()
|
||||
self._last = current
|
||||
|
||||
self._tokens = self.get_tokens(current)
|
||||
|
||||
# first token used means that we start a new rate limit window
|
||||
if self._tokens == self.rate:
|
||||
self._window = current
|
||||
|
||||
# check if we are rate limited
|
||||
if self._tokens == 0:
|
||||
return self.per - (current - self._window)
|
||||
|
||||
# we're not so decrement our tokens
|
||||
self._tokens -= 1
|
||||
|
||||
def reset(self) -> None:
|
||||
"""Reset the cooldown to its initial state."""
|
||||
self._tokens = self.rate
|
||||
self._last = 0.0
|
||||
|
||||
def copy(self) -> Cooldown:
|
||||
"""Creates a copy of this cooldown.
|
||||
|
||||
Returns
|
||||
-------
|
||||
:class:`Cooldown`
|
||||
A new instance of this cooldown.
|
||||
"""
|
||||
return Cooldown(self.rate, self.per)
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return (
|
||||
f"<Cooldown rate: {self.rate} per: {self.per} window:"
|
||||
f" {self._window} tokens: {self._tokens}>"
|
||||
)
|
||||
|
||||
|
||||
class CooldownMapping:
|
||||
def __init__(
|
||||
self,
|
||||
original: Cooldown | None,
|
||||
type: Callable[[Message], Any],
|
||||
) -> None:
|
||||
if not callable(type):
|
||||
raise TypeError("Cooldown type must be a BucketType or callable")
|
||||
|
||||
self._cache: dict[Any, Cooldown] = {}
|
||||
self._cooldown: Cooldown | None = original
|
||||
self._type: Callable[[Message], Any] = type
|
||||
|
||||
def copy(self) -> CooldownMapping:
|
||||
ret = CooldownMapping(self._cooldown, self._type)
|
||||
ret._cache = self._cache.copy()
|
||||
return ret
|
||||
|
||||
@property
|
||||
def valid(self) -> bool:
|
||||
return self._cooldown is not None
|
||||
|
||||
@property
|
||||
def type(self) -> Callable[[Message], Any]:
|
||||
return self._type
|
||||
|
||||
@classmethod
|
||||
def from_cooldown(cls: type[C], rate, per, type) -> C:
|
||||
return cls(Cooldown(rate, per), type)
|
||||
|
||||
def _bucket_key(self, msg: Message) -> Any:
|
||||
return self._type(msg)
|
||||
|
||||
def _verify_cache_integrity(self, current: float | None = None) -> None:
|
||||
# we want to delete all cache objects that haven't been used
|
||||
# in a cooldown window. e.g. if we have a command that has a
|
||||
# cooldown of 60s, and it has not been used in 60s then that key should be deleted
|
||||
current = current or time.time()
|
||||
dead_keys = [k for k, v in self._cache.items() if current > v._last + v.per]
|
||||
for k in dead_keys:
|
||||
del self._cache[k]
|
||||
|
||||
def create_bucket(self, message: Message) -> Cooldown:
|
||||
return self._cooldown.copy() # type: ignore
|
||||
|
||||
def get_bucket(self, message: Message, current: float | None = None) -> Cooldown:
|
||||
if self._type is BucketType.default:
|
||||
return self._cooldown # type: ignore
|
||||
|
||||
self._verify_cache_integrity(current)
|
||||
key = self._bucket_key(message)
|
||||
if key not in self._cache:
|
||||
bucket = self.create_bucket(message)
|
||||
if bucket is not None:
|
||||
self._cache[key] = bucket
|
||||
else:
|
||||
bucket = self._cache[key]
|
||||
|
||||
return bucket
|
||||
|
||||
def update_rate_limit(
|
||||
self, message: Message, current: float | None = None
|
||||
) -> float | None:
|
||||
bucket = self.get_bucket(message, current)
|
||||
return bucket.update_rate_limit(current)
|
||||
|
||||
|
||||
class DynamicCooldownMapping(CooldownMapping):
|
||||
def __init__(
|
||||
self, factory: Callable[[Message], Cooldown], type: Callable[[Message], Any]
|
||||
) -> None:
|
||||
super().__init__(None, type)
|
||||
self._factory: Callable[[Message], Cooldown] = factory
|
||||
|
||||
def copy(self) -> DynamicCooldownMapping:
|
||||
ret = DynamicCooldownMapping(self._factory, self._type)
|
||||
ret._cache = self._cache.copy()
|
||||
return ret
|
||||
|
||||
@property
|
||||
def valid(self) -> bool:
|
||||
return True
|
||||
|
||||
def create_bucket(self, message: Message) -> Cooldown:
|
||||
return self._factory(message)
|
||||
|
||||
|
||||
class _Semaphore:
|
||||
"""This class is a version of a semaphore.
|
||||
|
||||
If you're wondering why asyncio.Semaphore isn't being used,
|
||||
it's because it doesn't expose the internal value. This internal
|
||||
value is necessary because I need to support both `wait=True` and
|
||||
`wait=False`.
|
||||
|
||||
An asyncio.Queue could have been used to do this as well -- but it is
|
||||
not as efficient since internally that uses two queues and is a bit
|
||||
overkill for what is basically a counter.
|
||||
"""
|
||||
|
||||
__slots__ = ("value", "loop", "_waiters")
|
||||
|
||||
def __init__(self, number: int) -> None:
|
||||
self.value: int = number
|
||||
self.loop: asyncio.AbstractEventLoop = _get_event_loop()
|
||||
self._waiters: Deque[asyncio.Future] = deque()
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return f"<_Semaphore value={self.value} waiters={len(self._waiters)}>"
|
||||
|
||||
def locked(self) -> bool:
|
||||
return self.value == 0
|
||||
|
||||
def is_active(self) -> bool:
|
||||
return len(self._waiters) > 0
|
||||
|
||||
def wake_up(self) -> None:
|
||||
while self._waiters:
|
||||
future = self._waiters.popleft()
|
||||
if not future.done():
|
||||
future.set_result(None)
|
||||
return
|
||||
|
||||
async def acquire(self, *, wait: bool = False) -> bool:
|
||||
if not wait and self.value <= 0:
|
||||
# signal that we're not acquiring
|
||||
return False
|
||||
|
||||
while self.value <= 0:
|
||||
future = self.loop.create_future()
|
||||
self._waiters.append(future)
|
||||
try:
|
||||
await future
|
||||
except:
|
||||
future.cancel()
|
||||
if self.value > 0 and not future.cancelled():
|
||||
self.wake_up()
|
||||
raise
|
||||
|
||||
self.value -= 1
|
||||
return True
|
||||
|
||||
def release(self) -> None:
|
||||
self.value += 1
|
||||
self.wake_up()
|
||||
|
||||
|
||||
class MaxConcurrency:
|
||||
__slots__ = ("number", "per", "wait", "_mapping")
|
||||
|
||||
def __init__(self, number: int, *, per: BucketType, wait: bool) -> None:
|
||||
self._mapping: dict[Any, _Semaphore] = {}
|
||||
self.per: BucketType = per
|
||||
self.number: int = number
|
||||
self.wait: bool = wait
|
||||
|
||||
if number <= 0:
|
||||
raise ValueError("max_concurrency 'number' cannot be less than 1")
|
||||
|
||||
if not isinstance(per, BucketType):
|
||||
raise TypeError(
|
||||
f"max_concurrency 'per' must be of type BucketType not {type(per)!r}"
|
||||
)
|
||||
|
||||
def copy(self: MC) -> MC:
|
||||
return self.__class__(self.number, per=self.per, wait=self.wait)
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return (
|
||||
f"<MaxConcurrency per={self.per!r} number={self.number} wait={self.wait}>"
|
||||
)
|
||||
|
||||
def get_key(self, message: Message) -> Any:
|
||||
return self.per.get_key(message)
|
||||
|
||||
async def acquire(self, message: Message) -> None:
|
||||
key = self.get_key(message)
|
||||
|
||||
try:
|
||||
sem = self._mapping[key]
|
||||
except KeyError:
|
||||
self._mapping[key] = sem = _Semaphore(self.number)
|
||||
|
||||
acquired = await sem.acquire(wait=self.wait)
|
||||
if not acquired:
|
||||
raise MaxConcurrencyReached(self.number, self.per)
|
||||
|
||||
async def release(self, message: Message) -> None:
|
||||
# Technically there's no reason for this function to be async
|
||||
# But it might be more useful in the future
|
||||
key = self.get_key(message)
|
||||
|
||||
try:
|
||||
sem = self._mapping[key]
|
||||
except KeyError:
|
||||
# ...? peculiar
|
||||
return
|
||||
else:
|
||||
sem.release()
|
||||
|
||||
if sem.value >= self.number and not sem.is_active():
|
||||
del self._mapping[key]
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,700 @@
|
||||
"""
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2015-2021 Rapptz
|
||||
Copyright (c) 2021-present Pycord Development
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a
|
||||
copy of this software and associated documentation files (the "Software"),
|
||||
to deal in the Software without restriction, including without limitation
|
||||
the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
and/or sell copies of the Software, and to permit persons to whom the
|
||||
Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
||||
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
DEALINGS IN THE SOFTWARE.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import inspect
|
||||
import re
|
||||
import sys
|
||||
from dataclasses import dataclass, field
|
||||
from typing import TYPE_CHECKING, Any, Iterator, Literal, Pattern, TypeVar, Union
|
||||
|
||||
from discord.utils import (
|
||||
MISSING,
|
||||
maybe_coroutine,
|
||||
resolve_annotation,
|
||||
)
|
||||
|
||||
from .converter import run_converters
|
||||
from .errors import (
|
||||
BadFlagArgument,
|
||||
CommandError,
|
||||
MissingFlagArgument,
|
||||
MissingRequiredFlag,
|
||||
TooManyFlags,
|
||||
)
|
||||
from .view import StringView
|
||||
|
||||
__all__ = (
|
||||
"Flag",
|
||||
"flag",
|
||||
"FlagConverter",
|
||||
)
|
||||
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from .context import Context
|
||||
|
||||
|
||||
def _missing_field_factory() -> field:
|
||||
return field(default_factory=lambda: MISSING)
|
||||
|
||||
|
||||
@dataclass
|
||||
class Flag:
|
||||
"""Represents a flag parameter for :class:`FlagConverter`.
|
||||
|
||||
The :func:`~discord.ext.commands.flag` function helps
|
||||
create these flag objects, but it is not necessary to
|
||||
do so. These cannot be constructed manually.
|
||||
|
||||
Attributes
|
||||
----------
|
||||
name: :class:`str`
|
||||
The name of the flag.
|
||||
aliases: List[:class:`str`]
|
||||
The aliases of the flag name.
|
||||
attribute: :class:`str`
|
||||
The attribute in the class that corresponds to this flag.
|
||||
default: Any
|
||||
The default value of the flag, if available.
|
||||
annotation: Any
|
||||
The underlying evaluated annotation of the flag.
|
||||
max_args: :class:`int`
|
||||
The maximum number of arguments the flag can accept.
|
||||
A negative value indicates an unlimited amount of arguments.
|
||||
positional: :class:`bool`
|
||||
Whether the flag is positional.
|
||||
A :class:`FlagConverter` can only handle one positional flag.
|
||||
override: :class:`bool`
|
||||
Whether multiple given values overrides the previous value.
|
||||
"""
|
||||
|
||||
name: str = _missing_field_factory()
|
||||
aliases: list[str] = field(default_factory=list)
|
||||
attribute: str = _missing_field_factory()
|
||||
annotation: Any = _missing_field_factory()
|
||||
default: Any = _missing_field_factory()
|
||||
max_args: int = _missing_field_factory()
|
||||
positional: bool = _missing_field_factory()
|
||||
override: bool = _missing_field_factory()
|
||||
cast_to_dict: bool = False
|
||||
|
||||
@property
|
||||
def required(self) -> bool:
|
||||
"""Whether the flag is required.
|
||||
|
||||
A required flag has no default value.
|
||||
"""
|
||||
return self.default is MISSING
|
||||
|
||||
|
||||
def flag(
|
||||
*,
|
||||
name: str = MISSING,
|
||||
aliases: list[str] = MISSING,
|
||||
default: Any = MISSING,
|
||||
max_args: int = MISSING,
|
||||
override: bool = MISSING,
|
||||
positional: bool = MISSING,
|
||||
) -> Any:
|
||||
"""Override default functionality and parameters of the underlying :class:`FlagConverter`
|
||||
class attributes.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
name: :class:`str`
|
||||
The flag name. If not given, defaults to the attribute name.
|
||||
aliases: List[:class:`str`]
|
||||
Aliases to the flag name. If not given, no aliases are set.
|
||||
default: Any
|
||||
The default parameter. This could be either a value or a callable that takes
|
||||
:class:`Context` as its sole parameter. If not given then it defaults to
|
||||
the default value given to the attribute.
|
||||
max_args: :class:`int`
|
||||
The maximum number of arguments the flag can accept.
|
||||
A negative value indicates an unlimited amount of arguments.
|
||||
The default value depends on the annotation given.
|
||||
override: :class:`bool`
|
||||
Whether multiple given values overrides the previous value. The default
|
||||
value depends on the annotation given.
|
||||
positional: :class:`bool`
|
||||
Whether the flag is positional or not. There can only be one positional flag.
|
||||
"""
|
||||
return Flag(
|
||||
name=name,
|
||||
aliases=aliases,
|
||||
default=default,
|
||||
max_args=max_args,
|
||||
override=override,
|
||||
positional=positional,
|
||||
)
|
||||
|
||||
|
||||
def validate_flag_name(name: str, forbidden: set[str]):
|
||||
if not name:
|
||||
raise ValueError("flag names should not be empty")
|
||||
|
||||
for ch in name:
|
||||
if ch.isspace():
|
||||
raise ValueError(f"flag name {name!r} cannot have spaces")
|
||||
if ch == "\\":
|
||||
raise ValueError(f"flag name {name!r} cannot have backslashes")
|
||||
if ch in forbidden:
|
||||
raise ValueError(
|
||||
f"flag name {name!r} cannot have any of {forbidden!r} within them"
|
||||
)
|
||||
|
||||
|
||||
def get_flags(
|
||||
namespace: dict[str, Any], globals: dict[str, Any], locals: dict[str, Any]
|
||||
) -> dict[str, Flag]:
|
||||
annotations = namespace.get("__annotations__", {})
|
||||
case_insensitive = namespace["__commands_flag_case_insensitive__"]
|
||||
flags: dict[str, Flag] = {}
|
||||
cache: dict[str, Any] = {}
|
||||
names: set[str] = set()
|
||||
positional: Flag | None = None
|
||||
for name, annotation in annotations.items():
|
||||
flag = namespace.pop(name, MISSING)
|
||||
if isinstance(flag, Flag):
|
||||
flag.annotation = annotation
|
||||
else:
|
||||
flag = Flag(name=name, annotation=annotation, default=flag)
|
||||
|
||||
flag.attribute = name
|
||||
if flag.name is MISSING:
|
||||
flag.name = name
|
||||
|
||||
if flag.positional:
|
||||
if positional is not None:
|
||||
raise TypeError(
|
||||
f"{flag.name!r} positional flag conflicts with {positional.name!r} flag."
|
||||
)
|
||||
|
||||
positional = flag
|
||||
|
||||
annotation = flag.annotation = resolve_annotation(
|
||||
flag.annotation, globals, locals, cache
|
||||
)
|
||||
|
||||
if (
|
||||
flag.default is MISSING
|
||||
and hasattr(annotation, "__commands_is_flag__")
|
||||
and annotation._can_be_constructible()
|
||||
):
|
||||
flag.default = annotation._construct_default
|
||||
|
||||
if flag.aliases is MISSING:
|
||||
flag.aliases = []
|
||||
|
||||
# Add sensible defaults based off of the type annotation
|
||||
# <type> -> (max_args=1)
|
||||
# List[str] -> (max_args=-1)
|
||||
# Tuple[int, ...] -> (max_args=1)
|
||||
# Dict[K, V] -> (max_args=-1, override=True)
|
||||
# Union[str, int] -> (max_args=1)
|
||||
# Optional[str] -> (default=None, max_args=1)
|
||||
|
||||
try:
|
||||
origin = annotation.__origin__
|
||||
except AttributeError:
|
||||
# A regular type hint
|
||||
if flag.max_args is MISSING:
|
||||
flag.max_args = 1
|
||||
else:
|
||||
if origin is Union:
|
||||
# typing.Union
|
||||
if flag.max_args is MISSING:
|
||||
flag.max_args = 1
|
||||
if annotation.__args__[-1] is type(None) and flag.default is MISSING:
|
||||
# typing.Optional
|
||||
flag.default = None
|
||||
elif origin is tuple:
|
||||
# typing.Tuple
|
||||
# tuple parsing is e.g. `flag: peter 20`
|
||||
# for Tuple[str, int] would give you flag: ('peter', 20)
|
||||
if flag.max_args is MISSING:
|
||||
flag.max_args = 1
|
||||
elif origin is list:
|
||||
# typing.List
|
||||
if flag.max_args is MISSING:
|
||||
flag.max_args = -1
|
||||
elif origin is dict:
|
||||
# typing.Dict[K, V]
|
||||
# Equivalent to:
|
||||
# typing.List[typing.Tuple[K, V]]
|
||||
flag.cast_to_dict = True
|
||||
if flag.max_args is MISSING:
|
||||
flag.max_args = -1
|
||||
if flag.override is MISSING:
|
||||
flag.override = True
|
||||
elif origin is Literal:
|
||||
if flag.max_args is MISSING:
|
||||
flag.max_args = 1
|
||||
else:
|
||||
raise TypeError(
|
||||
f"Unsupported typing annotation {annotation!r} for"
|
||||
f" {flag.name!r} flag"
|
||||
)
|
||||
|
||||
if flag.override is MISSING:
|
||||
flag.override = False
|
||||
|
||||
# Validate flag names are unique
|
||||
name = flag.name.casefold() if case_insensitive else flag.name
|
||||
if name in names:
|
||||
raise TypeError(
|
||||
f"{flag.name!r} flag conflicts with previous flag or alias."
|
||||
)
|
||||
else:
|
||||
names.add(name)
|
||||
|
||||
for alias in flag.aliases:
|
||||
# Validate alias is unique
|
||||
alias = alias.casefold() if case_insensitive else alias
|
||||
if alias in names:
|
||||
raise TypeError(
|
||||
f"{flag.name!r} flag alias {alias!r} conflicts with previous flag"
|
||||
" or alias."
|
||||
)
|
||||
else:
|
||||
names.add(alias)
|
||||
|
||||
flags[flag.name] = flag
|
||||
|
||||
return flags
|
||||
|
||||
|
||||
class FlagsMeta(type):
|
||||
if TYPE_CHECKING:
|
||||
__commands_is_flag__: bool
|
||||
__commands_flags__: dict[str, Flag]
|
||||
__commands_flag_aliases__: dict[str, str]
|
||||
__commands_flag_regex__: Pattern[str]
|
||||
__commands_flag_case_insensitive__: bool
|
||||
__commands_flag_delimiter__: str
|
||||
__commands_flag_prefix__: str
|
||||
__commands_flag_positional__: Flag | None
|
||||
|
||||
def __new__(
|
||||
cls: type[type],
|
||||
name: str,
|
||||
bases: tuple[type, ...],
|
||||
attrs: dict[str, Any],
|
||||
*,
|
||||
case_insensitive: bool = MISSING,
|
||||
delimiter: str = MISSING,
|
||||
prefix: str = MISSING,
|
||||
):
|
||||
attrs["__commands_is_flag__"] = True
|
||||
|
||||
try:
|
||||
global_ns = sys.modules[attrs["__module__"]].__dict__
|
||||
except KeyError:
|
||||
global_ns = {}
|
||||
|
||||
frame = inspect.currentframe()
|
||||
try:
|
||||
if frame is None:
|
||||
local_ns = {}
|
||||
else:
|
||||
if frame.f_back is None:
|
||||
local_ns = frame.f_locals
|
||||
else:
|
||||
local_ns = frame.f_back.f_locals
|
||||
finally:
|
||||
del frame
|
||||
|
||||
flags: dict[str, Flag] = {}
|
||||
aliases: dict[str, str] = {}
|
||||
for base in reversed(bases):
|
||||
if base.__dict__.get("__commands_is_flag__", False):
|
||||
flags.update(base.__dict__["__commands_flags__"])
|
||||
aliases.update(base.__dict__["__commands_flag_aliases__"])
|
||||
if case_insensitive is MISSING:
|
||||
attrs["__commands_flag_case_insensitive__"] = base.__dict__[
|
||||
"__commands_flag_case_insensitive__"
|
||||
]
|
||||
if delimiter is MISSING:
|
||||
attrs["__commands_flag_delimiter__"] = base.__dict__[
|
||||
"__commands_flag_delimiter__"
|
||||
]
|
||||
if prefix is MISSING:
|
||||
attrs["__commands_flag_prefix__"] = base.__dict__[
|
||||
"__commands_flag_prefix__"
|
||||
]
|
||||
|
||||
if case_insensitive is not MISSING:
|
||||
attrs["__commands_flag_case_insensitive__"] = case_insensitive
|
||||
if delimiter is not MISSING:
|
||||
attrs["__commands_flag_delimiter__"] = delimiter
|
||||
if prefix is not MISSING:
|
||||
attrs["__commands_flag_prefix__"] = prefix
|
||||
|
||||
case_insensitive = attrs.setdefault("__commands_flag_case_insensitive__", False)
|
||||
delimiter = attrs.setdefault("__commands_flag_delimiter__", ":")
|
||||
prefix = attrs.setdefault("__commands_flag_prefix__", "")
|
||||
|
||||
positional_flag: Flag | None = None
|
||||
for flag_name, flag in get_flags(attrs, global_ns, local_ns).items():
|
||||
flags[flag_name] = flag
|
||||
if flag.positional:
|
||||
positional_flag = flag
|
||||
aliases.update({alias_name: flag_name for alias_name in flag.aliases})
|
||||
attrs["__commands_flag_positional__"] = positional_flag
|
||||
|
||||
forbidden = set(delimiter).union(prefix)
|
||||
for flag_name in flags:
|
||||
validate_flag_name(flag_name, forbidden)
|
||||
for alias_name in aliases:
|
||||
validate_flag_name(alias_name, forbidden)
|
||||
|
||||
regex_flags = 0
|
||||
if case_insensitive:
|
||||
flags = {key.casefold(): value for key, value in flags.items()}
|
||||
aliases = {
|
||||
key.casefold(): value.casefold() for key, value in aliases.items()
|
||||
}
|
||||
regex_flags = re.IGNORECASE
|
||||
|
||||
keys = [re.escape(k) for k in flags]
|
||||
keys.extend(re.escape(a) for a in aliases)
|
||||
keys = sorted(keys, key=len, reverse=True)
|
||||
|
||||
joined = "|".join(keys)
|
||||
pattern = re.compile(
|
||||
f"(({re.escape(prefix)})(?P<flag>{joined}){re.escape(delimiter)})",
|
||||
regex_flags,
|
||||
)
|
||||
attrs["__commands_flag_regex__"] = pattern
|
||||
attrs["__commands_flags__"] = flags
|
||||
attrs["__commands_flag_aliases__"] = aliases
|
||||
|
||||
return type.__new__(cls, name, bases, attrs)
|
||||
|
||||
|
||||
async def tuple_convert_all(
|
||||
ctx: Context, argument: str, flag: Flag, converter: Any
|
||||
) -> tuple[Any, ...]:
|
||||
view = StringView(argument)
|
||||
results = []
|
||||
param: inspect.Parameter = ctx.current_parameter # type: ignore
|
||||
while not view.eof:
|
||||
view.skip_ws()
|
||||
if view.eof:
|
||||
break
|
||||
|
||||
word = view.get_quoted_word()
|
||||
if word is None:
|
||||
break
|
||||
|
||||
try:
|
||||
converted = await run_converters(ctx, converter, word, param)
|
||||
except CommandError:
|
||||
raise
|
||||
except Exception as e:
|
||||
raise BadFlagArgument(flag) from e
|
||||
else:
|
||||
results.append(converted)
|
||||
|
||||
return tuple(results)
|
||||
|
||||
|
||||
async def tuple_convert_flag(
|
||||
ctx: Context, argument: str, flag: Flag, converters: Any
|
||||
) -> tuple[Any, ...]:
|
||||
view = StringView(argument)
|
||||
results = []
|
||||
param: inspect.Parameter = ctx.current_parameter # type: ignore
|
||||
for converter in converters:
|
||||
view.skip_ws()
|
||||
if view.eof:
|
||||
break
|
||||
|
||||
word = view.get_quoted_word()
|
||||
if word is None:
|
||||
break
|
||||
|
||||
try:
|
||||
converted = await run_converters(ctx, converter, word, param)
|
||||
except CommandError:
|
||||
raise
|
||||
except Exception as e:
|
||||
raise BadFlagArgument(flag) from e
|
||||
else:
|
||||
results.append(converted)
|
||||
|
||||
if len(results) != len(converters):
|
||||
raise BadFlagArgument(flag)
|
||||
|
||||
return tuple(results)
|
||||
|
||||
|
||||
async def convert_flag(ctx, argument: str, flag: Flag, annotation: Any = None) -> Any:
|
||||
param: inspect.Parameter = ctx.current_parameter # type: ignore
|
||||
annotation = annotation or flag.annotation
|
||||
try:
|
||||
origin = annotation.__origin__
|
||||
except AttributeError:
|
||||
pass
|
||||
else:
|
||||
if origin is tuple:
|
||||
if annotation.__args__[-1] is Ellipsis:
|
||||
return await tuple_convert_all(
|
||||
ctx, argument, flag, annotation.__args__[0]
|
||||
)
|
||||
else:
|
||||
return await tuple_convert_flag(
|
||||
ctx, argument, flag, annotation.__args__
|
||||
)
|
||||
elif origin is list:
|
||||
# typing.List[x]
|
||||
annotation = annotation.__args__[0]
|
||||
return await convert_flag(ctx, argument, flag, annotation)
|
||||
elif origin is Union and annotation.__args__[-1] is type(None):
|
||||
# typing.Optional[x]
|
||||
annotation = Union[annotation.__args__[:-1]]
|
||||
return await run_converters(ctx, annotation, argument, param)
|
||||
elif origin is dict:
|
||||
# typing.Dict[K, V] -> typing.Tuple[K, V]
|
||||
return await tuple_convert_flag(ctx, argument, flag, annotation.__args__)
|
||||
|
||||
try:
|
||||
return await run_converters(ctx, annotation, argument, param)
|
||||
except CommandError:
|
||||
raise
|
||||
except Exception as e:
|
||||
raise BadFlagArgument(flag) from e
|
||||
|
||||
|
||||
F = TypeVar("F", bound="FlagConverter")
|
||||
|
||||
|
||||
class FlagConverter(metaclass=FlagsMeta):
|
||||
"""A converter that allows for a user-friendly flag syntax.
|
||||
|
||||
The flags are defined using :pep:`526` type annotations similar
|
||||
to the :mod:`dataclasses` Python module. For more information on
|
||||
how this converter works, check the appropriate
|
||||
:ref:`documentation <ext_commands_flag_converter>`.
|
||||
|
||||
.. container:: operations
|
||||
|
||||
.. describe:: iter(x)
|
||||
|
||||
Returns an iterator of ``(flag_name, flag_value)`` pairs. This allows it
|
||||
to be, for example, constructed as a dict or a list of pairs.
|
||||
Note that aliases are not shown.
|
||||
|
||||
.. versionadded:: 2.0
|
||||
|
||||
Parameters
|
||||
----------
|
||||
case_insensitive: :class:`bool`
|
||||
A class parameter to toggle case insensitivity of the flag parsing.
|
||||
If ``True`` then flags are parsed in a case-insensitive manner.
|
||||
Defaults to ``False``.
|
||||
prefix: :class:`str`
|
||||
The prefix that all flags must be prefixed with. By default,
|
||||
there is no prefix.
|
||||
delimiter: :class:`str`
|
||||
The delimiter that separates a flag's argument from the flag's name.
|
||||
By default, this is ``:``.
|
||||
"""
|
||||
|
||||
@classmethod
|
||||
def get_flags(cls) -> dict[str, Flag]:
|
||||
"""A mapping of flag name to flag object this converter has."""
|
||||
return cls.__commands_flags__.copy()
|
||||
|
||||
@classmethod
|
||||
def _can_be_constructible(cls) -> bool:
|
||||
return all(not flag.required for flag in cls.__commands_flags__.values())
|
||||
|
||||
def __iter__(self) -> Iterator[tuple[str, Any]]:
|
||||
for flag in self.__class__.__commands_flags__.values():
|
||||
yield flag.name, getattr(self, flag.attribute)
|
||||
|
||||
@classmethod
|
||||
async def _construct_default(cls: type[F], ctx: Context) -> F:
|
||||
self: F = cls.__new__(cls)
|
||||
flags = cls.__commands_flags__
|
||||
for flag in flags.values():
|
||||
if callable(flag.default):
|
||||
default = await maybe_coroutine(flag.default, ctx)
|
||||
setattr(self, flag.attribute, default)
|
||||
else:
|
||||
setattr(self, flag.attribute, flag.default)
|
||||
return self
|
||||
|
||||
def __repr__(self) -> str:
|
||||
pairs = " ".join(
|
||||
[
|
||||
f"{flag.attribute}={getattr(self, flag.attribute)!r}"
|
||||
for flag in self.get_flags().values()
|
||||
]
|
||||
)
|
||||
return f"<{self.__class__.__name__} {pairs}>"
|
||||
|
||||
@classmethod
|
||||
def parse_flags(cls, argument: str) -> dict[str, list[str]]:
|
||||
result: dict[str, list[str]] = {}
|
||||
flags = cls.__commands_flags__
|
||||
aliases = cls.__commands_flag_aliases__
|
||||
positional_flag = cls.__commands_flag_positional__
|
||||
last_position = 0
|
||||
last_flag: Flag | None = None
|
||||
|
||||
case_insensitive = cls.__commands_flag_case_insensitive__
|
||||
|
||||
if positional_flag is not None:
|
||||
match = cls.__commands_flag_regex__.search(argument)
|
||||
if match is not None:
|
||||
begin, end = match.span(0)
|
||||
value = argument[:begin].strip()
|
||||
else:
|
||||
value = argument.strip()
|
||||
last_position = len(argument)
|
||||
|
||||
if value:
|
||||
name = (
|
||||
positional_flag.name.casefold()
|
||||
if case_insensitive
|
||||
else positional_flag.name
|
||||
)
|
||||
result[name] = [value]
|
||||
|
||||
for match in cls.__commands_flag_regex__.finditer(argument):
|
||||
begin, end = match.span(0)
|
||||
key = match.group("flag")
|
||||
if case_insensitive:
|
||||
key = key.casefold()
|
||||
|
||||
if key in aliases:
|
||||
key = aliases[key]
|
||||
|
||||
flag = flags.get(key)
|
||||
if last_position and last_flag is not None:
|
||||
value = argument[last_position : begin - 1].lstrip()
|
||||
if not value:
|
||||
raise MissingFlagArgument(last_flag)
|
||||
|
||||
try:
|
||||
values = result[last_flag.name]
|
||||
except KeyError:
|
||||
result[last_flag.name] = [value]
|
||||
else:
|
||||
values.append(value)
|
||||
|
||||
last_position = end
|
||||
last_flag = flag
|
||||
|
||||
# Add the remaining string to the last available flag
|
||||
if last_position and last_flag is not None:
|
||||
value = argument[last_position:].strip()
|
||||
if not value:
|
||||
raise MissingFlagArgument(last_flag)
|
||||
|
||||
try:
|
||||
values = result[last_flag.name]
|
||||
except KeyError:
|
||||
result[last_flag.name] = [value]
|
||||
else:
|
||||
values.append(value)
|
||||
|
||||
# Verification of values will come at a later stage
|
||||
return result
|
||||
|
||||
@classmethod
|
||||
async def convert(cls: type[F], ctx: Context, argument: str) -> F:
|
||||
"""|coro|
|
||||
|
||||
The method that actually converters an argument to the flag mapping.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
cls: Type[:class:`FlagConverter`]
|
||||
The flag converter class.
|
||||
ctx: :class:`Context`
|
||||
The invocation context.
|
||||
argument: :class:`str`
|
||||
The argument to convert from.
|
||||
|
||||
Returns
|
||||
-------
|
||||
:class:`FlagConverter`
|
||||
The flag converter instance with all flags parsed.
|
||||
|
||||
Raises
|
||||
------
|
||||
FlagError
|
||||
A flag related parsing error.
|
||||
CommandError
|
||||
A command related error.
|
||||
"""
|
||||
arguments = cls.parse_flags(argument)
|
||||
flags = cls.__commands_flags__
|
||||
|
||||
self: F = cls.__new__(cls)
|
||||
for name, flag in flags.items():
|
||||
try:
|
||||
values = arguments[name]
|
||||
except KeyError:
|
||||
if flag.required:
|
||||
raise MissingRequiredFlag(flag)
|
||||
else:
|
||||
if callable(flag.default):
|
||||
default = await maybe_coroutine(flag.default, ctx)
|
||||
setattr(self, flag.attribute, default)
|
||||
else:
|
||||
setattr(self, flag.attribute, flag.default)
|
||||
continue
|
||||
|
||||
if 0 < flag.max_args < len(values):
|
||||
if flag.override:
|
||||
values = values[-flag.max_args :]
|
||||
else:
|
||||
raise TooManyFlags(flag, values)
|
||||
|
||||
# Special case:
|
||||
if flag.max_args == 1:
|
||||
value = await convert_flag(ctx, values[0], flag)
|
||||
setattr(self, flag.attribute, value)
|
||||
continue
|
||||
|
||||
# Another special case, tuple parsing.
|
||||
# Tuple parsing is basically converting arguments within the flag
|
||||
# So, given flag: hello 20 as the input and Tuple[str, int] as the type hint
|
||||
# We would receive ('hello', 20) as the resulting value
|
||||
# This uses the same whitespace and quoting rules as regular parameters.
|
||||
values = [await convert_flag(ctx, value, flag) for value in values]
|
||||
|
||||
if flag.cast_to_dict:
|
||||
values = dict(values) # type: ignore
|
||||
|
||||
setattr(self, flag.attribute, values)
|
||||
|
||||
return self
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,200 @@
|
||||
"""
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2015-2021 Rapptz
|
||||
Copyright (c) 2021-present Pycord Development
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a
|
||||
copy of this software and associated documentation files (the "Software"),
|
||||
to deal in the Software without restriction, including without limitation
|
||||
the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
and/or sell copies of the Software, and to permit persons to whom the
|
||||
Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
||||
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
DEALINGS IN THE SOFTWARE.
|
||||
"""
|
||||
|
||||
from .errors import (
|
||||
ExpectedClosingQuoteError,
|
||||
InvalidEndOfQuotedStringError,
|
||||
UnexpectedQuoteError,
|
||||
)
|
||||
|
||||
# map from opening quotes to closing quotes
|
||||
_quotes = {
|
||||
'"': '"',
|
||||
"‘": "’",
|
||||
"‚": "‛",
|
||||
"“": "”",
|
||||
"„": "‟",
|
||||
"⹂": "⹂",
|
||||
"「": "」",
|
||||
"『": "』",
|
||||
"〝": "〞",
|
||||
"﹁": "﹂",
|
||||
"﹃": "﹄",
|
||||
""": """,
|
||||
"「": "」",
|
||||
"«": "»",
|
||||
"‹": "›",
|
||||
"《": "》",
|
||||
"〈": "〉",
|
||||
}
|
||||
_all_quotes = set(_quotes.keys()) | set(_quotes.values())
|
||||
|
||||
|
||||
class StringView:
|
||||
def __init__(self, buffer):
|
||||
self.index = 0
|
||||
self.buffer = buffer
|
||||
self.end = len(buffer)
|
||||
self.previous = 0
|
||||
|
||||
@property
|
||||
def current(self):
|
||||
return None if self.eof else self.buffer[self.index]
|
||||
|
||||
@property
|
||||
def eof(self):
|
||||
return self.index >= self.end
|
||||
|
||||
def undo(self):
|
||||
self.index = self.previous
|
||||
|
||||
def skip_ws(self):
|
||||
pos = 0
|
||||
while not self.eof:
|
||||
try:
|
||||
current = self.buffer[self.index + pos]
|
||||
if not current.isspace():
|
||||
break
|
||||
pos += 1
|
||||
except IndexError:
|
||||
break
|
||||
|
||||
self.previous = self.index
|
||||
self.index += pos
|
||||
return self.previous != self.index
|
||||
|
||||
def skip_string(self, string):
|
||||
strlen = len(string)
|
||||
if self.buffer[self.index : self.index + strlen] == string:
|
||||
self.previous = self.index
|
||||
self.index += strlen
|
||||
return True
|
||||
return False
|
||||
|
||||
def read_rest(self):
|
||||
result = self.buffer[self.index :]
|
||||
self.previous = self.index
|
||||
self.index = self.end
|
||||
return result
|
||||
|
||||
def read(self, n):
|
||||
result = self.buffer[self.index : self.index + n]
|
||||
self.previous = self.index
|
||||
self.index += n
|
||||
return result
|
||||
|
||||
def get(self):
|
||||
try:
|
||||
result = self.buffer[self.index + 1]
|
||||
except IndexError:
|
||||
result = None
|
||||
|
||||
self.previous = self.index
|
||||
self.index += 1
|
||||
return result
|
||||
|
||||
def get_word(self):
|
||||
pos = 0
|
||||
while not self.eof:
|
||||
try:
|
||||
current = self.buffer[self.index + pos]
|
||||
if current.isspace():
|
||||
break
|
||||
pos += 1
|
||||
except IndexError:
|
||||
break
|
||||
self.previous = self.index
|
||||
result = self.buffer[self.index : self.index + pos]
|
||||
self.index += pos
|
||||
return result
|
||||
|
||||
def get_quoted_word(self):
|
||||
current = self.current
|
||||
if current is None:
|
||||
return None
|
||||
|
||||
close_quote = _quotes.get(current)
|
||||
is_quoted = bool(close_quote)
|
||||
if is_quoted:
|
||||
result = []
|
||||
_escaped_quotes = (current, close_quote)
|
||||
else:
|
||||
result = [current]
|
||||
_escaped_quotes = _all_quotes
|
||||
|
||||
while not self.eof:
|
||||
current = self.get()
|
||||
if not current:
|
||||
if is_quoted:
|
||||
# unexpected EOF
|
||||
raise ExpectedClosingQuoteError(close_quote)
|
||||
return "".join(result)
|
||||
|
||||
# currently, we accept strings in the format of "hello world"
|
||||
# to embed a quote inside the string you must escape it: "a \"world\""
|
||||
if current == "\\":
|
||||
next_char = self.get()
|
||||
if not next_char:
|
||||
# string ends with \ and no character after it
|
||||
if is_quoted:
|
||||
# if we're quoted then we're expecting a closing quote
|
||||
raise ExpectedClosingQuoteError(close_quote)
|
||||
# if we aren't then we just let it through
|
||||
return "".join(result)
|
||||
|
||||
if next_char in _escaped_quotes:
|
||||
# escaped quote
|
||||
result.append(next_char)
|
||||
else:
|
||||
# different escape character, ignore it
|
||||
self.undo()
|
||||
result.append(current)
|
||||
continue
|
||||
|
||||
if not is_quoted and current in _all_quotes:
|
||||
# we aren't quoted
|
||||
raise UnexpectedQuoteError(current)
|
||||
|
||||
# closing quote
|
||||
if is_quoted and current == close_quote:
|
||||
next_char = self.get()
|
||||
valid_eof = not next_char or next_char.isspace()
|
||||
if not valid_eof:
|
||||
raise InvalidEndOfQuotedStringError(next_char)
|
||||
|
||||
# we're quoted so it's okay
|
||||
return "".join(result)
|
||||
|
||||
if current.isspace() and not is_quoted:
|
||||
# end of word found
|
||||
return "".join(result)
|
||||
|
||||
result.append(current)
|
||||
|
||||
def __repr__(self):
|
||||
return (
|
||||
f"<StringView pos: {self.index} prev: {self.previous} end: {self.end} eof:"
|
||||
f" {self.eof}>"
|
||||
)
|
||||
@@ -0,0 +1,10 @@
|
||||
"""
|
||||
discord.ext.pages
|
||||
~~~~~~~~~~~~~~~~~~~~~
|
||||
An extension module to provide useful menu options.
|
||||
|
||||
:copyright: 2021-present Pycord-Development
|
||||
:license: MIT, see LICENSE for more details.
|
||||
"""
|
||||
|
||||
from .pagination import *
|
||||
BIN
Binary file not shown.
BIN
Binary file not shown.
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,858 @@
|
||||
"""
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2015-2021 Rapptz
|
||||
Copyright (c) 2021-present Pycord Development
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a
|
||||
copy of this software and associated documentation files (the "Software"),
|
||||
to deal in the Software without restriction, including without limitation
|
||||
the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
and/or sell copies of the Software, and to permit persons to whom the
|
||||
Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
||||
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
DEALINGS IN THE SOFTWARE.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import contextvars
|
||||
import datetime
|
||||
import inspect
|
||||
import sys
|
||||
import traceback
|
||||
from collections.abc import Sequence
|
||||
from typing import Any, Awaitable, Callable, Generic, TypeVar, cast
|
||||
|
||||
import aiohttp
|
||||
|
||||
import discord
|
||||
from discord.backoff import ExponentialBackoff
|
||||
from discord.utils import MISSING, _get_event_loop
|
||||
|
||||
__all__ = ("loop",)
|
||||
|
||||
T = TypeVar("T")
|
||||
_func = Callable[..., Awaitable[Any]]
|
||||
LF = TypeVar("LF", bound=_func)
|
||||
FT = TypeVar("FT", bound=_func)
|
||||
ET = TypeVar("ET", bound=Callable[[Any, BaseException], Awaitable[Any]])
|
||||
_current_loop_ctx: contextvars.ContextVar[int] = contextvars.ContextVar(
|
||||
"_current_loop_ctx", default=None
|
||||
)
|
||||
|
||||
|
||||
class SleepHandle:
|
||||
__slots__ = ("future", "loop", "handle")
|
||||
|
||||
def __init__(
|
||||
self, dt: datetime.datetime, *, loop: asyncio.AbstractEventLoop
|
||||
) -> None:
|
||||
self.loop = loop
|
||||
self.future = future = loop.create_future()
|
||||
relative_delta = discord.utils.compute_timedelta(dt)
|
||||
self.handle = loop.call_later(relative_delta, future.set_result, True)
|
||||
|
||||
def _set_result_safe(self):
|
||||
if not self.future.done():
|
||||
self.future.set_result(True)
|
||||
|
||||
def recalculate(self, dt: datetime.datetime) -> None:
|
||||
self.handle.cancel()
|
||||
relative_delta = discord.utils.compute_timedelta(dt)
|
||||
self.handle = self.loop.call_later(relative_delta, self._set_result_safe)
|
||||
|
||||
def wait(self) -> asyncio.Future[Any]:
|
||||
return self.future
|
||||
|
||||
def done(self) -> bool:
|
||||
return self.future.done()
|
||||
|
||||
def cancel(self) -> None:
|
||||
self.handle.cancel()
|
||||
self.future.cancel()
|
||||
|
||||
|
||||
class Loop(Generic[LF]):
|
||||
"""A background task helper that abstracts the loop and reconnection logic for you.
|
||||
|
||||
The main interface to create this is through :func:`loop`.
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
coro: LF,
|
||||
seconds: float,
|
||||
hours: float,
|
||||
minutes: float,
|
||||
time: datetime.time | Sequence[datetime.time],
|
||||
count: int | None,
|
||||
reconnect: bool,
|
||||
loop: asyncio.AbstractEventLoop,
|
||||
overlap: bool | int,
|
||||
) -> None:
|
||||
self.coro: LF = coro
|
||||
self.reconnect: bool = reconnect
|
||||
self.loop: asyncio.AbstractEventLoop = loop
|
||||
self.overlap: bool | int = overlap
|
||||
self.count: int | None = count
|
||||
self._current_loop = 0
|
||||
self._handle: SleepHandle = MISSING
|
||||
self._task: asyncio.Task[None] = MISSING
|
||||
self._injected = None
|
||||
self._valid_exception = (
|
||||
OSError,
|
||||
discord.GatewayNotFound,
|
||||
discord.ConnectionClosed,
|
||||
aiohttp.ClientError,
|
||||
asyncio.TimeoutError,
|
||||
)
|
||||
|
||||
self._before_loop = None
|
||||
self._after_loop = None
|
||||
self._before_loop_running = False
|
||||
self._after_loop_running = False
|
||||
self._is_being_cancelled = False
|
||||
self._has_failed = False
|
||||
self._stop_next_iteration = False
|
||||
self._tasks: set[asyncio.Task[Any]] = set()
|
||||
|
||||
if self.count is not None and self.count <= 0:
|
||||
raise ValueError("count must be greater than 0 or None.")
|
||||
|
||||
self.change_interval(seconds=seconds, minutes=minutes, hours=hours, time=time)
|
||||
self._last_iteration_failed = False
|
||||
self._last_iteration: datetime.datetime = MISSING
|
||||
self._next_iteration = None
|
||||
|
||||
if not inspect.iscoroutinefunction(self.coro):
|
||||
raise TypeError(
|
||||
f"Expected coroutine function, not {type(self.coro).__name__!r}."
|
||||
)
|
||||
if isinstance(overlap, bool):
|
||||
if overlap:
|
||||
self._run_with_semaphore = self._run_direct
|
||||
elif isinstance(overlap, int):
|
||||
if overlap <= 1:
|
||||
raise ValueError("overlap as an integer must be greater than 1.")
|
||||
self._semaphore = asyncio.Semaphore(overlap)
|
||||
self._run_with_semaphore = self._semaphore_runner_factory()
|
||||
else:
|
||||
raise TypeError("overlap must be a bool or a positive integer.")
|
||||
|
||||
async def _run_direct(self, *args: Any, **kwargs: Any) -> None:
|
||||
"""Run the coroutine directly."""
|
||||
await self.coro(*args, **kwargs)
|
||||
|
||||
def _semaphore_runner_factory(self) -> Callable[..., Awaitable[None]]:
|
||||
"""Return a function that runs the coroutine with a semaphore."""
|
||||
|
||||
async def runner(*args: Any, **kwargs: Any) -> None:
|
||||
async with self._semaphore:
|
||||
await self.coro(*args, **kwargs)
|
||||
|
||||
return runner
|
||||
|
||||
async def _call_loop_function(self, name: str, *args: Any, **kwargs: Any) -> None:
|
||||
coro = getattr(self, f"_{name}")
|
||||
if coro is None:
|
||||
return
|
||||
|
||||
if name.endswith("_loop"):
|
||||
setattr(self, f"_{name}_running", True)
|
||||
|
||||
if self._injected is not None:
|
||||
await coro(self._injected, *args, **kwargs)
|
||||
else:
|
||||
await coro(*args, **kwargs)
|
||||
|
||||
if name.endswith("_loop"):
|
||||
setattr(self, f"_{name}_running", False)
|
||||
|
||||
def _try_sleep_until(self, dt: datetime.datetime):
|
||||
self._handle = SleepHandle(dt=dt, loop=self.loop)
|
||||
return self._handle.wait()
|
||||
|
||||
async def _loop(self, *args: Any, **kwargs: Any) -> None:
|
||||
backoff = ExponentialBackoff()
|
||||
await self._call_loop_function("before_loop")
|
||||
self._last_iteration_failed = False
|
||||
if self._time is not MISSING:
|
||||
# the time index should be prepared every time the internal loop is started
|
||||
self._prepare_time_index()
|
||||
self._next_iteration = self._get_next_sleep_time()
|
||||
else:
|
||||
self._next_iteration = datetime.datetime.now(datetime.timezone.utc)
|
||||
try:
|
||||
await self._try_sleep_until(self._next_iteration)
|
||||
while True:
|
||||
if not self._last_iteration_failed:
|
||||
self._last_iteration = self._next_iteration
|
||||
self._next_iteration = self._get_next_sleep_time()
|
||||
try:
|
||||
token = _current_loop_ctx.set(self._current_loop)
|
||||
if not self.overlap:
|
||||
await self.coro(*args, **kwargs)
|
||||
else:
|
||||
task = asyncio.create_task(
|
||||
self._run_with_semaphore(*args, **kwargs),
|
||||
name=f"pycord-loop-{self.coro.__name__}-{self._current_loop}",
|
||||
)
|
||||
task.add_done_callback(self._tasks.discard)
|
||||
self._tasks.add(task)
|
||||
|
||||
_current_loop_ctx.reset(token)
|
||||
self._last_iteration_failed = False
|
||||
backoff = ExponentialBackoff()
|
||||
except self._valid_exception:
|
||||
self._last_iteration_failed = True
|
||||
if not self.reconnect:
|
||||
raise
|
||||
await asyncio.sleep(backoff.delay())
|
||||
else:
|
||||
await self._try_sleep_until(self._next_iteration)
|
||||
|
||||
if self._stop_next_iteration:
|
||||
return
|
||||
|
||||
now = datetime.datetime.now(datetime.timezone.utc)
|
||||
if now > self._next_iteration:
|
||||
self._next_iteration = now
|
||||
if self._time is not MISSING:
|
||||
self._prepare_time_index(now)
|
||||
|
||||
self._current_loop += 1
|
||||
if self._current_loop == self.count:
|
||||
break
|
||||
|
||||
except asyncio.CancelledError:
|
||||
self._is_being_cancelled = True
|
||||
for task in self._tasks:
|
||||
task.cancel()
|
||||
await asyncio.gather(*self._tasks, return_exceptions=True)
|
||||
raise
|
||||
except Exception as exc:
|
||||
self._has_failed = True
|
||||
await self._call_loop_function("error", exc)
|
||||
raise exc
|
||||
finally:
|
||||
await self._call_loop_function("after_loop")
|
||||
self._handle.cancel()
|
||||
self._is_being_cancelled = False
|
||||
self._current_loop = 0
|
||||
self._stop_next_iteration = False
|
||||
self._has_failed = False
|
||||
|
||||
def __get__(self, obj: T, objtype: type[T]) -> Loop[LF]:
|
||||
if obj is None:
|
||||
return self
|
||||
|
||||
copy: Loop[LF] = Loop(
|
||||
self.coro,
|
||||
seconds=self._seconds,
|
||||
hours=self._hours,
|
||||
minutes=self._minutes,
|
||||
time=self._time,
|
||||
count=self.count,
|
||||
reconnect=self.reconnect,
|
||||
loop=self.loop,
|
||||
overlap=self.overlap,
|
||||
)
|
||||
copy._injected = obj
|
||||
copy._before_loop = self._before_loop
|
||||
copy._after_loop = self._after_loop
|
||||
copy._error = self._error
|
||||
setattr(obj, self.coro.__name__, copy)
|
||||
return copy
|
||||
|
||||
@property
|
||||
def seconds(self) -> float | None:
|
||||
"""Read-only value for the number of seconds
|
||||
between each iteration. ``None`` if an explicit ``time`` value was passed instead.
|
||||
|
||||
.. versionadded:: 2.0
|
||||
"""
|
||||
if self._seconds is not MISSING:
|
||||
return self._seconds
|
||||
|
||||
@property
|
||||
def minutes(self) -> float | None:
|
||||
"""Read-only value for the number of minutes
|
||||
between each iteration. ``None`` if an explicit ``time`` value was passed instead.
|
||||
|
||||
.. versionadded:: 2.0
|
||||
"""
|
||||
if self._minutes is not MISSING:
|
||||
return self._minutes
|
||||
|
||||
@property
|
||||
def hours(self) -> float | None:
|
||||
"""Read-only value for the number of hours
|
||||
between each iteration. ``None`` if an explicit ``time`` value was passed instead.
|
||||
|
||||
.. versionadded:: 2.0
|
||||
"""
|
||||
if self._hours is not MISSING:
|
||||
return self._hours
|
||||
|
||||
@property
|
||||
def time(self) -> list[datetime.time] | None:
|
||||
"""Read-only list for the exact times this loop runs at.
|
||||
``None`` if relative times were passed instead.
|
||||
|
||||
.. versionadded:: 2.0
|
||||
"""
|
||||
if self._time is not MISSING:
|
||||
return self._time.copy()
|
||||
|
||||
@property
|
||||
def current_loop(self) -> int:
|
||||
"""The current iteration of the loop."""
|
||||
return (
|
||||
_current_loop_ctx.get()
|
||||
if _current_loop_ctx.get() is not None
|
||||
else self._current_loop
|
||||
)
|
||||
|
||||
@property
|
||||
def next_iteration(self) -> datetime.datetime | None:
|
||||
"""When the next iteration of the loop will occur.
|
||||
|
||||
.. versionadded:: 1.3
|
||||
"""
|
||||
if self._task is MISSING:
|
||||
return None
|
||||
elif self._task and self._task.done() or self._stop_next_iteration:
|
||||
return None
|
||||
return self._next_iteration
|
||||
|
||||
async def __call__(self, *args: Any, **kwargs: Any) -> Any:
|
||||
r"""|coro|
|
||||
|
||||
Calls the internal callback that the task holds.
|
||||
|
||||
.. versionadded:: 1.6
|
||||
|
||||
Parameters
|
||||
------------
|
||||
\*args
|
||||
The arguments to use.
|
||||
\*\*kwargs
|
||||
The keyword arguments to use.
|
||||
"""
|
||||
|
||||
if self._injected is not None:
|
||||
args = (self._injected, *args)
|
||||
|
||||
return await self.coro(*args, **kwargs)
|
||||
|
||||
def start(self, *args: Any, **kwargs: Any) -> asyncio.Task[None]:
|
||||
r"""Starts the internal task in the event loop.
|
||||
|
||||
Parameters
|
||||
------------
|
||||
\*args
|
||||
The arguments to use.
|
||||
\*\*kwargs
|
||||
The keyword arguments to use.
|
||||
|
||||
Raises
|
||||
--------
|
||||
RuntimeError
|
||||
A task has already been launched and is running.
|
||||
|
||||
Returns
|
||||
---------
|
||||
:class:`asyncio.Task`
|
||||
The task that has been created.
|
||||
"""
|
||||
|
||||
if self._task is not MISSING and not self._task.done():
|
||||
raise RuntimeError("Task is already launched and is not completed.")
|
||||
|
||||
if self._injected is not None:
|
||||
args = (self._injected, *args)
|
||||
|
||||
if self.loop is MISSING:
|
||||
self.loop = _get_event_loop()
|
||||
|
||||
self._task = self.loop.create_task(self._loop(*args, **kwargs))
|
||||
return self._task
|
||||
|
||||
def stop(self) -> None:
|
||||
r"""Gracefully stops the task from running.
|
||||
|
||||
Unlike :meth:`cancel`\, this allows the task to finish its
|
||||
current iteration before gracefully exiting.
|
||||
|
||||
.. note::
|
||||
|
||||
If the internal function raises an error that can be
|
||||
handled before finishing then it will retry until
|
||||
it succeeds.
|
||||
|
||||
If this is undesirable, either remove the error handling
|
||||
before stopping via :meth:`clear_exception_types` or
|
||||
use :meth:`cancel` instead.
|
||||
|
||||
.. versionadded:: 1.2
|
||||
"""
|
||||
if self._task is not MISSING and not self._task.done():
|
||||
self._stop_next_iteration = True
|
||||
|
||||
def _can_be_cancelled(self) -> bool:
|
||||
return bool(
|
||||
not self._is_being_cancelled and self._task and not self._task.done()
|
||||
)
|
||||
|
||||
def cancel(self) -> None:
|
||||
"""Cancels the internal task, if it is running."""
|
||||
if self._can_be_cancelled():
|
||||
self._task.cancel()
|
||||
|
||||
def restart(self, *args: Any, **kwargs: Any) -> None:
|
||||
r"""A convenience method to restart the internal task.
|
||||
|
||||
.. note::
|
||||
|
||||
Due to the way this function works, the task is not
|
||||
returned like :meth:`start`.
|
||||
|
||||
Parameters
|
||||
------------
|
||||
\*args
|
||||
The arguments to use.
|
||||
\*\*kwargs
|
||||
The keyword arguments to use.
|
||||
"""
|
||||
|
||||
def restart_when_over(
|
||||
fut: Any, *, args: Any = args, kwargs: Any = kwargs
|
||||
) -> None:
|
||||
self._task.remove_done_callback(restart_when_over)
|
||||
self.start(*args, **kwargs)
|
||||
|
||||
if self._can_be_cancelled():
|
||||
self._task.add_done_callback(restart_when_over)
|
||||
self._task.cancel()
|
||||
|
||||
def add_exception_type(self, *exceptions: type[BaseException]) -> None:
|
||||
r"""Adds exception types to be handled during the reconnect logic.
|
||||
|
||||
By default, the exception types handled are those handled by
|
||||
:meth:`discord.Client.connect`\, which includes a lot of internet disconnection
|
||||
errors.
|
||||
|
||||
This function is useful if you're interacting with a 3rd party library that
|
||||
raises its own set of exceptions.
|
||||
|
||||
Parameters
|
||||
------------
|
||||
\*exceptions: Type[:class:`BaseException`]
|
||||
An argument list of exception classes to handle.
|
||||
|
||||
Raises
|
||||
--------
|
||||
TypeError
|
||||
An exception passed is either not a class or not inherited from :class:`BaseException`.
|
||||
"""
|
||||
|
||||
for exc in exceptions:
|
||||
if not inspect.isclass(exc):
|
||||
raise TypeError(f"{exc!r} must be a class.")
|
||||
if not issubclass(exc, BaseException):
|
||||
raise TypeError(f"{exc!r} must inherit from BaseException.")
|
||||
|
||||
self._valid_exception = (*self._valid_exception, *exceptions)
|
||||
|
||||
def clear_exception_types(self) -> None:
|
||||
"""Removes all exception types that are handled.
|
||||
|
||||
.. note::
|
||||
|
||||
This operation obviously cannot be undone!
|
||||
"""
|
||||
self._valid_exception = ()
|
||||
|
||||
def remove_exception_type(self, *exceptions: type[BaseException]) -> bool:
|
||||
r"""Removes exception types from being handled during the reconnect logic.
|
||||
|
||||
Parameters
|
||||
------------
|
||||
\*exceptions: Type[:class:`BaseException`]
|
||||
An argument list of exception classes to handle.
|
||||
|
||||
Returns
|
||||
---------
|
||||
:class:`bool`
|
||||
Whether all exceptions were successfully removed.
|
||||
"""
|
||||
old_length = len(self._valid_exception)
|
||||
self._valid_exception = tuple(
|
||||
x for x in self._valid_exception if x not in exceptions
|
||||
)
|
||||
return len(self._valid_exception) == old_length - len(exceptions)
|
||||
|
||||
def get_task(self) -> asyncio.Task[None] | None:
|
||||
"""Fetches the internal task or ``None`` if there isn't one running."""
|
||||
return self._task if self._task is not MISSING else None
|
||||
|
||||
def is_being_cancelled(self) -> bool:
|
||||
"""Whether the task is being cancelled."""
|
||||
return self._is_being_cancelled
|
||||
|
||||
def failed(self) -> bool:
|
||||
"""Whether the internal task has failed.
|
||||
|
||||
.. versionadded:: 1.2
|
||||
"""
|
||||
return self._has_failed
|
||||
|
||||
def is_running(self) -> bool:
|
||||
"""Check if the task is currently running.
|
||||
|
||||
.. versionadded:: 1.4
|
||||
"""
|
||||
return not bool(self._task.done()) if self._task is not MISSING else False
|
||||
|
||||
async def _error(self, *args: Any) -> None:
|
||||
exception: Exception = args[-1]
|
||||
print(
|
||||
f"Unhandled exception in internal background task {self.coro.__name__!r}.",
|
||||
file=sys.stderr,
|
||||
)
|
||||
traceback.print_exception(
|
||||
type(exception), exception, exception.__traceback__, file=sys.stderr
|
||||
)
|
||||
|
||||
def before_loop(self, coro: FT) -> FT:
|
||||
"""A decorator that registers a coroutine to be called before the loop starts running.
|
||||
|
||||
This is useful if you want to wait for some bot state before the loop starts,
|
||||
such as :meth:`discord.Client.wait_until_ready`.
|
||||
|
||||
The coroutine must take no arguments (except ``self`` in a class context).
|
||||
|
||||
Parameters
|
||||
----------
|
||||
coro: :ref:`coroutine <coroutine>`
|
||||
The coroutine to register before the loop runs.
|
||||
|
||||
Raises
|
||||
------
|
||||
TypeError
|
||||
The function was not a coroutine.
|
||||
"""
|
||||
|
||||
if not inspect.iscoroutinefunction(coro):
|
||||
raise TypeError(
|
||||
f"Expected coroutine function, received {coro.__class__.__name__!r}."
|
||||
)
|
||||
|
||||
self._before_loop = coro
|
||||
return coro
|
||||
|
||||
def after_loop(self, coro: FT) -> FT:
|
||||
"""A decorator that register a coroutine to be called after the loop finished running.
|
||||
|
||||
The coroutine must take no arguments (except ``self`` in a class context).
|
||||
|
||||
.. note::
|
||||
|
||||
This coroutine is called even during cancellation. If it is desirable
|
||||
to tell apart whether something was cancelled or not, check to see
|
||||
whether :meth:`is_being_cancelled` is ``True`` or not.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
coro: :ref:`coroutine <coroutine>`
|
||||
The coroutine to register after the loop finishes.
|
||||
|
||||
Raises
|
||||
------
|
||||
TypeError
|
||||
The function was not a coroutine.
|
||||
"""
|
||||
|
||||
if not inspect.iscoroutinefunction(coro):
|
||||
raise TypeError(
|
||||
f"Expected coroutine function, received {coro.__class__.__name__!r}."
|
||||
)
|
||||
|
||||
self._after_loop = coro
|
||||
return coro
|
||||
|
||||
def error(self, coro: ET) -> ET:
|
||||
"""A decorator that registers a coroutine to be called if the task encounters an unhandled exception.
|
||||
|
||||
The coroutine must take only one argument the exception raised (except ``self`` in a class context).
|
||||
|
||||
By default, this prints to :data:`sys.stderr` however it could be
|
||||
overridden to have a different implementation.
|
||||
|
||||
.. versionadded:: 1.4
|
||||
|
||||
Parameters
|
||||
----------
|
||||
coro: :ref:`coroutine <coroutine>`
|
||||
The coroutine to register in the event of an unhandled exception.
|
||||
|
||||
Raises
|
||||
------
|
||||
TypeError
|
||||
The function was not a coroutine.
|
||||
"""
|
||||
if not inspect.iscoroutinefunction(coro):
|
||||
raise TypeError(
|
||||
f"Expected coroutine function, received {coro.__class__.__name__!r}."
|
||||
)
|
||||
|
||||
self._error = coro # type: ignore
|
||||
return coro
|
||||
|
||||
def _get_next_sleep_time(self) -> datetime.datetime:
|
||||
if self._sleep is not MISSING:
|
||||
return self._last_iteration + datetime.timedelta(seconds=self._sleep)
|
||||
|
||||
if self._time_index >= len(self._time):
|
||||
self._time_index = 0
|
||||
if self._current_loop == 0:
|
||||
# if we're at the last index on the first iteration, we need to sleep until tomorrow
|
||||
return datetime.datetime.combine(
|
||||
datetime.datetime.now(self._time[0].tzinfo or datetime.timezone.utc)
|
||||
+ datetime.timedelta(days=1),
|
||||
self._time[0],
|
||||
)
|
||||
|
||||
next_time = self._time[self._time_index]
|
||||
|
||||
if self._current_loop == 0:
|
||||
self._time_index += 1
|
||||
if (
|
||||
next_time
|
||||
> datetime.datetime.now(
|
||||
next_time.tzinfo or datetime.timezone.utc
|
||||
).timetz()
|
||||
):
|
||||
return datetime.datetime.combine(
|
||||
datetime.datetime.now(next_time.tzinfo or datetime.timezone.utc),
|
||||
next_time,
|
||||
)
|
||||
else:
|
||||
return datetime.datetime.combine(
|
||||
datetime.datetime.now(next_time.tzinfo or datetime.timezone.utc)
|
||||
+ datetime.timedelta(days=1),
|
||||
next_time,
|
||||
)
|
||||
|
||||
next_date = cast(
|
||||
datetime.datetime, self._last_iteration.astimezone(next_time.tzinfo)
|
||||
)
|
||||
if next_time < next_date.timetz():
|
||||
next_date += datetime.timedelta(days=1)
|
||||
|
||||
self._time_index += 1
|
||||
return datetime.datetime.combine(next_date, next_time)
|
||||
|
||||
def _prepare_time_index(self, now: datetime.datetime = MISSING) -> None:
|
||||
# now kwarg should be a datetime.datetime representing the time "now"
|
||||
# to calculate the next time index from
|
||||
|
||||
# pre-condition: self._time is set
|
||||
time_now = (
|
||||
now
|
||||
if now is not MISSING
|
||||
else datetime.datetime.now(datetime.timezone.utc).replace(microsecond=0)
|
||||
)
|
||||
for idx, time in enumerate(self._time):
|
||||
if time >= time_now.astimezone(time.tzinfo).timetz():
|
||||
self._time_index = idx
|
||||
break
|
||||
else:
|
||||
self._time_index = 0
|
||||
|
||||
def _get_time_parameter(
|
||||
self,
|
||||
time: datetime.time | Sequence[datetime.time],
|
||||
*,
|
||||
dt: type[datetime.time] = datetime.time,
|
||||
utc: datetime.timezone = datetime.timezone.utc,
|
||||
) -> list[datetime.time]:
|
||||
if isinstance(time, dt):
|
||||
inner = time if time.tzinfo is not None else time.replace(tzinfo=utc)
|
||||
return [inner]
|
||||
if not isinstance(time, Sequence):
|
||||
raise TypeError(
|
||||
"Expected datetime.time or a sequence of datetime.time for ``time``,"
|
||||
f" received {type(time)!r} instead."
|
||||
)
|
||||
if not time:
|
||||
raise ValueError("time parameter must not be an empty sequence.")
|
||||
|
||||
ret: list[datetime.time] = []
|
||||
for index, t in enumerate(time):
|
||||
if not isinstance(t, dt):
|
||||
raise TypeError(
|
||||
f"Expected a sequence of {dt!r} for ``time``, received"
|
||||
f" {type(t).__name__!r} at index {index} instead."
|
||||
)
|
||||
ret.append(t if t.tzinfo is not None else t.replace(tzinfo=utc))
|
||||
|
||||
return sorted(set(ret)) # de-dupe and sort times
|
||||
|
||||
def change_interval(
|
||||
self,
|
||||
*,
|
||||
seconds: float = 0,
|
||||
minutes: float = 0,
|
||||
hours: float = 0,
|
||||
time: datetime.time | Sequence[datetime.time] = MISSING,
|
||||
) -> None:
|
||||
"""Changes the interval for the sleep time.
|
||||
|
||||
.. versionadded:: 1.2
|
||||
|
||||
Parameters
|
||||
----------
|
||||
seconds: :class:`float`
|
||||
The number of seconds between every iteration.
|
||||
minutes: :class:`float`
|
||||
The number of minutes between every iteration.
|
||||
hours: :class:`float`
|
||||
The number of hours between every iteration.
|
||||
time: Union[:class:`datetime.time`, Sequence[:class:`datetime.time`]]
|
||||
The exact times to run this loop at. Either a non-empty list or a single
|
||||
value of :class:`datetime.time` should be passed.
|
||||
This cannot be used in conjunction with the relative time parameters.
|
||||
|
||||
.. versionadded:: 2.0
|
||||
|
||||
.. note::
|
||||
|
||||
Duplicate times will be ignored, and only run once.
|
||||
|
||||
Raises
|
||||
------
|
||||
ValueError
|
||||
An invalid value was given.
|
||||
TypeError
|
||||
An invalid value for the ``time`` parameter was passed, or the
|
||||
``time`` parameter was passed in conjunction with relative time parameters.
|
||||
"""
|
||||
|
||||
if time is MISSING:
|
||||
seconds = seconds or 0
|
||||
minutes = minutes or 0
|
||||
hours = hours or 0
|
||||
sleep = seconds + (minutes * 60.0) + (hours * 3600.0)
|
||||
if sleep < 0:
|
||||
raise ValueError("Total number of seconds cannot be less than zero.")
|
||||
|
||||
self._sleep = sleep
|
||||
self._seconds = float(seconds)
|
||||
self._hours = float(hours)
|
||||
self._minutes = float(minutes)
|
||||
self._time: list[datetime.time] = MISSING
|
||||
else:
|
||||
if any((seconds, minutes, hours)):
|
||||
raise TypeError("Cannot mix explicit time with relative time")
|
||||
self._time = self._get_time_parameter(time)
|
||||
self._sleep = self._seconds = self._minutes = self._hours = MISSING
|
||||
|
||||
if self.is_running() and not (
|
||||
self._before_loop_running or self._after_loop_running
|
||||
):
|
||||
if self._time is not MISSING:
|
||||
# prepare the next time index starting from after the last iteration
|
||||
self._prepare_time_index(now=self._last_iteration)
|
||||
|
||||
self._next_iteration = self._get_next_sleep_time()
|
||||
if not self._handle.done():
|
||||
# the loop is sleeping, recalculate based on new interval
|
||||
self._handle.recalculate(self._next_iteration)
|
||||
|
||||
|
||||
def loop(
|
||||
*,
|
||||
seconds: float = MISSING,
|
||||
minutes: float = MISSING,
|
||||
hours: float = MISSING,
|
||||
time: datetime.time | Sequence[datetime.time] = MISSING,
|
||||
count: int | None = None,
|
||||
reconnect: bool = True,
|
||||
loop: asyncio.AbstractEventLoop = MISSING,
|
||||
overlap: bool | int = False,
|
||||
) -> Callable[[LF], Loop[LF]]:
|
||||
"""A decorator that schedules a task in the background for you with
|
||||
optional reconnect logic. The decorator returns a :class:`Loop`.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
seconds: :class:`float`
|
||||
The number of seconds between every iteration.
|
||||
minutes: :class:`float`
|
||||
The number of minutes between every iteration.
|
||||
hours: :class:`float`
|
||||
The number of hours between every iteration.
|
||||
time: Union[:class:`datetime.time`, Sequence[:class:`datetime.time`]]
|
||||
The exact times to run this loop at. Either a non-empty list or a single
|
||||
value of :class:`datetime.time` should be passed. Timezones are supported.
|
||||
If no timezone is given for the times, it is assumed to represent UTC time.
|
||||
|
||||
This cannot be used in conjunction with the relative time parameters.
|
||||
|
||||
.. note::
|
||||
|
||||
Duplicate times will be ignored, and only run once.
|
||||
|
||||
.. versionadded:: 2.0
|
||||
|
||||
count: Optional[:class:`int`]
|
||||
The number of loops to do, ``None`` if it should be an
|
||||
infinite loop.
|
||||
reconnect: :class:`bool`
|
||||
Whether to handle errors and restart the task
|
||||
using an exponential back-off algorithm similar to the
|
||||
one used in :meth:`discord.Client.connect`.
|
||||
loop: :class:`asyncio.AbstractEventLoop`
|
||||
The loop to use to register the task, if not given the default event loop is used via
|
||||
:func:`asyncio.get_event_loop()` if it exists or one is created via :func:`asyncio.new_event_loop()`.
|
||||
overlap: Union[:class:`bool`, :class:`int`]
|
||||
Controls whether overlapping executions of the task loop are allowed.
|
||||
Set to False (default) to run iterations one at a time, True for unlimited overlap, or an int to cap the number of concurrent runs.
|
||||
|
||||
.. versionadded:: 2.7
|
||||
|
||||
Raises
|
||||
------
|
||||
ValueError
|
||||
An invalid value was given.
|
||||
TypeError
|
||||
The function was not a coroutine, an invalid value for the ``time`` parameter was passed,
|
||||
or ``time`` parameter was passed in conjunction with relative time parameters.
|
||||
"""
|
||||
|
||||
def decorator(func: LF) -> Loop[LF]:
|
||||
return Loop[LF](
|
||||
func,
|
||||
seconds=seconds,
|
||||
minutes=minutes,
|
||||
hours=hours,
|
||||
count=count,
|
||||
time=time,
|
||||
reconnect=reconnect,
|
||||
loop=loop,
|
||||
overlap=overlap,
|
||||
)
|
||||
|
||||
return decorator
|
||||
BIN
Binary file not shown.
Reference in New Issue
Block a user