Miscellaneous¶
This section documents everything that doesn’t fit into any other section, but isn’t big enough to have its own section.
Classes¶
AsyncIterator¶
- class disnake.AsyncIterator¶
Represents the “AsyncIterator” concept. Note that no such class exists, it is purely abstract.
- async for x in y
Iterates over the contents of the async iterator.
- await next()¶
This function is a coroutine.
Advances the iterator by one, if possible. If no more items are found then this raises
NoMoreItems
.
- await get(**attrs)¶
This function is a coroutine.
Similar to
utils.get()
except run over the async iterator.Getting the last message by a user named ‘Dave’ or
None
:msg = await channel.history().get(author__name='Dave')
- await find(predicate)¶
This function is a coroutine.
Similar to
utils.find()
except run over the async iterator.Unlike
utils.find()
, the predicate provided can be a coroutine.Getting the last audit log with a reason or
None
:def predicate(event): return event.reason is not None event = await guild.audit_logs().find(predicate)
- Parameters:
predicate – The predicate to use. Could be a coroutine.
- Returns:
The first element that returns
True
for the predicate orNone
.
- await flatten()¶
This function is a coroutine.
Flattens the async iterator into a
list
with all the elements.- Returns:
A list of every element in the async iterator.
- Return type:
- chunk(max_size)¶
Collects items into chunks of up to a given maximum size. Another
AsyncIterator
is returned which collects items intolist
s of a given size. The maximum chunk size must be a positive integer.New in version 1.6.
Collecting groups of users:
async for leader, *users in reaction.users().chunk(3): ...
Warning
The last chunk collected may not be as large as
max_size
.- Parameters:
max_size – The size of individual chunks.
- Return type:
- map(func)¶
This is similar to the built-in
map
function. AnotherAsyncIterator
is returned that executes the function on every element it is iterating over. This function can either be a regular function or a coroutine.Creating a content iterator:
def transform(message): return message.content async for content in channel.history().map(transform): message_length = len(content)
- Parameters:
func – The function to call on every element. Could be a coroutine.
- Return type:
- filter(predicate)¶
This is similar to the built-in
filter
function. AnotherAsyncIterator
is returned that filters over the original async iterator. This predicate can be a regular function or a coroutine.Getting messages by non-bot accounts:
def predicate(message): return not message.author.bot async for elem in channel.history().filter(predicate): ...
- Parameters:
predicate – The predicate to call on every element. Could be a coroutine.
- Return type:
Discord Models¶
Asset¶
- defis_animated
- asyncread
- defreplace
- asyncsave
- asyncto_file
- defwith_format
- defwith_size
- defwith_static_format
- class disnake.Asset[source]¶
Represents a CDN asset on Discord.
- str(x)
Returns the URL of the CDN asset.
- len(x)
Returns the length of the CDN asset’s URL.
- x == y
Checks if the asset is equal to another asset.
- x != y
Checks if the asset is not equal to another asset.
- hash(x)
Returns the hash of the asset.
- replace(*, size=..., format=..., static_format=...)[source]¶
Returns a new asset with the passed components replaced.
Changed in version 2.6: Raises
ValueError
instead ofInvalidArgument
.- Parameters:
- Raises:
ValueError – An invalid size or format was passed.
- Returns:
The newly updated asset.
- Return type:
- with_size(size, /)[source]¶
Returns a new asset with the specified size.
Changed in version 2.6: Raises
ValueError
instead ofInvalidArgument
.- Parameters:
size (
int
) – The new size of the asset.- Raises:
ValueError – The asset had an invalid size.
- Returns:
The newly updated asset.
- Return type:
- with_format(format, /)[source]¶
Returns a new asset with the specified format.
Changed in version 2.6: Raises
ValueError
instead ofInvalidArgument
.- Parameters:
format (
str
) – The new format of the asset.- Raises:
ValueError – The asset had an invalid format.
- Returns:
The newly updated asset.
- Return type:
- with_static_format(format, /)[source]¶
Returns a new asset with the specified static format.
This only changes the format if the underlying asset is not animated. Otherwise, the asset is not changed.
Changed in version 2.6: Raises
ValueError
instead ofInvalidArgument
.- Parameters:
format (
str
) – The new static format of the asset.- Raises:
ValueError – The asset had an invalid format.
- Returns:
The newly updated asset.
- Return type:
- await read()[source]¶
This function is a coroutine.
Retrieves the content of this asset as a
bytes
object.- Raises:
DiscordException – There was no internal connection state.
HTTPException – Downloading the asset failed.
NotFound – The asset was deleted.
- Returns:
The content of the asset.
- Return type:
- await save(fp, *, seek_begin=True)[source]¶
This function is a coroutine.
Saves this asset into a file-like object.
- Parameters:
fp (Union[
io.BufferedIOBase
,os.PathLike
]) – The file-like object to save this asset to or the filename to use. If a filename is passed then a file is created with that filename and used instead.seek_begin (
bool
) – Whether to seek to the beginning of the file after saving is successfully done.
- Raises:
DiscordException – There was no internal connection state.
HTTPException – Downloading the asset failed.
NotFound – The asset was deleted.
- Returns:
The number of bytes written.
- Return type:
- await to_file(*, spoiler=False, filename=None, description=None)[source]¶
This function is a coroutine.
Converts the asset into a
File
suitable for sending viaabc.Messageable.send()
.New in version 2.5.
Changed in version 2.6: Raises
TypeError
instead ofInvalidArgument
.- Parameters:
- Raises:
DiscordException – The asset does not have an associated state.
HTTPException – Downloading the asset failed.
NotFound – The asset was deleted.
TypeError – The asset is a unicode emoji or a sticker with lottie type.
- Returns:
The asset as a file suitable for sending.
- Return type:
Data Classes¶
Object¶
- class disnake.Object(id)[source]¶
Represents a generic Discord object.
The purpose of this class is to allow you to create ‘miniature’ versions of data classes if you want to pass in just an ID. Most functions that take in a specific data class with an ID can also take in this class as a substitute instead. Note that even though this is the case, not all objects (if any) actually inherit from this class.
There are also some cases where some websocket events are received in strange order and when such events happened you would receive this class rather than the actual data class. These cases are extremely rare.
- x == y
Checks if two objects are equal.
- x != y
Checks if two objects are not equal.
- hash(x)
Returns the object’s hash.
Colour¶
- clsColour.blue
- clsColour.blurple
- clsColour.brand_green
- clsColour.brand_red
- clsColour.dark_blue
- clsColour.dark_embed
- clsColour.dark_gold
- clsColour.dark_gray
- clsColour.dark_green
- clsColour.dark_grey
- clsColour.dark_magenta
- clsColour.dark_orange
- clsColour.dark_purple
- clsColour.dark_red
- clsColour.dark_teal
- clsColour.dark_theme
- clsColour.darker_gray
- clsColour.darker_grey
- clsColour.default
- clsColour.from_hsv
- clsColour.from_rgb
- clsColour.fuchsia
- clsColour.gold
- clsColour.green
- clsColour.greyple
- clsColour.light_embed
- clsColour.light_gray
- clsColour.light_grey
- clsColour.lighter_gray
- clsColour.lighter_grey
- clsColour.magenta
- clsColour.og_blurple
- clsColour.old_blurple
- clsColour.orange
- clsColour.purple
- clsColour.random
- clsColour.red
- clsColour.teal
- clsColour.yellow
- defto_rgb
- class disnake.Colour(value)[source]¶
Represents a Discord role colour. This class is similar to a (red, green, blue)
tuple
.There is an alias for this called Color.
- x == y
Checks if two colours are equal.
- x != y
Checks if two colours are not equal.
- hash(x)
Return the colour’s hash.
- str(x)
Returns the hex format for the colour.
- int(x)
Returns the raw colour value.
- classmethod random(*, seed=None)[source]¶
A factory method that returns a
Colour
with a random hue.Note
The random algorithm works by choosing a colour with a random hue but with maxed out saturation and value.
New in version 1.6.
- classmethod brand_green()[source]¶
A factory method that returns a
Colour
with a value of0x57F287
.New in version 2.0.
- classmethod dark_magenta()[source]¶
A factory method that returns a
Colour
with a value of0xad1457
.
- classmethod brand_red()[source]¶
A factory method that returns a
Colour
with a value of0xED4245
.New in version 2.0.
- classmethod lighter_grey()[source]¶
A factory method that returns a
Colour
with a value of0x95a5a6
.
- classmethod lighter_gray()[source]¶
A factory method that returns a
Colour
with a value of0x95a5a6
.
- classmethod dark_theme()[source]¶
A factory method that returns a
Colour
with a value of0x313338
. This will appear transparent on Discord’s dark theme.New in version 1.5.
- classmethod fuchsia()[source]¶
A factory method that returns a
Colour
with a value of0xEB459E
.New in version 2.0.
- classmethod yellow()[source]¶
A factory method that returns a
Colour
with a value of0xFEE75C
.New in version 2.0.
Module Attributes¶
Version Info¶
There are two main ways to query version information about the library. For guarantees, check Version Guarantees.
- disnake.version_info¶
A named tuple that is similar to
sys.version_info
.Just like
sys.version_info
the valid values forreleaselevel
are ‘alpha’, ‘beta’, ‘candidate’ and ‘final’.