Skip to content

PyDrocsid.command

add_reactions async

add_reactions(ctx: Context[Any] | Message, *emojis: str) -> None

Add reactions to a given message.

Parameters:

  • ctx (Context[Any] | Message) –

    the message or context

  • emojis

    emoji names to react with

Source code in PyDrocsid/command.py
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
async def add_reactions(ctx: Context[Any] | Message, *emojis: str) -> None:
    """
    Add reactions to a given message.

    :param ctx: the message or context
    :param emojis: emoji names to react with
    """

    message: Message = ctx if isinstance(ctx, Message) else ctx.message

    try:
        for emoji in emojis:
            await message.add_reaction(name_to_emoji[emoji])
    except Forbidden:
        if not isinstance(message.channel, (TextChannel, Thread)):
            return

        await call_event_handlers("permission_error", ctx.guild, t.could_not_add_reaction(message.channel.mention))

can_run_command async

can_run_command(command: Command[Cog, Any, Any], ctx: Context[Bot]) -> bool

Return whether a command can be executed in a given context.

Source code in PyDrocsid/command.py
103
104
105
106
107
108
109
async def can_run_command(command: Command[Cog, Any, Any], ctx: Context[Bot]) -> bool:
    """Return whether a command can be executed in a given context."""

    try:
        return await command.can_run(ctx)
    except CommandError:
        return False

docs

docs(text: str) -> Callable[[Func], Func]

Decorator for setting the docstring of a function.

Source code in PyDrocsid/command.py
60
61
62
63
64
65
66
67
def docs(text: str) -> Callable[[Func], Func]:
    """Decorator for setting the docstring of a function."""

    def deco(f: Func) -> Func:
        f.__doc__ = text
        return f

    return deco

get_optional_permissions

get_optional_permissions(command: Command[Cog, Any, Any]) -> list[BasePermission]

Get the optional permissions of a given command, set by the optional_permissions decorator.

Source code in PyDrocsid/command.py
80
81
82
83
def get_optional_permissions(command: Command[Cog, Any, Any]) -> list[BasePermission]:
    """Get the optional permissions of a given command, set by the optional_permissions decorator."""

    return getattr(command.callback, "optional_permissions", [])

make_error

make_error(message: str, user: User | Member | None = None) -> Embed

Create an error embed with an optional author.

Parameters:

  • message (str) –

    the error message

  • user (User | Member | None) –

    an optional user

Returns:

  • Embed

    the error embed

Source code in PyDrocsid/command.py
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
def make_error(message: str, user: User | Member | None = None) -> Embed:
    """
    Create an error embed with an optional author.

    :param message: the error message
    :param user: an optional user
    :return: the error embed
    """

    embed = Embed(title=t.error, colour=MaterialColors.error, description=str(message))

    if user:
        embed.set_author(name=str(user), icon_url=user.display_avatar.url)

    return embed

optional_permissions

optional_permissions(*permissions: BasePermission) -> Callable[[Func], Func]

Decorator for setting optional permissions of a command.

Source code in PyDrocsid/command.py
70
71
72
73
74
75
76
77
def optional_permissions(*permissions: BasePermission) -> Callable[[Func], Func]:
    """Decorator for setting optional permissions of a command."""

    def deco(f: Func) -> Func:
        f.optional_permissions = list(permissions)
        return f

    return deco

reply async

reply(ctx: Message | Messageable | InteractionResponse, *args: Any, no_reply: bool = False, **kwargs: Any) -> Message

Reply to a message and link response to this message.

Parameters:

  • ctx (Message | Messageable | InteractionResponse) –

    the context/message/messageable to reply to

  • args

    positional arguments to pass to ctx.send/ctx.reply

  • no_reply (bool) –

    whether to use ctx.send instead of ctx.reply

  • kwargs

    keyword arguments to pass to ctx.send/ctx.reply

Returns:

  • Message

    the message that was sent

Source code in PyDrocsid/command.py
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
async def reply(
    ctx: Message | Messageable | InteractionResponse, *args: Any, no_reply: bool = False, **kwargs: Any
) -> Message:
    """
    Reply to a message and link response to this message.

    :param ctx: the context/message/messageable to reply to
    :param args: positional arguments to pass to ctx.send/ctx.reply
    :param no_reply: whether to use ctx.send instead of ctx.reply
    :param kwargs: keyword arguments to pass to ctx.send/ctx.reply
    :return: the message that was sent
    """

    if isinstance(ctx, InteractionResponse):
        interaction = await ctx.send_message(*args, **kwargs, ephemeral=True)
        return await interaction.original_message()

    if isinstance(channel := ctx.channel if isinstance(ctx, (Message, Context)) else ctx, TextChannel):
        try:
            check_message_send_permissions(
                channel, check_file=bool(kwargs.get("file")), check_embed=bool(kwargs.get("embed"))
            )
        except CommandError as e:
            raise PermissionError(channel.guild, e.args[0])

    msg: Message
    if REPLY and isinstance(ctx, (Context, Message)) and not no_reply:
        msg = await ctx.reply(*args, **kwargs, mention_author=MENTION_AUTHOR)
    elif isinstance(ctx, Message):
        msg = await ctx.channel.send(*args, **kwargs)
    else:
        msg = await ctx.send(*args, **kwargs)

    if isinstance(ctx, (Context, Message)):
        await link_response(ctx, msg)

    return msg