forked from Pycord-Development/pycord
/
core.py
665 lines (520 loc) · 22.1 KB
/
core.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
"""
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,
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)
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.
"""
__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 is 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 user or a role."""
async def convert(self, ctx, argument):
try:
return await RoleConverter().convert(ctx, argument)
except BadArgument:
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,
}
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.
"""
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[self.input_type]
if issubclass(converter, Converter):
converted = await converter().convert(ctx, argument) # type: ignore # protocol class
else:
converted = converter(argument)
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 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
discord.commands.options.Option = BridgeOption
discord.Option = BridgeOption
warn_deprecated(
"Option",
"BridgeOption",
"2.5",
reference="https://github.com/Pycord-Development/pycord/pull/2417",
)