Utilities

This section documents all functions under disnake.utils.* that are part of the public API.

Functions

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

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

member = disnake.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 (collections.abc.Iterable) – The iterable to search through.

disnake.utils.get(iterable, **attrs)[source]

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 = disnake.utils.get(message.guild.members, name='Foo')

Multiple attribute matching:

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

Nested attribute matching:

channel = disnake.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.

disnake.utils.snowflake_time(id)[source]
Parameters:

id (int) – The snowflake ID.

Returns:

An aware datetime in UTC representing the creation time of the snowflake.

Return type:

datetime.datetime

disnake.utils.oauth_url(client_id, *, permissions=..., guild=..., redirect_uri=..., scopes=..., 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',).

    New in version 1.7.

  • disable_guild_select (bool) –

    Whether to disallow the user from changing the guild dropdown.

    New in version 2.0.

Returns:

The OAuth2 URL for inviting the bot into guilds.

Return type:

str

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

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

disnake.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

disnake.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

disnake.utils.resolve_invite(invite, *, with_params=False)[source]

Resolves an invite from a Invite, URL or code.

Parameters:
  • invite (Union[Invite, str]) – The invite to resolve.

  • with_params (bool) –

    Whether to also return the query parameters of the invite, if it’s a url.

    New in version 2.3.

Returns:

The invite code if with_params is False, otherwise a tuple containing the invite code and the url’s query parameters, if applicable.

Return type:

Union[str, Tuple[str, Dict[str, str]]]

disnake.utils.resolve_template(code)[source]

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 disnake.utils.sleep_until(when, result=None)[source]

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 local time.

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

disnake.utils.utcnow()[source]

A helper function to return an aware UTC datetime representing the current time.

This should be preferred to datetime.datetime.utcnow() since it is an aware datetime, compared to the naive datetime in the standard library.

New in version 2.0.

Returns:

The current aware datetime in UTC.

Return type:

datetime.datetime

disnake.utils.format_dt(dt, /, style='f')[source]

A helper function to format a datetime.datetime, int or float for presentation within Discord.

This allows for a locale-independent way of presenting data using Discord specific Markdown.

Style

Example Output

Description

t

22:57

Short Time

T

22:57:58

Long Time

d

17/05/2016

Short Date

D

17 May 2016

Long Date

f (default)

17 May 2016 22:57

Short Date Time

F

Tuesday, 17 May 2016 22:57

Long Date Time

R

5 years ago

Relative Time

Note that the exact output depends on the user’s locale setting in the client. The example output presented is using the en-GB locale.

New in version 2.0.

Parameters:
  • dt (Union[datetime.datetime, int, float]) – The datetime to format. If this is a naive datetime, it is assumed to be local time.

  • style (str) – The style to format the datetime with. Defaults to f

Returns:

The formatted string.

Return type:

str

disnake.utils.as_chunks(iterator, max_size)[source]

A helper function that collects an iterator into chunks of a given size.

New in version 2.0.

Parameters:

Warning

The last chunk collected may not be as large as max_size.

Returns:

A new iterator which yields chunks of a given size.

Return type:

Union[Iterator, AsyncIterator]

for ... in disnake.utils.search_directory(path)[source]

Walk through a directory and yield all modules.

Parameters:

path (str) – The path to search for modules

Yields:

str – The name of the found module. (usable in load_extension)

disnake.utils.as_valid_locale(locale)[source]

Converts the provided locale name to a name that is valid for use with the API, for example by returning en-US for en_US. Returns None for invalid names.

New in version 2.5.

Parameters:

locale (str) – The input locale name.