22
22
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23
23
DEALINGS IN THE SOFTWARE.
24
24
"""
25
+ from __future__ import annotations
25
26
26
27
import inspect
27
- from typing import Any , List , Union , Optional
28
+ from typing import TYPE_CHECKING , Any , List , Union , Optional , Callable , Dict
28
29
29
30
import discord .commands .options
30
- from discord import SlashCommandOptionType , Attachment , Option , SlashCommand , SlashCommandGroup
31
- from .context import BridgeApplicationContext
31
+ from discord import (
32
+ ApplicationCommand ,
33
+ SlashCommand ,
34
+ SlashCommandGroup ,
35
+ Permissions ,
36
+ SlashCommandOptionType ,
37
+ Attachment ,
38
+ Option ,
39
+ )
32
40
from ..commands .converter import _convert_to_bool , run_converters
33
41
from ..commands import (
34
42
Command ,
43
51
)
44
52
from ...utils import get , filter_params , find
45
53
54
+ if TYPE_CHECKING :
55
+ from .context import BridgeApplicationContext , BridgeExtContext
56
+
46
57
47
58
__all__ = (
48
59
"BridgeCommand" ,
54
65
"BridgeExtGroup" ,
55
66
"BridgeSlashGroup" ,
56
67
"map_to" ,
68
+ "guild_only" ,
69
+ "has_permissions" ,
57
70
)
58
71
59
72
@@ -183,6 +196,11 @@ def add_to(self, bot: ExtBot) -> None:
183
196
bot .add_application_command (self .slash_variant )
184
197
bot .add_command (self .ext_variant )
185
198
199
+ async def invoke (self , ctx : Union [BridgeExtContext , BridgeApplicationContext ], / , * args , ** kwargs ):
200
+ if ctx .is_app :
201
+ return await self .slash_variant .invoke (ctx )
202
+ return await self .ext_variant .invoke (ctx )
203
+
186
204
def error (self , coro ):
187
205
"""A decorator that registers a coroutine as a local error handler.
188
206
@@ -329,7 +347,7 @@ def bridge_group(**kwargs):
329
347
Parameters
330
348
----------
331
349
kwargs: Optional[Dict[:class:`str`, Any]]
332
- Keyword arguments that are directly passed to the respective command constructors. (:class:`.SlashCommandGroup` and :class:`.ext.commands.Group`)
350
+ Keyword arguments that are directly passed to the respective command constructors (:class:`.SlashCommandGroup` and :class:`.ext.commands.Group`).
333
351
"""
334
352
def decorator (callback ):
335
353
return BridgeCommandGroup (callback , ** kwargs )
@@ -376,6 +394,54 @@ def decorator(callback):
376
394
return decorator
377
395
378
396
397
+ def guild_only ():
398
+ """Intended to work with :class:`.ApplicationCommand` and :class:`BridgeCommand`, adds a :func:`~ext.commands.check`
399
+ that locks the command to only run in guilds, and also registers the command as guild only client-side (on discord).
400
+
401
+ Basically a utility function that wraps both :func:`discord.ext.commands.guild_only` and :func:`discord.commands.guild_only`.
402
+ """
403
+ def predicate (func : Union [Callable , ApplicationCommand ]):
404
+ if isinstance (func , ApplicationCommand ):
405
+ func .guild_only = True
406
+ else :
407
+ func .__guild_only__ = True
408
+
409
+ from ..commands import guild_only
410
+
411
+ return guild_only ()(func )
412
+
413
+ return predicate
414
+
415
+
416
+ def has_permissions (** perms : Dict [str , bool ]):
417
+ """Intended to work with :class:`.SlashCommand` and :class:`BridgeCommand`, adds a
418
+ :func:`~ext.commands.check` that locks the command to be run by people with certain
419
+ permissions inside guilds, and also registers the command as locked behind said permissions.
420
+
421
+ Basically a utility function that wraps both :func:`discord.ext.commands.has_permissions`
422
+ and :func:`discord.commands.default_permissions`.
423
+
424
+ Parameters
425
+ ----------
426
+ \*\*perms: Dict[:class:`str`, :class:`bool`]
427
+ An argument list of permissions to check for.
428
+ """
429
+
430
+ def predicate (func : Union [Callable , ApplicationCommand ]):
431
+ from ..commands import has_permissions
432
+
433
+ func = has_permissions (** perms )(func )
434
+ _perms = Permissions (** perms )
435
+ if isinstance (func , ApplicationCommand ):
436
+ func .default_member_permissions = perms
437
+ else :
438
+ func .__default_member_permissions__ = perms
439
+
440
+ return perms
441
+
442
+ return predicate
443
+
444
+
379
445
class MentionableConverter (Converter ):
380
446
"""A converter that can convert a mention to a user or a role."""
381
447
@@ -385,6 +451,7 @@ async def convert(self, ctx, argument):
385
451
except BadArgument :
386
452
return await UserConverter ().convert (ctx , argument )
387
453
454
+
388
455
class AttachmentConverter (Converter ):
389
456
async def convert (self , ctx : Context , arg : str ):
390
457
try :
0 commit comments