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
Memberwhose name is ‘Mighty’ and return it. If an entry is not found, thenNoneis returned.This is different from
filter()due to the fact it stops the moment it finds a valid entry.
- 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:
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:
- 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 * 5then it will be converted into10 5.- Parameters:
- Returns:
The text with the markdown special characters removed.
- Return type:
- 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 toFalse.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 withas_needed. Defaults toTrue.
- Returns:
The text with the markdown special characters escaped with a slash.
- Return type:
- 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
AllowedMentionsclass.
- discord.utils.raw_mentions(text)[source]¶
Returns a list of user IDs matching
<@user_id>in the string.Added in version 2.2.
- discord.utils.raw_channel_mentions(text)[source]¶
Returns a list of channel IDs matching
<@#channel_id>in the string.Added in version 2.2.
- discord.utils.raw_role_mentions(text)[source]¶
Returns a list of role IDs matching
<@&role_id>in the string.Added in version 2.2.
- 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
valuesparameter is callable, it will be called with the AutocompleteContext.This is meant to be passed into the
discord.Option.autocompleteattribute.- 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 ofstr, a callable (sync or async) that takes a single argument ofAutocompleteContext, or a coroutine. Must resolve to an iterable ofstr.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
AutocompleteContextand an item fromvaluesiteration treated as callback parameters. IfNoneis 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.