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, thenNone
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 forfind()
.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 inx__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:
- disnake.utils.oauth_url(client_id, *, permissions=..., guild=..., redirect_uri=..., scopes=..., disable_guild_select=False, integration_type=...)[source]¶
A helper function that returns the OAuth2 URL for authorizing the application.
- 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',)
.New in version 1.7.
disable_guild_select (
bool
) –Whether to disallow the user from changing the guild dropdown.
New in version 2.0.
integration_type (
int
) –An optional integration type/installation type to install the application with.
New in version 2.10.
- Returns:
The OAuth2 URL for inviting the bot into guilds.
- Return type:
- 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 into10 5
.- Parameters:
- Returns:
The text with the markdown special characters removed.
- Return type:
- 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 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:
- 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.
- disnake.utils.resolve_invite(invite, *, with_params=False)[source]¶
Resolves an invite from a
Invite
, URL or code.- Parameters:
- Returns:
The invite code if
with_params
isFalse
, otherwise a tuple containing the invite code and the url’s query parameters, if applicable.- Return type:
- disnake.utils.resolve_template(code)[source]¶
Resolves a template code from a
Template
, URL or code.New in version 1.4.
- 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:
- disnake.utils.format_dt(dt, /, style='f')[source]¶
A helper function to format a
datetime.datetime
,int
orfloat
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 tof
- Returns:
The formatted string.
- Return type:
- 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:
iterator (Union[
collections.abc.Iterator
,collections.abc.AsyncIterator
]) – The iterator to chunk, can be sync or async.max_size (
int
) – The maximum chunk size.
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
]