Fix weird git problems
This commit is contained in:
commit
0aa8c5bef3
@ -329,7 +329,7 @@ class Client:
|
||||
If this is not passed via ``__init__`` then this is retrieved
|
||||
through the gateway when an event contains the data. Usually
|
||||
after :func:`~discord.on_connect` is called.
|
||||
|
||||
|
||||
.. versionadded:: 2.0
|
||||
"""
|
||||
return self._connection.application_id
|
||||
@ -595,7 +595,7 @@ class Client:
|
||||
async def start(self, token: str, *, reconnect: bool = True) -> None:
|
||||
"""|coro|
|
||||
|
||||
A shorthand coroutine for :meth:`login` + :meth:`connect`.
|
||||
A shorthand coroutine for :meth:`login` + :meth:`setup` + :meth:`connect`.
|
||||
|
||||
Raises
|
||||
-------
|
||||
@ -603,8 +603,19 @@ class Client:
|
||||
An unexpected keyword argument was received.
|
||||
"""
|
||||
await self.login(token)
|
||||
await self.setup()
|
||||
await self.connect(reconnect=reconnect)
|
||||
|
||||
async def setup(self) -> Any:
|
||||
"""|coro|
|
||||
|
||||
A coroutine to be called to setup the bot, by default this is blank.
|
||||
|
||||
To perform asynchronous setup after the bot is logged in but before
|
||||
it has connected to the Websocket, overwrite this coroutine.
|
||||
"""
|
||||
pass
|
||||
|
||||
def run(self, *args: Any, **kwargs: Any) -> None:
|
||||
"""A blocking call that abstracts away the event loop
|
||||
initialisation from you.
|
||||
@ -687,7 +698,7 @@ class Client:
|
||||
self._connection._activity = value.to_dict() # type: ignore
|
||||
else:
|
||||
raise TypeError('activity must derive from BaseActivity.')
|
||||
|
||||
|
||||
@property
|
||||
def status(self):
|
||||
""":class:`.Status`:
|
||||
@ -758,7 +769,7 @@ class Client:
|
||||
|
||||
This is useful if you have a channel_id but don't want to do an API call
|
||||
to send messages to it.
|
||||
|
||||
|
||||
.. versionadded:: 2.0
|
||||
|
||||
Parameters
|
||||
@ -1604,7 +1615,7 @@ class Client:
|
||||
|
||||
This method should be used for when a view is comprised of components
|
||||
that last longer than the lifecycle of the program.
|
||||
|
||||
|
||||
.. versionadded:: 2.0
|
||||
|
||||
Parameters
|
||||
@ -1636,7 +1647,7 @@ class Client:
|
||||
@property
|
||||
def persistent_views(self) -> Sequence[View]:
|
||||
"""Sequence[:class:`.View`]: A sequence of persistent views added to the client.
|
||||
|
||||
|
||||
.. versionadded:: 2.0
|
||||
"""
|
||||
return self._connection.persistent_views
|
||||
|
@ -366,7 +366,7 @@ class Embed:
|
||||
self._footer['icon_url'] = str(icon_url)
|
||||
|
||||
return self
|
||||
|
||||
|
||||
def remove_footer(self: E) -> E:
|
||||
"""Clears embed's footer information.
|
||||
|
||||
@ -381,7 +381,7 @@ class Embed:
|
||||
pass
|
||||
|
||||
return self
|
||||
|
||||
|
||||
@property
|
||||
def image(self) -> _EmbedMediaProxy:
|
||||
"""Returns an ``EmbedProxy`` denoting the image contents.
|
||||
|
@ -28,17 +28,24 @@ from __future__ import annotations
|
||||
import asyncio
|
||||
import collections
|
||||
import collections.abc
|
||||
from discord.http import HTTPClient
|
||||
|
||||
import inspect
|
||||
import importlib.util
|
||||
import sys
|
||||
import traceback
|
||||
import types
|
||||
from typing import Any, Callable, Mapping, List, Dict, TYPE_CHECKING, Optional, TypeVar, Type, Union
|
||||
from typing import Any, Callable, cast, Mapping, List, Dict, TYPE_CHECKING, Optional, TypeVar, Type, Union
|
||||
|
||||
import discord
|
||||
from discord.types.interactions import (
|
||||
ApplicationCommandInteractionData,
|
||||
_ApplicationCommandInteractionDataOptionString
|
||||
)
|
||||
|
||||
from .core import GroupMixin
|
||||
from .view import StringView
|
||||
from .converter import Greedy
|
||||
from .view import StringView, supported_quotes
|
||||
from .context import Context
|
||||
from . import errors
|
||||
from .help import HelpCommand, DefaultHelpCommand
|
||||
@ -66,6 +73,13 @@ T = TypeVar('T')
|
||||
CFT = TypeVar('CFT', bound='CoroFunc')
|
||||
CXT = TypeVar('CXT', bound='Context')
|
||||
|
||||
class _FakeSlashMessage(discord.PartialMessage):
|
||||
activity = application = edited_at = reference = webhook_id = None
|
||||
attachments = components = reactions = stickers = mentions = []
|
||||
author: Union[discord.User, discord.Member]
|
||||
tts = False
|
||||
|
||||
|
||||
def when_mentioned(bot: Union[Bot, AutoShardedBot], msg: Message) -> List[str]:
|
||||
"""A callable that implements a command prefix equivalent to being mentioned.
|
||||
|
||||
@ -120,9 +134,17 @@ class _DefaultRepr:
|
||||
_default = _DefaultRepr()
|
||||
|
||||
class BotBase(GroupMixin):
|
||||
def __init__(self, command_prefix, help_command=_default, description=None, **options):
|
||||
def __init__(self,
|
||||
command_prefix,
|
||||
help_command=_default,
|
||||
description=None,
|
||||
message_commands: bool = True,
|
||||
slash_commands: bool = False, **options
|
||||
):
|
||||
super().__init__(**options)
|
||||
self.command_prefix = command_prefix
|
||||
self.slash_commands = slash_commands
|
||||
self.message_commands = message_commands
|
||||
self.extra_events: Dict[str, List[CoroFunc]] = {}
|
||||
self.__cogs: Dict[str, Cog] = {}
|
||||
self.__extensions: Dict[str, types.ModuleType] = {}
|
||||
@ -142,11 +164,17 @@ class BotBase(GroupMixin):
|
||||
if self.owner_ids and not isinstance(self.owner_ids, collections.abc.Collection):
|
||||
raise TypeError(f'owner_ids must be a collection not {self.owner_ids.__class__!r}')
|
||||
|
||||
if not (message_commands or slash_commands):
|
||||
raise TypeError("Both message_commands and slash_commands are disabled.")
|
||||
elif slash_commands:
|
||||
self.slash_command_guild = options.get('slash_command_guild', None)
|
||||
|
||||
if help_command is _default:
|
||||
self.help_command = DefaultHelpCommand()
|
||||
else:
|
||||
self.help_command = help_command
|
||||
|
||||
|
||||
# internal helpers
|
||||
|
||||
def dispatch(self, event_name: str, *args: Any, **kwargs: Any) -> None:
|
||||
@ -156,6 +184,15 @@ class BotBase(GroupMixin):
|
||||
for event in self.extra_events.get(ev, []):
|
||||
self._schedule_event(event, ev, *args, **kwargs) # type: ignore
|
||||
|
||||
async def _create_application_commands(self, application_id: int, http: HTTPClient):
|
||||
commands = [scmd for cmd in self.commands if not cmd.hidden and (scmd := cmd.to_application_command()) is not None]
|
||||
|
||||
if self.slash_command_guild is None:
|
||||
await http.bulk_upsert_global_commands(application_id, payload=commands)
|
||||
else:
|
||||
await http.bulk_upsert_guild_commands(application_id, self.slash_command_guild, payload=commands)
|
||||
|
||||
|
||||
@discord.utils.copy_doc(discord.Client.close)
|
||||
async def close(self) -> None:
|
||||
for extension in tuple(self.__extensions):
|
||||
@ -1031,7 +1068,88 @@ class BotBase(GroupMixin):
|
||||
await self.invoke(ctx)
|
||||
|
||||
async def on_message(self, message):
|
||||
await self.process_commands(message)
|
||||
if self.message_commands:
|
||||
await self.process_commands(message)
|
||||
|
||||
async def on_interaction(self, interaction: discord.Interaction):
|
||||
if not self.slash_commands or interaction.type != discord.InteractionType.application_command:
|
||||
return
|
||||
|
||||
assert interaction.user is not None
|
||||
interaction.data = cast(ApplicationCommandInteractionData, interaction.data)
|
||||
|
||||
# Ensure the interaction channel is usable
|
||||
channel = interaction.channel
|
||||
if channel is None or isinstance(channel, discord.PartialMessageable):
|
||||
if interaction.guild is None:
|
||||
channel = await interaction.user.create_dm()
|
||||
elif interaction.channel_id is not None:
|
||||
channel = await interaction.guild.fetch_channel(interaction.channel_id)
|
||||
else:
|
||||
return # cannot do anything without stable channel
|
||||
|
||||
# Fetch out subcommands from the options
|
||||
command_name = interaction.data['name']
|
||||
command_options = interaction.data.get('options') or []
|
||||
while any(o["type"] in {1, 2} for o in command_options):
|
||||
for option in command_options:
|
||||
if option['type'] in {1, 2}:
|
||||
command_name += f' {option["name"]}'
|
||||
command_options = option.get('options') or []
|
||||
|
||||
command = self.get_command(command_name)
|
||||
if command is None:
|
||||
raise errors.CommandNotFound(f'Command "{command_name}" is not found')
|
||||
|
||||
message: discord.Message = _FakeSlashMessage(id=interaction.id, channel=channel) # type: ignore
|
||||
message.author = interaction.user
|
||||
|
||||
# Fetch a valid prefix, so process_commands can function
|
||||
prefix = await self.get_prefix(message)
|
||||
if isinstance(prefix, list):
|
||||
prefix = prefix[0]
|
||||
|
||||
# Add arguments to fake message content, in the right order
|
||||
message.content = f'{prefix}{command_name} '
|
||||
for name, param in command.clean_params.items():
|
||||
option = next((o for o in command_options if o['name'] == name), None) # type: ignore
|
||||
|
||||
if option is None:
|
||||
if param.default is param.empty and not command._is_typing_optional(param.annotation):
|
||||
raise errors.MissingRequiredArgument(param)
|
||||
elif (
|
||||
option["type"] == 3
|
||||
and " " in option["value"] # type: ignore
|
||||
and param.kind != param.KEYWORD_ONLY
|
||||
and not isinstance(param.annotation, Greedy)
|
||||
):
|
||||
# String with space in without "consume rest"
|
||||
option = cast(_ApplicationCommandInteractionDataOptionString, option)
|
||||
|
||||
# we need to quote this string otherwise we may spill into
|
||||
# other parameters and cause all kinds of trouble, as many
|
||||
# quotes are supported and some may be in the option, we
|
||||
# loop through all supported quotes and if neither open or
|
||||
# close are in the string, we add them
|
||||
quoted = False
|
||||
string = option['value']
|
||||
for open, close in supported_quotes.items():
|
||||
if not (open in string or close in string):
|
||||
message.content += f"{open}{string}{close} "
|
||||
quoted = True
|
||||
break
|
||||
|
||||
# all supported quotes are in the message and we cannot add any
|
||||
# safely, very unlikely but still got to be covered
|
||||
if not quoted:
|
||||
raise errors.UnexpectedQuoteError(string)
|
||||
else:
|
||||
message.content += f'{option.get("value", "")} '
|
||||
|
||||
ctx = await self.get_context(message)
|
||||
ctx.interaction = interaction
|
||||
await self.invoke(ctx)
|
||||
|
||||
|
||||
class Bot(BotBase, discord.Client):
|
||||
"""Represents a discord bot.
|
||||
@ -1103,10 +1221,20 @@ class Bot(BotBase, discord.Client):
|
||||
|
||||
.. versionadded:: 1.7
|
||||
"""
|
||||
pass
|
||||
async def setup(self):
|
||||
if not self.slash_commands:
|
||||
return
|
||||
|
||||
application = self.application_id or (await self.application_info()).id
|
||||
await self._create_application_commands(application, self.http)
|
||||
|
||||
class AutoShardedBot(BotBase, discord.AutoShardedClient):
|
||||
"""This is similar to :class:`.Bot` except that it is inherited from
|
||||
:class:`discord.AutoShardedClient` instead.
|
||||
"""
|
||||
pass
|
||||
async def setup(self):
|
||||
if not self.slash_commands:
|
||||
return
|
||||
|
||||
application = self.application_id or (await self.application_info()).id
|
||||
await self._create_application_commands(application, self.http)
|
||||
|
@ -25,8 +25,8 @@ from __future__ import annotations
|
||||
|
||||
import inspect
|
||||
import re
|
||||
|
||||
from typing import Any, Dict, Generic, List, Optional, TYPE_CHECKING, TypeVar, Union
|
||||
from datetime import timedelta
|
||||
from typing import Any, Dict, Generic, List, Literal, Optional, TYPE_CHECKING, TypeVar, Union, overload
|
||||
|
||||
import discord.abc
|
||||
import discord.utils
|
||||
@ -41,6 +41,8 @@ if TYPE_CHECKING:
|
||||
from discord.member import Member
|
||||
from discord.state import ConnectionState
|
||||
from discord.user import ClientUser, User
|
||||
from discord.webhook import WebhookMessage
|
||||
from discord.interactions import Interaction
|
||||
from discord.voice_client import VoiceProtocol
|
||||
|
||||
from .bot import Bot, AutoShardedBot
|
||||
@ -121,6 +123,7 @@ class Context(discord.abc.Messageable, Generic[BotT]):
|
||||
A boolean that indicates if the command failed to be parsed, checked,
|
||||
or invoked.
|
||||
"""
|
||||
interaction: Optional[Interaction] = None
|
||||
|
||||
def __init__(self,
|
||||
*,
|
||||
@ -395,6 +398,81 @@ class Context(discord.abc.Messageable, Generic[BotT]):
|
||||
except CommandError as e:
|
||||
await cmd.on_help_command_error(self, e)
|
||||
|
||||
|
||||
@overload
|
||||
async def send(self,
|
||||
content: Optional[str] = None,
|
||||
return_message: Literal[False] = False,
|
||||
ephemeral: bool = False, **kwargs: Any
|
||||
) -> Optional[Union[Message, WebhookMessage]]: ...
|
||||
@overload
|
||||
async def send(self,
|
||||
content: Optional[str] = None,
|
||||
return_message: Literal[True] = True,
|
||||
ephemeral: bool = False, **kwargs: Any
|
||||
) -> Union[Message, WebhookMessage]: ...
|
||||
|
||||
async def send(self,
|
||||
content: Optional[str] = None,
|
||||
return_message: bool = True,
|
||||
ephemeral: bool = False, **kwargs: Any
|
||||
) -> Optional[Union[Message, WebhookMessage]]:
|
||||
"""
|
||||
|coro|
|
||||
|
||||
A shortcut method to :meth:`.abc.Messageable.send` with interaction helpers.
|
||||
|
||||
This function takes all the parameters of :meth:`.abc.Messageable.send` plus the following:
|
||||
|
||||
Parameters
|
||||
------------
|
||||
return_message: :class:`bool`
|
||||
Ignored if not in a slash command context.
|
||||
If this is set to False more native interaction methods will be used.
|
||||
ephemeral: :class:`bool`
|
||||
Ignored if not in a slash command context.
|
||||
Indicates if the message should only be visible to the user who started the interaction.
|
||||
If a view is sent with an ephemeral message and it has no timeout set then the timeout
|
||||
is set to 15 minutes.
|
||||
|
||||
Returns
|
||||
--------
|
||||
Optional[Union[:class:`.Message`, :class:`.WebhookMessage`]]
|
||||
In a slash command context, the message that was sent if return_message is True.
|
||||
|
||||
In a normal context, it always returns a :class:`.Message`
|
||||
"""
|
||||
|
||||
if (
|
||||
self.interaction is None
|
||||
or (
|
||||
self.interaction.response.responded_at is not None
|
||||
and discord.utils.utcnow() - self.interaction.response.responded_at >= timedelta(minutes=15)
|
||||
)):
|
||||
return await super().send(content, **kwargs)
|
||||
|
||||
# Remove unsupported arguments from kwargs
|
||||
kwargs.pop("nonce", None)
|
||||
kwargs.pop("stickers", None)
|
||||
kwargs.pop("reference", None)
|
||||
kwargs.pop("delete_after", None)
|
||||
kwargs.pop("mention_author", None)
|
||||
|
||||
if not (
|
||||
return_message
|
||||
or self.interaction.response.is_done()
|
||||
or any(arg in kwargs for arg in ("file", "files", "allowed_mentions"))
|
||||
):
|
||||
send = self.interaction.response.send_message
|
||||
else:
|
||||
# We have to defer in order to use the followup webhook
|
||||
if not self.interaction.response.is_done():
|
||||
await self.interaction.response.defer(ephemeral=ephemeral)
|
||||
|
||||
send = self.interaction.followup.send
|
||||
|
||||
return await send(content, ephemeral=ephemeral, **kwargs) # type: ignore
|
||||
|
||||
@discord.utils.copy_doc(Message.reply)
|
||||
async def reply(self, content: Optional[str] = None, **kwargs: Any) -> Message:
|
||||
return await self.message.reply(content, **kwargs)
|
||||
|
@ -77,6 +77,7 @@ __all__ = (
|
||||
'GuildStickerConverter',
|
||||
'clean_content',
|
||||
'Greedy',
|
||||
'Option',
|
||||
'run_converters',
|
||||
)
|
||||
|
||||
@ -96,6 +97,9 @@ T_co = TypeVar('T_co', covariant=True)
|
||||
CT = TypeVar('CT', bound=discord.abc.GuildChannel)
|
||||
TT = TypeVar('TT', bound=discord.Thread)
|
||||
|
||||
NT = TypeVar('NT', bound=str)
|
||||
DT = TypeVar('DT', bound=str)
|
||||
|
||||
|
||||
@runtime_checkable
|
||||
class Converter(Protocol[T_co]):
|
||||
@ -1004,6 +1008,20 @@ class Greedy(List[T]):
|
||||
|
||||
return cls(converter=converter)
|
||||
|
||||
if TYPE_CHECKING:
|
||||
def Option(default: T = inspect.Parameter.empty, *, name: str = None, description: str) -> T: ...
|
||||
else:
|
||||
class Option(Generic[T, DT, NT]):
|
||||
description: DT
|
||||
name: Optional[NT]
|
||||
default: Union[T, inspect.Parameter.empty]
|
||||
__slots__ = ('name', 'default', 'description',)
|
||||
|
||||
def __init__(self, default: T = inspect.Parameter.empty, *, name: NT = None, description: DT) -> None:
|
||||
self.description = description
|
||||
self.default = default
|
||||
self.name = name
|
||||
|
||||
|
||||
def _convert_to_bool(argument: str) -> bool:
|
||||
lowered = argument.lower()
|
||||
|
@ -23,6 +23,7 @@ DEALINGS IN THE SOFTWARE.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
|
||||
from typing import (
|
||||
Any,
|
||||
Callable,
|
||||
@ -38,18 +39,21 @@ from typing import (
|
||||
TypeVar,
|
||||
Type,
|
||||
TYPE_CHECKING,
|
||||
cast,
|
||||
overload,
|
||||
)
|
||||
import asyncio
|
||||
import functools
|
||||
import inspect
|
||||
import datetime
|
||||
from collections import defaultdict
|
||||
from operator import itemgetter
|
||||
|
||||
import discord
|
||||
|
||||
from .errors import *
|
||||
from .cooldowns import Cooldown, BucketType, CooldownMapping, MaxConcurrency, DynamicCooldownMapping
|
||||
from .converter import run_converters, get_converter, Greedy
|
||||
from .converter import run_converters, get_converter, Greedy, Option
|
||||
from ._types import _BaseCommand
|
||||
from .cog import Cog
|
||||
from .context import Context
|
||||
@ -59,6 +63,7 @@ if TYPE_CHECKING:
|
||||
from typing_extensions import Concatenate, ParamSpec, TypeGuard
|
||||
|
||||
from discord.message import Message
|
||||
from discord.types.interactions import EditApplicationCommand
|
||||
|
||||
from ._types import (
|
||||
Coro,
|
||||
@ -106,6 +111,16 @@ ContextT = TypeVar('ContextT', bound='Context')
|
||||
GroupT = TypeVar('GroupT', bound='Group')
|
||||
HookT = TypeVar('HookT', bound='Hook')
|
||||
ErrorT = TypeVar('ErrorT', bound='Error')
|
||||
application_option_type_lookup = {
|
||||
str: 3,
|
||||
bool: 5,
|
||||
int: 4,
|
||||
(discord.Member, discord.User): 6, # Preferably discord.abc.User, but 'Protocols with non-method members don't support issubclass()'
|
||||
(discord.abc.GuildChannel, discord.DMChannel): 7,
|
||||
discord.Role: 8,
|
||||
discord.Object: 9,
|
||||
float: 10
|
||||
}
|
||||
|
||||
if TYPE_CHECKING:
|
||||
P = ParamSpec('P')
|
||||
@ -123,13 +138,19 @@ def unwrap_function(function: Callable[..., Any]) -> Callable[..., Any]:
|
||||
return function
|
||||
|
||||
|
||||
def get_signature_parameters(function: Callable[..., Any], globalns: Dict[str, Any]) -> Dict[str, inspect.Parameter]:
|
||||
def get_signature_parameters(function: Callable[..., Any], globalns: Dict[str, Any]) -> Tuple[Dict[str, inspect.Parameter], Dict[str, str]]:
|
||||
signature = inspect.signature(function)
|
||||
params = {}
|
||||
cache: Dict[str, Any] = {}
|
||||
descriptions = defaultdict(lambda: 'no description')
|
||||
eval_annotation = discord.utils.evaluate_annotation
|
||||
for name, parameter in signature.parameters.items():
|
||||
annotation = parameter.annotation
|
||||
if isinstance(parameter.default, Option): # type: ignore
|
||||
option = parameter.default
|
||||
descriptions[name] = option.description
|
||||
parameter = parameter.replace(default=option.default)
|
||||
|
||||
if annotation is parameter.empty:
|
||||
params[name] = parameter
|
||||
continue
|
||||
@ -143,7 +164,7 @@ def get_signature_parameters(function: Callable[..., Any], globalns: Dict[str, A
|
||||
|
||||
params[name] = parameter.replace(annotation=annotation)
|
||||
|
||||
return params
|
||||
return params, descriptions
|
||||
|
||||
|
||||
def wrap_callback(coro):
|
||||
@ -269,8 +290,8 @@ class Command(_BaseCommand, Generic[CogT, P, T]):
|
||||
which calls converters. If ``False`` then cooldown processing is done
|
||||
first and then the converters are called second. Defaults to ``False``.
|
||||
extras: :class:`dict`
|
||||
A dict of user provided extras to attach to the Command.
|
||||
|
||||
A dict of user provided extras to attach to the Command.
|
||||
|
||||
.. note::
|
||||
This object may be copied by the library.
|
||||
|
||||
@ -309,6 +330,8 @@ class Command(_BaseCommand, Generic[CogT, P, T]):
|
||||
|
||||
self.callback = func
|
||||
self.enabled: bool = kwargs.get('enabled', True)
|
||||
self.slash_command: Optional[bool] = kwargs.get("slash_command", None)
|
||||
self.normal_command: Optional[bool] = kwargs.get("normal_command", None)
|
||||
|
||||
help_doc = kwargs.get('help')
|
||||
if help_doc is not None:
|
||||
@ -344,7 +367,7 @@ class Command(_BaseCommand, Generic[CogT, P, T]):
|
||||
cooldown = func.__commands_cooldown__
|
||||
except AttributeError:
|
||||
cooldown = kwargs.get('cooldown')
|
||||
|
||||
|
||||
if cooldown is None:
|
||||
buckets = CooldownMapping(cooldown, BucketType.default)
|
||||
elif isinstance(cooldown, CooldownMapping):
|
||||
@ -406,7 +429,7 @@ class Command(_BaseCommand, Generic[CogT, P, T]):
|
||||
except AttributeError:
|
||||
globalns = {}
|
||||
|
||||
self.params = get_signature_parameters(function, globalns)
|
||||
self.params, self.option_descriptions = get_signature_parameters(function, globalns)
|
||||
|
||||
def add_check(self, func: Check) -> None:
|
||||
"""Adds a check to the command.
|
||||
@ -1098,7 +1121,13 @@ class Command(_BaseCommand, Generic[CogT, P, T]):
|
||||
A boolean indicating if the command can be invoked.
|
||||
"""
|
||||
|
||||
if not self.enabled:
|
||||
if not self.enabled or (
|
||||
ctx.interaction is not None
|
||||
and self.slash_command is False
|
||||
) or (
|
||||
ctx.interaction is None
|
||||
and self.normal_command is False
|
||||
):
|
||||
raise DisabledCommand(f'{self.name} command is disabled')
|
||||
|
||||
original = ctx.command
|
||||
@ -1125,6 +1154,58 @@ class Command(_BaseCommand, Generic[CogT, P, T]):
|
||||
finally:
|
||||
ctx.command = original
|
||||
|
||||
def to_application_command(self, nested: int = 0) -> Optional[EditApplicationCommand]:
|
||||
if self.slash_command is False:
|
||||
return
|
||||
elif nested == 3:
|
||||
raise ValueError(f"{self.qualified_name} is too deeply nested!")
|
||||
|
||||
payload = {
|
||||
"name": self.name,
|
||||
"description": self.short_doc or "no description",
|
||||
"options": []
|
||||
}
|
||||
if nested != 0:
|
||||
payload["type"] = 1
|
||||
|
||||
for name, param in self.clean_params.items():
|
||||
annotation: Type[Any] = param.annotation if param.annotation is not param.empty else str
|
||||
origin = getattr(param.annotation, "__origin__", None)
|
||||
|
||||
if origin is None and isinstance(annotation, Greedy):
|
||||
annotation = annotation.converter
|
||||
origin = Greedy
|
||||
|
||||
option: Dict[str, Any] = {
|
||||
"name": name,
|
||||
"description": self.option_descriptions[name],
|
||||
"required": param.default is param.empty and not self._is_typing_optional(annotation),
|
||||
}
|
||||
|
||||
annotation = cast(Any, annotation)
|
||||
if not option["required"] and origin is not None and len(annotation.__args__) == 2:
|
||||
# Unpack Optional[T] (Union[T, None]) into just T
|
||||
annotation, origin = annotation.__args__[0], None
|
||||
|
||||
if origin is None:
|
||||
option["type"] = next(
|
||||
(num for t, num in application_option_type_lookup.items()
|
||||
if issubclass(annotation, t)), 3
|
||||
)
|
||||
elif origin is Literal and len(origin.__args__) <= 25: # type: ignore
|
||||
option["choices"] = [{
|
||||
"name": literal_value,
|
||||
"value": literal_value
|
||||
} for literal_value in origin.__args__] # type: ignore
|
||||
else:
|
||||
option["type"] = 3 # STRING
|
||||
|
||||
payload["options"].append(option)
|
||||
|
||||
# Now we have all options, make sure required is before optional.
|
||||
payload["options"] = sorted(payload["options"], key=itemgetter("required"), reverse=True)
|
||||
return payload # type: ignore
|
||||
|
||||
class GroupMixin(Generic[CogT]):
|
||||
"""A mixin that implements common functionality for classes that behave
|
||||
similar to :class:`.Group` and are allowed to register commands.
|
||||
@ -1492,6 +1573,22 @@ class Group(GroupMixin[CogT], Command[CogT, P, T]):
|
||||
view.previous = previous
|
||||
await super().reinvoke(ctx, call_hooks=call_hooks)
|
||||
|
||||
def to_application_command(self, nested: int = 0) -> Optional[EditApplicationCommand]:
|
||||
if self.slash_command is False:
|
||||
return
|
||||
elif nested == 2:
|
||||
raise ValueError(f"{self.qualified_name} is too deeply nested for slash commands!")
|
||||
|
||||
return { # type: ignore
|
||||
"name": self.name,
|
||||
"type": int(not (nested - 1)) + 1,
|
||||
"description": self.short_doc or 'no description',
|
||||
"options": [
|
||||
cmd.to_application_command(nested=nested+1)
|
||||
for cmd in self.commands
|
||||
]
|
||||
}
|
||||
|
||||
# Decorators
|
||||
|
||||
@overload
|
||||
|
@ -615,7 +615,7 @@ class HelpCommand:
|
||||
:class:`.abc.Messageable`
|
||||
The destination where the help command will be output.
|
||||
"""
|
||||
return self.context.channel
|
||||
return self.context
|
||||
|
||||
async def send_error_message(self, error):
|
||||
"""|coro|
|
||||
@ -977,6 +977,14 @@ class DefaultHelpCommand(HelpCommand):
|
||||
for page in self.paginator.pages:
|
||||
await destination.send(page)
|
||||
|
||||
interaction = self.context.interaction
|
||||
if (
|
||||
interaction is not None
|
||||
and destination == self.context.author
|
||||
and not interaction.response.is_done()
|
||||
):
|
||||
await interaction.response.send_message("Sent help to your DMs!", ephemeral=True)
|
||||
|
||||
def add_command_formatting(self, command):
|
||||
"""A utility function to format the non-indented block of commands and groups.
|
||||
|
||||
@ -1007,7 +1015,7 @@ class DefaultHelpCommand(HelpCommand):
|
||||
elif self.dm_help is None and len(self.paginator) > self.dm_help_threshold:
|
||||
return ctx.author
|
||||
else:
|
||||
return ctx.channel
|
||||
return ctx
|
||||
|
||||
async def prepare_help_command(self, ctx, command):
|
||||
self.paginator.clear()
|
||||
|
@ -25,7 +25,7 @@ DEALINGS IN THE SOFTWARE.
|
||||
from .errors import UnexpectedQuoteError, InvalidEndOfQuotedStringError, ExpectedClosingQuoteError
|
||||
|
||||
# map from opening quotes to closing quotes
|
||||
_quotes = {
|
||||
supported_quotes = {
|
||||
'"': '"',
|
||||
"‘": "’",
|
||||
"‚": "‛",
|
||||
@ -44,7 +44,7 @@ _quotes = {
|
||||
"《": "》",
|
||||
"〈": "〉",
|
||||
}
|
||||
_all_quotes = set(_quotes.keys()) | set(_quotes.values())
|
||||
_all_quotes = set(supported_quotes.keys()) | set(supported_quotes.values())
|
||||
|
||||
class StringView:
|
||||
def __init__(self, buffer):
|
||||
@ -129,7 +129,7 @@ class StringView:
|
||||
if current is None:
|
||||
return None
|
||||
|
||||
close_quote = _quotes.get(current)
|
||||
close_quote = supported_quotes.get(current)
|
||||
is_quoted = bool(close_quote)
|
||||
if is_quoted:
|
||||
result = []
|
||||
|
@ -47,6 +47,8 @@ __all__ = (
|
||||
)
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from datetime import datetime
|
||||
|
||||
from .types.interactions import (
|
||||
Interaction as InteractionPayload,
|
||||
InteractionData,
|
||||
@ -58,11 +60,11 @@ if TYPE_CHECKING:
|
||||
from aiohttp import ClientSession
|
||||
from .embeds import Embed
|
||||
from .ui.view import View
|
||||
from .channel import VoiceChannel, StageChannel, TextChannel, CategoryChannel, StoreChannel, PartialMessageable
|
||||
from .channel import TextChannel, CategoryChannel, StoreChannel, PartialMessageable
|
||||
from .threads import Thread
|
||||
|
||||
InteractionChannel = Union[
|
||||
VoiceChannel, StageChannel, TextChannel, CategoryChannel, StoreChannel, Thread, PartialMessageable
|
||||
TextChannel, CategoryChannel, StoreChannel, Thread, PartialMessageable
|
||||
]
|
||||
|
||||
MISSING: Any = utils.MISSING
|
||||
@ -179,7 +181,7 @@ class Interaction:
|
||||
type = ChannelType.text if self.guild_id is not None else ChannelType.private
|
||||
return PartialMessageable(state=self._state, id=self.channel_id, type=type)
|
||||
return None
|
||||
return channel
|
||||
return channel # type: ignore
|
||||
|
||||
@property
|
||||
def permissions(self) -> Permissions:
|
||||
@ -369,20 +371,20 @@ class InteractionResponse:
|
||||
"""
|
||||
|
||||
__slots__: Tuple[str, ...] = (
|
||||
'_responded',
|
||||
'responded_at',
|
||||
'_parent',
|
||||
)
|
||||
|
||||
def __init__(self, parent: Interaction):
|
||||
self.responded_at: Optional[datetime] = None
|
||||
self._parent: Interaction = parent
|
||||
self._responded: bool = False
|
||||
|
||||
def is_done(self) -> bool:
|
||||
""":class:`bool`: Indicates whether an interaction response has been done before.
|
||||
|
||||
An interaction can only be responded to once.
|
||||
"""
|
||||
return self._responded
|
||||
return self.responded_at is not None
|
||||
|
||||
async def defer(self, *, ephemeral: bool = False) -> None:
|
||||
"""|coro|
|
||||
@ -405,7 +407,7 @@ class InteractionResponse:
|
||||
InteractionResponded
|
||||
This interaction has already been responded to before.
|
||||
"""
|
||||
if self._responded:
|
||||
if self.is_done():
|
||||
raise InteractionResponded(self._parent)
|
||||
|
||||
defer_type: int = 0
|
||||
@ -423,7 +425,8 @@ class InteractionResponse:
|
||||
await adapter.create_interaction_response(
|
||||
parent.id, parent.token, session=parent._session, type=defer_type, data=data
|
||||
)
|
||||
self._responded = True
|
||||
|
||||
self.responded_at = utils.utcnow()
|
||||
|
||||
async def pong(self) -> None:
|
||||
"""|coro|
|
||||
@ -439,7 +442,7 @@ class InteractionResponse:
|
||||
InteractionResponded
|
||||
This interaction has already been responded to before.
|
||||
"""
|
||||
if self._responded:
|
||||
if self.is_done():
|
||||
raise InteractionResponded(self._parent)
|
||||
|
||||
parent = self._parent
|
||||
@ -448,7 +451,7 @@ class InteractionResponse:
|
||||
await adapter.create_interaction_response(
|
||||
parent.id, parent.token, session=parent._session, type=InteractionResponseType.pong.value
|
||||
)
|
||||
self._responded = True
|
||||
self.responded_at = utils.utcnow()
|
||||
|
||||
async def send_message(
|
||||
self,
|
||||
@ -494,7 +497,7 @@ class InteractionResponse:
|
||||
InteractionResponded
|
||||
This interaction has already been responded to before.
|
||||
"""
|
||||
if self._responded:
|
||||
if self.is_done():
|
||||
raise InteractionResponded(self._parent)
|
||||
|
||||
payload: Dict[str, Any] = {
|
||||
@ -537,7 +540,7 @@ class InteractionResponse:
|
||||
|
||||
self._parent._state.store_view(view)
|
||||
|
||||
self._responded = True
|
||||
self.responded_at = utils.utcnow()
|
||||
|
||||
async def edit_message(
|
||||
self,
|
||||
@ -578,7 +581,7 @@ class InteractionResponse:
|
||||
InteractionResponded
|
||||
This interaction has already been responded to before.
|
||||
"""
|
||||
if self._responded:
|
||||
if self.is_done():
|
||||
raise InteractionResponded(self._parent)
|
||||
|
||||
parent = self._parent
|
||||
@ -629,7 +632,7 @@ class InteractionResponse:
|
||||
if view and not view.is_finished():
|
||||
state.store_view(view, message_id)
|
||||
|
||||
self._responded = True
|
||||
self.responded_at = utils.utcnow()
|
||||
|
||||
|
||||
class _InteractionMessageState:
|
||||
|
@ -428,7 +428,7 @@ class Decoder(_OpusStruct):
|
||||
@overload
|
||||
def decode(self, data: bytes, *, fec: bool) -> bytes:
|
||||
...
|
||||
|
||||
|
||||
@overload
|
||||
def decode(self, data: Literal[None], *, fec: Literal[False]) -> bytes:
|
||||
...
|
||||
|
@ -310,7 +310,7 @@ class Template:
|
||||
@property
|
||||
def url(self) -> str:
|
||||
""":class:`str`: The template url.
|
||||
|
||||
|
||||
.. versionadded:: 2.0
|
||||
"""
|
||||
return f'https://discord.new/{self.code}'
|
||||
|
@ -273,7 +273,7 @@ class Thread(Messageable, Hashable):
|
||||
if parent is None:
|
||||
raise ClientException('Parent channel not found')
|
||||
return parent.category
|
||||
|
||||
|
||||
@property
|
||||
def category_id(self) -> Optional[int]:
|
||||
"""The category channel ID the parent channel belongs to, if applicable.
|
||||
|
@ -229,8 +229,7 @@ class _EditApplicationCommandOptional(TypedDict, total=False):
|
||||
description: str
|
||||
options: Optional[List[ApplicationCommandOption]]
|
||||
type: ApplicationCommandType
|
||||
|
||||
default_permission: bool
|
||||
|
||||
class EditApplicationCommand(_EditApplicationCommandOptional):
|
||||
name: str
|
||||
default_permission: bool
|
||||
|
@ -357,7 +357,7 @@ class View:
|
||||
return
|
||||
|
||||
await item.callback(interaction)
|
||||
if not interaction.response._responded:
|
||||
if not interaction.response.is_done():
|
||||
await interaction.response.defer()
|
||||
except Exception as e:
|
||||
return await self.on_error(e, item, interaction)
|
||||
|
@ -66,7 +66,7 @@ if TYPE_CHECKING:
|
||||
VoiceServerUpdate as VoiceServerUpdatePayload,
|
||||
SupportedModes,
|
||||
)
|
||||
|
||||
|
||||
|
||||
has_nacl: bool
|
||||
|
||||
|
@ -355,7 +355,7 @@ texinfo_documents = [
|
||||
#texinfo_no_detailmenu = False
|
||||
|
||||
def setup(app):
|
||||
if app.config.language == 'ja':
|
||||
app.config.intersphinx_mapping['py'] = ('https://docs.python.org/ja/3', None)
|
||||
app.config.html_context['discord_invite'] = 'https://discord.gg/nXzj3dg'
|
||||
app.config.resource_links['discord'] = 'https://discord.gg/nXzj3dg'
|
||||
if app.config.language == 'ja':
|
||||
app.config.intersphinx_mapping['py'] = ('https://docs.python.org/ja/3', None)
|
||||
app.config.html_context['discord_invite'] = 'https://discord.gg/nXzj3dg'
|
||||
app.config.resource_links['discord'] = 'https://discord.gg/nXzj3dg'
|
||||
|
@ -52,4 +52,3 @@ def setup(app):
|
||||
app.add_node(details, html=(visit_details_node, depart_details_node))
|
||||
app.add_node(summary, html=(visit_summary_node, depart_summary_node))
|
||||
app.add_directive('details', DetailsDirective)
|
||||
|
||||
|
@ -5,7 +5,7 @@ from sphinx.util import logging as sphinx_logging
|
||||
|
||||
|
||||
class NitpickFileIgnorer(logging.Filter):
|
||||
|
||||
|
||||
def __init__(self, app: Sphinx) -> None:
|
||||
self.app = app
|
||||
super().__init__()
|
||||
|
@ -78,7 +78,7 @@ class ChannelOrMemberConverter(commands.Converter):
|
||||
async def notify(ctx: commands.Context, target: ChannelOrMemberConverter):
|
||||
# This command signature utilises the custom converter written above
|
||||
# What will happen during command invocation is that the `target` above will be passed to
|
||||
# the `argument` parameter of the `ChannelOrMemberConverter.convert` method and
|
||||
# the `argument` parameter of the `ChannelOrMemberConverter.convert` method and
|
||||
# the conversion will go through the process defined there.
|
||||
|
||||
await target.send(f'Hello, {target.name}!')
|
||||
|
@ -27,7 +27,7 @@ class MyBot(commands.Bot):
|
||||
# subclass to the super() method, which tells the bot to
|
||||
# use the new MyContext class
|
||||
return await super().get_context(message, cls=cls)
|
||||
|
||||
|
||||
|
||||
bot = MyBot(command_prefix='!')
|
||||
|
||||
@ -43,7 +43,7 @@ async def guess(ctx, number: int):
|
||||
await ctx.tick(number == value)
|
||||
|
||||
# IMPORTANT: You shouldn't hard code your token
|
||||
# these are very important, and leaking them can
|
||||
# these are very important, and leaking them can
|
||||
# let people do very malicious things with your
|
||||
# bot. Try to use a file or something to keep
|
||||
# them private, and don't commit it to GitHub
|
||||
|
@ -5,7 +5,7 @@ from discord.ext import commands
|
||||
|
||||
bot = commands.Bot(command_prefix=commands.when_mentioned, description="Nothing to see here!")
|
||||
|
||||
# the `hidden` keyword argument hides it from the help command.
|
||||
# the `hidden` keyword argument hides it from the help command.
|
||||
@bot.group(hidden=True)
|
||||
async def secret(ctx: commands.Context):
|
||||
"""What is this "secret" you speak of?"""
|
||||
@ -13,7 +13,7 @@ async def secret(ctx: commands.Context):
|
||||
await ctx.send('Shh!', delete_after=5)
|
||||
|
||||
def create_overwrites(ctx, *objects):
|
||||
"""This is just a helper function that creates the overwrites for the
|
||||
"""This is just a helper function that creates the overwrites for the
|
||||
voice/text channels.
|
||||
|
||||
A `discord.PermissionOverwrite` allows you to determine the permissions
|
||||
@ -45,10 +45,10 @@ def create_overwrites(ctx, *objects):
|
||||
@secret.command()
|
||||
@commands.guild_only()
|
||||
async def text(ctx: commands.Context, name: str, *objects: typing.Union[discord.Role, discord.Member]):
|
||||
"""This makes a text channel with a specified name
|
||||
"""This makes a text channel with a specified name
|
||||
that is only visible to roles or members that are specified.
|
||||
"""
|
||||
|
||||
|
||||
overwrites = create_overwrites(ctx, *objects)
|
||||
|
||||
await ctx.guild.create_text_channel(
|
||||
|
@ -24,7 +24,7 @@ class Dropdown(discord.ui.Select):
|
||||
async def callback(self, interaction: discord.Interaction):
|
||||
# Use the interaction object to send a response message containing
|
||||
# the user's favourite colour or choice. The self object refers to the
|
||||
# Select object, and the values attribute gets a list of the user's
|
||||
# Select object, and the values attribute gets a list of the user's
|
||||
# selected options. We only want the first one.
|
||||
await interaction.response.send_message(f'Your favourite colour is {self.values[0]}')
|
||||
|
||||
@ -44,8 +44,8 @@ class Bot(commands.Bot):
|
||||
async def on_ready(self):
|
||||
print(f'Logged in as {self.user} (ID: {self.user.id})')
|
||||
print('------')
|
||||
|
||||
|
||||
|
||||
|
||||
bot = Bot()
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user