Utility Functions

discord.utils.find(predicate, seq)[source]

A helper to return the first element found in the sequence that meets the predicate. For example:

member = discord.utils.find(lambda m: m.name == "Mighty", channel.guild.members)

would find the first Member whose name is ‘Mighty’ and return it. If an entry is not found, then None is returned.

This is different from filter() due to the fact it stops the moment it finds a valid entry.

Parameters:
Return type:

Optional[TypeVar(T)]

discord.utils.oauth_url(client_id, *, permissions=Undefined.MISSING, guild=Undefined.MISSING, redirect_uri=Undefined.MISSING, scopes=Undefined.MISSING, disable_guild_select=False)[source]

A helper function that returns the OAuth2 URL for inviting the bot into guilds.

Parameters:
  • client_id (Union[int, str]) – The client ID for your bot.

  • permissions (Permissions) – The permissions you’re requesting. If not given then you won’t be requesting any permissions.

  • guild (Snowflake) – The guild to pre-select in the authorization screen, if available.

  • redirect_uri (str) – An optional valid redirect URI.

  • scopes (Iterable[str]) –

    An optional valid list of scopes. Defaults to ('bot',).

    Added in version 1.7.

  • disable_guild_select (bool) –

    Whether to disallow the user from changing the guild dropdown.

    Added in version 2.0.

Returns:

The OAuth2 URL for inviting the bot into guilds.

Return type:

str

discord.utils.remove_markdown(text, *, ignore_links=True)[source]

A helper function that removes markdown characters.

Added in version 1.7.

Note

This function is not markdown aware and may remove meaning from the original text. For example, if the input contains 10 * 5 then it will be converted into 10  5.

Parameters:
  • text (str) – The text to remove markdown from.

  • ignore_links (bool) – Whether to leave links alone when removing markdown. For example, if a URL in the text contains characters such as _ then it will be left alone. Defaults to True.

Returns:

The text with the markdown special characters removed.

Return type:

str

discord.utils.escape_markdown(text, *, as_needed=False, ignore_links=True)[source]

A helper function that escapes Discord’s markdown.

Parameters:
  • text (str) – The text to escape markdown from.

  • as_needed (bool) – Whether to escape the markdown characters as needed. This means that it does not escape extraneous characters if it’s not necessary, e.g. **hello** is escaped into \*\*hello** instead of \*\*hello\*\*. Note however that this can open you up to some clever syntax abuse. Defaults to False.

  • ignore_links (bool) – Whether to leave links alone when escaping markdown. For example, if a URL in the text contains characters such as _ then it will be left alone. This option is not supported with as_needed. Defaults to True.

Returns:

The text with the markdown special characters escaped with a slash.

Return type:

str

discord.utils.escape_mentions(text)[source]

A helper function that escapes everyone, here, role, and user mentions.

Note

This does not include channel mentions.

Note

For more granular control over what mentions should be escaped within messages, refer to the AllowedMentions class.

Parameters:

text (str) – The text to escape mentions from.

Returns:

The text with the mentions removed.

Return type:

str

discord.utils.raw_mentions(text)[source]

Returns a list of user IDs matching <@user_id> in the string.

Added in version 2.2.

Parameters:

text (str) – The string to get user mentions from.

Returns:

A list of user IDs found in the string.

Return type:

List[int]

discord.utils.raw_channel_mentions(text)[source]

Returns a list of channel IDs matching <@#channel_id> in the string.

Added in version 2.2.

Parameters:

text (str) – The string to get channel mentions from.

Returns:

A list of channel IDs found in the string.

Return type:

List[int]

discord.utils.raw_role_mentions(text)[source]

Returns a list of role IDs matching <@&role_id> in the string.

Added in version 2.2.

Parameters:

text (str) – The string to get role mentions from.

Returns:

A list of role IDs found in the string.

Return type:

List[int]

discord.utils.basic_autocomplete(values, *, filter=None)[source]

A helper function to make a basic autocomplete for slash commands. This is a pretty standard autocomplete and will return any options that start with the value from the user, case-insensitive. If the values parameter is callable, it will be called with the AutocompleteContext.

This is meant to be passed into the discord.Option.autocomplete attribute.

Parameters:
  • values (Union[Union[Iterable[OptionChoice], Iterable[str], Iterable[int], Iterable[float]], Callable[[AutocompleteContext], Union[Union[Iterable[str], Iterable[int], Iterable[float]], Awaitable[Union[Iterable[str], Iterable[int], Iterable[float]]]]], Awaitable[Union[Iterable[str], Iterable[int], Iterable[float]]]]) – Possible values for the option. Accepts an iterable of str, a callable (sync or async) that takes a single argument of AutocompleteContext, or a coroutine. Must resolve to an iterable of str.

  • filter (Optional[Callable[[AutocompleteContext, Any], Union[bool, Awaitable[bool]]]]) –

    An optional callable (sync or async) used to filter the autocomplete options. It accepts two arguments: the AutocompleteContext and an item from values iteration treated as callback parameters. If None is provided, a default filter is used that includes items whose string representation starts with the user’s input value, case-insensitive.

    Added in version 2.7.

Returns:

A wrapped callback for the autocomplete.

Return type:

Callable[[AutocompleteContext], Awaitable[Union[Iterable[OptionChoice], Iterable[str], Iterable[int], Iterable[float]]]]

Examples

Basic usage:

Option(str, "color", autocomplete=basic_autocomplete(("red", "green", "blue")))

# or

async def autocomplete(ctx):
    return "foo", "bar", "baz", ctx.interaction.user.name


Option(str, "name", autocomplete=basic_autocomplete(autocomplete))

With filter parameter:

Option(
    str,
    "color",
    autocomplete=basic_autocomplete(("red", "green", "blue"), filter=lambda c, i: str(c.value or "") in i),
)

Added in version 2.0.

Note

Autocomplete cannot be used for options that have specified choices.