2222FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
2323DEALINGS IN THE SOFTWARE.
2424"""
25+ from __future__ import annotations
2526
2627import inspect
27- from typing import Any , List , Union , Optional
28+ from typing import TYPE_CHECKING , Any , List , Union , Optional , Callable , Dict
2829
2930import 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+ )
3240from ..commands .converter import _convert_to_bool , run_converters
3341from ..commands import (
3442 Command ,
4351)
4452from ...utils import get , filter_params , find
4553
54+ if TYPE_CHECKING :
55+ from .context import BridgeApplicationContext , BridgeExtContext
56+
4657
4758__all__ = (
4859 "BridgeCommand" ,
5465 "BridgeExtGroup" ,
5566 "BridgeSlashGroup" ,
5667 "map_to" ,
68+ "guild_only" ,
69+ "has_permissions" ,
5770)
5871
5972
@@ -183,6 +196,11 @@ def add_to(self, bot: ExtBot) -> None:
183196 bot .add_application_command (self .slash_variant )
184197 bot .add_command (self .ext_variant )
185198
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+
186204 def error (self , coro ):
187205 """A decorator that registers a coroutine as a local error handler.
188206
@@ -329,7 +347,7 @@ def bridge_group(**kwargs):
329347 Parameters
330348 ----------
331349 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`).
333351 """
334352 def decorator (callback ):
335353 return BridgeCommandGroup (callback , ** kwargs )
@@ -376,6 +394,54 @@ def decorator(callback):
376394 return decorator
377395
378396
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+
379445class MentionableConverter (Converter ):
380446 """A converter that can convert a mention to a user or a role."""
381447
@@ -385,6 +451,7 @@ async def convert(self, ctx, argument):
385451 except BadArgument :
386452 return await UserConverter ().convert (ctx , argument )
387453
454+
388455class AttachmentConverter (Converter ):
389456 async def convert (self , ctx : Context , arg : str ):
390457 try :
0 commit comments