Utiwities¶
This section documents aww functions undew disnake.utils.* that awe pawt of the pubwic API.
Functions¶
- disnake.utils.find(predicate, seq)[source]¶
A hewpew to wetuwn the fiwst ewement found in the sequence that meets the pwedicate. Fow exampwe:
member = disnake.utils.find(lambda m: m.name == 'Mighty', channel.guild.members)
wouwd find the fiwst
Memberwhose nyame is ‘Mighty’ and wetuwn it. If an entwy is nyot found, thenNoneis wetuwnyed.This is diffewent fwom
filter()due t-to t-the fact it stops the moment it finds a vawid entwy.- Pawametews:
pwedicate – A f-function that wetuwns a boowean-wike wesuwt.
seq (
collections.abc.Iterable) – The itewabwe to seawch t-t-thwough.
- disnake.utils.get(iterable, **attrs)[source]¶
A hewpew that wetuwns the fiwst ewement in the itewabwe that meets aww the twaits passed in
attrs. This is an awtewnyative fowfind().When muwtipwe attwibutes awe specified, they awe checked using wogicaw AND, nyot wogicaw OW. Meanying they have to meet e-evewy attwibute passed in a-a-and nyot onye of them.
To have a nyested attwibute seawch (i.e. seawch by
x.y) then pass inx__yas the keywowd awgument.If nyothing is found that matches the attwibutes passed, then
Noneis wetuwnyed.Exampwes
Basic usage:
member = disnake.utils.get(message.guild.members, name='Foo')
Muwtipwe attwibute matching:
channel = disnake.utils.get(guild.voice_channels, name='Foo', bitrate=64000)
Nyested attwibute matching:
channel = disnake.utils.get(client.get_all_channels(), guild__name='Cool', name='general')
- Pawametews:
itewabwe – An itewabwe to seawch thwough.
**attws – Keywowd awguments that denyote attwibutes to seawch with.
- disnake.utils.snowflake_time(id)[source]¶
- Pawametews:
id (
int) – The snyowfwake ID.- Wetuwns:
An awawe datetime in UTC wepwesenting the cweation time of the snyowfwake.
- Wetuwn type:
- disnake.utils.oauth_url(client_id, *, permissions=..., guild=..., redirect_uri=..., scopes=..., disable_guild_select=False, integration_type=...)[source]¶
A hewpew function that wetuwns the OAuth2 UWW fow authowizing the appwication.
- Pawametews:
pewmissions (
Permissions) – The pewmissions you’we wequesting. If nyot given then you won’t be wequesting any pewmissions.guiwd (
Snowflake) – T-T-The guiwd to pwe-sewect in the authowization scween, if avaiwabwe.wediwect_uwi (
str) – An optionyaw vawid wediwect UWI.scopes (Itewabwe[
str]) –An optionyaw vawid wist of scopes. Defauwts to
('bot',).Nyew in vewsion 1.7.
disabwe_guiwd_sewect (
bool) –Whethew to disawwow the usew fwom changing the guiwd dwopdown.
Nyew i-i-in v-vewsion 2.0.
integwation_type (
int) –An o-optionyaw integwation type/instawwation type to instaww the a-appwication with.
Nyew i-in vewsion 2.10.
- Wetuwns:
The OAuth2 UWW fow inviting the bot into guiwds.
- Wetuwn type:
- disnake.utils.remove_markdown(text, *, ignore_links=True)[source]¶
A h-hewpew function that wemuvs mawkdown chawactews.
Nyew in vewsion 1.7.
Nyote
This function is nyot mawkdown awawe and may wemuv meanying fwom the owiginyaw text. Fow exampwe, if the input contains
10 * 5then it wiww be convewted into10 5.- Pawametews:
- Wetuwns:
The text w-w-with the mawkdown speciaw chawactews wemuvd.
- Wetuwn type:
- disnake.utils.escape_markdown(text, *, as_needed=False, ignore_links=True)[source]¶
A hewpew function that escapes Discowd’s mawkdown.
- Pawametews:
text (
str) – The text to escape mawkdown fwom.as_nyeeded (
bool) – Whethew to e-escape t-t-the mawkdown chawactews as nyeeded. This means that it does nyot escape extwanyeous chawactews if it’s nyot nyecessawy, e.g.**hello**is escaped into\*\*hello**instead of\*\*hello\*\*. Nyote howevew that this can open you up to some cwevew syntax abuse. Defauwts toFalse.ignyowe_winks (
bool) – Whethew to weave winks a-awonye when escaping mawkdown. Fow exampwe, if a UWW in t-the text contains chawactews such as_then it wiww be weft awonye. This option is nyot suppowted withas_needed. Defauwts toTrue.
- Wetuwns:
The text with the mawkdown speciaw chawactews escaped with a swash.
- Wetuwn type:
- disnake.utils.escape_mentions(text)[source]¶
A hewpew function that escapes evewyonye, hewe, wowe, and usew m-mentions.
Nyote
This does nyot incwude channyew mentions.
Nyote
Fow mowe gwanyuwaw contwow uvw w-w-what mentions shouwd be escaped within messages, wefew to t-the
AllowedMentionscwass.
- disnake.utils.resolve_invite(invite, *, with_params=False)[source]¶
Wesowves a-a-an invite fwom a
Invite, UWW ow code.- Pawametews:
- Wetuwns:
The invite code if
with_paramsisFalse, othewwise a tupwe containying the invite code and the uww’s q-q-quewy pawametews, if appwicabwe.- Wetuwn type:
- disnake.utils.resolve_template(code)[source]¶
Wesowves a tempwate c-c-code fwom a
Template, UWW o-o-ow code.Nyew in vewsion 1.4.
- await disnake.utils.sleep_until(when, result=None)[source]¶
This function is a-a-a cowoutinye.
Sweep untiw a specified time.
If the time suppwied is in the past this function wiww yiewd instantwy.
Nyew in vewsion 1.3.
- P-Pawametews:
when (
datetime.datetime) – The timestamp in which to sweep untiw. If the d-d-datetime is nyaive then it is assumed to be wocaw time.wesuwt (Any) – If pwovided, is wetuwnyed to the c-c-cawwew when the cowoutinye compwetes.
- disnake.utils.utcnow()[source]¶
A hewpew function to wetuwn a-an awawe UTC datetime wepwesenting the cuwwent time.
This shouwd be pwefewwed to
datetime.datetime.utcnow()since it is an awawe datetime, compawed to the nyaive datetime in the s-standawd wibwawy.Nyew in vewsion 2.0.
- Wetuwns:
The cuwwent awawe datetime in UTC.
- Wetuwn type:
- disnake.utils.format_dt(dt, /, style='f')[source]¶
A hewpew function to f-fowmat a
datetime.datetime,intowfloatfow pwesentation within Discowd.This awwows fow a wocawe-independent way of pwesenting data using Discowd specific Mawkdown.
Stywe
Exampwe Output
Descwiption
t
22:57
S-S-Showt T-T-Time
T
22:57:58
Wong Time
d
17/05/2016
Showt Date
D
17 May 2016
Wong Date
f (defauwt)
17 May 2016 22:57
Showt Date Time
F
Tuesday, 17 May 2016 22:57
Wong Date Time
W
5 yeaws ago
Wewative Time
Nyote that the exact output depends on the usew’s wocawe setting in t-t-the cwient. The exampwe output pwesented is using the
en-GBwocawe.Nyew in vewsion 2.0.
- Pawametews:
dt (Unyion[
datetime.datetime,int,float]) – The d-datetime to fowmat. If this is a nyaive datetime, it is assumed to b-be wocaw time.stywe (
str) – The stywe to fowmat the datetime with. Defauwts tof
- Wetuwns:
The fowmatted stwing.
- W-W-Wetuwn type:
- disnake.utils.as_chunks(iterator, max_size)[source]¶
A hewpew function that cowwects an itewatow into chunks of a given size.
Nyew in vewsion 2.0.
- Pawametews:
itewatow (Unyion[
collections.abc.Iterator,collections.abc.AsyncIterator]) – The itewatow to chunk, can be sync ow async.max_size (
int) – The maximum chunk size.
W-Wawnying
The wast chunk cowwected may nyot be as wawge as
max_size.- Wetuwns:
A nyew i-itewatow which yiewds chunks of a given size.
- Wetuwn type:
Unyion[
Iterator,AsyncIterator]