Utility Functions

discord.utils.find(predicate, seq)

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:
  • predicate – A function that returns a boolean-like result.

  • seq (iterable) – The iterable to search through.

discord.utils.get(iterable, **attrs)

A helper that returns the first element in the iterable that meets all the traits passed in attrs. This is an alternative for find().

When multiple attributes are specified, they are checked using logical AND, not logical OR. Meaning they have to meet every attribute passed in and not one of them.

To have a nested attribute search (i.e. search by x.y) then pass in x__y as the keyword argument.

If nothing is found that matches the attributes passed, then None is returned.

Examples

Basic usage:

member = discord.utils.get(message.guild.members, name='Foo')

Multiple attribute matching:

channel = discord.utils.get(guild.voice_channels, name='Foo', bitrate=64000)

Nested attribute matching:

channel = discord.utils.get(client.get_all_channels(), guild__name='Cool', name='general')
Parameters:
  • iterable – An iterable to search through.

  • **attrs – Keyword arguments that denote attributes to search with.

discord.utils.snowflake_time(id)
Parameters:

id (int) – The snowflake ID.

Returns:

The creation date in UTC of a Discord snowflake ID.

Return type:

datetime.datetime

discord.utils.oauth_url(client_id, permissions=None, guild=None, redirect_uri=None, scopes=('bot',))

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

Parameters:
  • client_id (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 (Guild) – 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',).

    New in version 1.7.

Returns:

The OAuth2 URL for inviting the bot into guilds.

Return type:

str

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

A helper function that removes markdown characters.

New 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)

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)

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.resolve_invite(invite)

Resolves an invite from a Invite, URL or code.

Parameters:

invite (Union[Invite, str]) – The invite.

Returns:

The invite code.

Return type:

str

discord.utils.resolve_template(code)

Resolves a template code from a Template, URL or code.

New in version 1.4.

Parameters:

code (Union[Template, str]) – The code.

Returns:

The template code.

Return type:

str

await discord.utils.sleep_until(when, result=None)

This function is a coroutine.

Sleep until a specified time.

If the time supplied is in the past this function will yield instantly.

New in version 1.3.

Parameters:
  • when (datetime.datetime) – The timestamp in which to sleep until. If the datetime is naive then it is assumed to be in UTC.

  • result (Any) – If provided is returned to the caller when the coroutine completes.

discord.utils.styled_timestamp(timestamp, style=('short', 'f'))

A small function that returns a styled timestamp for discord, this will be displayed accordingly in the Discord client depending on the style specified.

Timestamps will display the given timestamp in the user’s timezone and locale.

Parameters:

Examples

# Normal timestamp
@client.command()
async def time(ctx):
    await ctx.send(discord.utils.styled_timestamp(discord.utils.utcnow(), discord.TimestampStyle.long))

# Relative timestamp
@client.command()
async def countdown(ctx, seconds: int):
    happens_in = discord.utils.utcnow() + datetime.timedelta(seconds=seconds)
    await ctx.send(f'Happens {discord.utils.styled_timestamp(happens_in, discord.TimestampStyle.relative)}')
Raises:

AttributeError – If the style is not a valid member of TimestampStyle

Returns:

The formatted timestamp.

Return type:

str

discord.utils.SupportsStr

The SupportsStr object is a special annotation that means that this can any object that is already a str or has a __str__() method.