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, 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 (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 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 = 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:
- 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',)
.Added in version 1.7.
- Returns:
The OAuth2 URL for inviting the bot into guilds.
- Return type:
- discord.utils.remove_markdown(text, *, ignore_links=True)
A helper function that removes markdown characters.
Added 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:
- 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 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:
- 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.
- discord.utils.resolve_invite(invite)
Resolves an invite from a
Invite
, URL or code.
- discord.utils.resolve_template(code)
Resolves a template code from a
Template
, URL or code.Added in version 1.4.
- 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.
Added 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:
timestamp (Union[
datetime.datetime
,int
]) – The timestamp; Adatetime.datetime
object or a unix timestamp as anint
.style (Optional[Union[
TimestampStyle
,str
]]) –How the timestamp should be displayed in Discord; this can either be a
TimestampStyle
or directly the associated value.- default:
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 ofTimestampStyle
- Returns:
The formatted timestamp.
- Return type: