Skip to content

embeds

Embeds module adds some pre-configured embeds to streamline the creation of your help command.

MAX_CHARS_PER_FIELD = 1024 module-attribute

Set the max and default character limit per embed field.

This is a Discord limitation, so we use this for chunking command description lines for the overview embed(s) which allow us to split into multiple fields within a single embed.

Notes

I do not recommend exceeding 400 characters as this can create very tall embeds.

MAX_FIELDS_PER_EMBED = 25 module-attribute

Set the max and default field per embed.

Discord limits this to 25, so that's what has been set as the max here as well.

command_detail_embed(command, *, thumbnail_url=None, guild=None, color=None)

Create and return an embed showing command details.

Parameters:

  • command (AppCommand) –

    The AppCommand retrieved from AppCommandHelp.get_command_named

  • thumbnail_url (Optional[str], default: None ) –

    The image's URL. - changed in 0.5.0 - thumbnail -> thumbnail_url

  • guild (Optional[Guild], default: None ) –

    Guild where this embed will be displayed. Used to convert any role checks into role objects

  • color (Optional[Color], default: None ) –

    Set the color the embed. Default is None

Examples:

from disnake.ext.app_command_help import AppCommandHelp, utils

...
app_command_help = AppCommandHelp(bot)

...
command = app_command_help.get_command_named(name)
embed = utils.command_detail_embed(command, guild=inter.guild)
await inter.response.send_message(embed=embed)

Returns:

  • Embed

    The created embed containing the command details.

Source code in src\helply\utils\embeds.py
 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
def command_detail_embed(
    command: AppCommand,
    *,
    thumbnail_url: Optional[str] = None,
    guild: Optional[disnake.Guild] = None,
    color: Optional[disnake.Color] = None,
) -> disnake.Embed:
    """Create and return an embed showing command details.

    Parameters
    ----------
    command: AppCommand
        The `AppCommand` retrieved from `AppCommandHelp.get_command_named`
    thumbnail_url: Optional[str]
        The image's URL. - *changed in 0.5.0 - thumbnail -> thumbnail_url*
    guild: Optional[disnake.Guild]
        Guild where this embed will be displayed. Used to convert
        any role checks into role objects
    color: Optional[disnake.Color]
        Set the color the embed. Default is None

    Examples
    --------
    ```py
    from disnake.ext.app_command_help import AppCommandHelp, utils

    ...
    app_command_help = AppCommandHelp(bot)

    ...
    command = app_command_help.get_command_named(name)
    embed = utils.command_detail_embed(command, guild=inter.guild)
    await inter.response.send_message(embed=embed)
    ```

    Returns
    -------
    disnake.Embed
        The created embed containing the command details.
    """
    type_ = (
        "Slash Command Details"
        if command.type is AppCommandType.SLASH
        else (
            "User Command Details"
            if command.type is AppCommandType.USER
            else "Message Command Details"
        )
    )

    embed = disnake.Embed(description=f"{command.mention}\n{command.description}", color=color)
    embed.set_author(name=f'{type_} {"(NSFW)" if command.nsfw else ""}')
    if thumbnail_url:
        embed.set_thumbnail(url=thumbnail_url)

    if command.cooldown:
        cooldown = command.cooldown
        text = f"{cooldown.rate} / {cooldown.per}s / {cooldown.type}"
        embed.add_field(name="Cooldown", value=text, inline=False)

    if command.checks.permissions:
        permissions = ", ".join(command.checks.permissions)
        embed.add_field(name="Required Permissions", value=permissions, inline=True)

    if command.checks.roles:
        if guild:
            roles = Helply.roles_from_checks(command.checks, guild)
            role_checks = ", ".join(r.mention for r in roles)
        else:
            role_checks = ", ".join(str(check) for check in command.checks.roles)

        roles_as_string = f"**Required Roles**:\n{role_checks}"
        embed.add_field(name="Required Role(s)", value=roles_as_string, inline=True)

    if command.type is AppCommandType.SLASH:
        embed.set_footer(text="[ required ] | ( optional )")
        if command.args:
            args: str = ""

            for arg in command.args:
                name = f"**[{arg.name}]**" if arg.required else f"**({arg.name})**"
                args += f"{name}: *{arg.description}*\n"

            embed.add_field(name="Parameters", value=args, inline=False)
        else:
            embed.add_field(name="Parameters", value="None", inline=True)

    return embed

commands_overview_embeds(commands, *, thumbnail_url=None, max_field_chars=MAX_CHARS_PER_FIELD, max_fields=MAX_FIELDS_PER_EMBED, color=None, category='')

Create and return one or more embeds containing all commands and descriptions.

Parameters:

  • commands (List[AppCommand]) –

    List of AppCommand received from AppCommandHelp.get_all_commands

  • thumbnail_url (Optional[str], default: None ) –

    The image's URL. - changed in 0.5.0 - thumbnail -> thumbnail_url

  • max_field_chars (int, default: MAX_CHARS_PER_FIELD ) –

    Max number of characters per embed field description, default is MAX_CHARS_PER_FIELD (1024)

  • max_fields (int, default: MAX_FIELDS_PER_EMBED ) –

    Max number of fields per embed. default is MAX_FIELDS_PER_EMBED (10)

  • color (Optional[Color], default: None ) –

    Set the color the embed(s). Default is None

Examples:

from disnake.ext.app_command_help import AppCommandHelp, utils

...
app_command_help = AppCommandHelp(bot)

...
commands = app_command_help.get_all_commands(inter.guild)
embeds = utils.commands_overview_embeds(commands)
await inter.response.send_message(embeds=embeds)

Returns:

  • List[Embed]

    A list of disnake.Embed containing an overview of the commands.

Source code in src\helply\utils\embeds.py
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
def commands_overview_embeds(
    commands: List[AppCommand],
    *,
    thumbnail_url: Optional[str] = None,
    max_field_chars: int = MAX_CHARS_PER_FIELD,
    max_fields: int = MAX_FIELDS_PER_EMBED,
    color: Optional[disnake.Color] = None,
    category: str = "",
) -> List[disnake.Embed]:
    """Create and return one or more embeds containing all commands and descriptions.

    Parameters
    ----------
    commands: List[AppCommand]
        List of `AppCommand` received from `AppCommandHelp.get_all_commands`
    thumbnail_url: Optional[str]
        The image's URL. - *changed in 0.5.0 - thumbnail -> thumbnail_url*
    max_field_chars: int
        Max number of characters per embed field description, default is MAX_CHARS_PER_FIELD (1024)
    max_fields: int
        Max number of fields per embed. default is MAX_FIELDS_PER_EMBED (10)
    color: Optional[disnake.Color]
        Set the color the embed(s). Default is None


    Examples
    --------
    ```py
    from disnake.ext.app_command_help import AppCommandHelp, utils

    ...
    app_command_help = AppCommandHelp(bot)

    ...
    commands = app_command_help.get_all_commands(inter.guild)
    embeds = utils.commands_overview_embeds(commands)
    await inter.response.send_message(embeds=embeds)
    ```

    Returns
    -------
    List[disnake.Embed]
        A list of disnake.Embed containing an overview of the commands.
    """
    if max_field_chars > MAX_CHARS_PER_FIELD:
        max_field_chars = MAX_CHARS_PER_FIELD

    if max_fields > MAX_FIELDS_PER_EMBED:
        max_fields = MAX_FIELDS_PER_EMBED

    msg = "%s must be greater or equal to one. Got %s"
    if max_field_chars <= 0:
        raise ValueError(msg % ("'max_field_chars'", str(max_field_chars)))

    if max_fields <= 0:
        raise ValueError(msg % ("'max_fields'", str(max_fields)))

    embeds: list[disnake.Embed] = []
    current_embed: Optional[disnake.Embed] = None
    current_field: str = ""
    current_field_chars: int = 0

    for command in commands:
        type_ = (
            "Slash Command"
            if command.type is AppCommandType.SLASH
            else "User Command"
            if command.type is AppCommandType.USER
            else "Message Command"
        )

        nsfw = "*(NSFW)*" if command.nsfw else ""

        command_lines = f"{command.mention} *({type_})* {nsfw}\n{command.description}\n\n"

        if current_embed is None:
            title = (
                f"{category} Commands Overview"
                if not embeds
                else f"{category} Commands Overview (continued)"
            )
            current_embed = _create_base_embed(title, color, thumbnail_url)
            current_field_chars = 0

        if current_field_chars + len(command_lines) <= max_field_chars:
            current_field += command_lines
            current_field_chars += len(command_lines)
        else:
            current_embed.add_field(name="\u200b", value=current_field, inline=False)
            current_field = command_lines
            current_field_chars = len(command_lines)

        if len(current_embed.fields) >= max_fields:
            embeds.append(current_embed)
            current_embed = None

    if current_embed is not None:
        current_embed.add_field(name="\u200b", value=current_field, inline=False)
        embeds.append(current_embed)

    return embeds