Message Models¶
Message object¶
Message objects are used whenever your bot responds to an Interaction or sends or edits a followup message. They can contain content, embeds, files, and other parameters depending on the situation.
In most cases, you can return a string and it will be converted into a
Message
object:
@discord.command()
def just_content(ctx):
"Just normal string content"
return "Just return a string to send it as a message"
@discord.command()
def markdown(ctx):
"Fancy markdown tricks"
return "All *the* **typical** ~~discord~~ _markdown_ `works` ***too.***"
Alternatively, you can create a Message object yourself:
from flask_discord_interactions import Message
@discord.command()
def message_object(ctx):
"Just normal string content"
return Message("Return a Message object with the content")
You can also pass more options to the Message object, such as making the response ephemeral (this is useful for showing error messages, for example):
@discord.command()
def ephemeral(ctx):
"Ephemeral Message"
return Message(
"Ephemeral messages are only sent to the user who ran the command, "
"and they go away after a short while.\n\n"
"Note that they cannot include files, "
"but Markdown is *perfectly fine.*",
ephemeral=True
)
Note that this only works with the initial response, not for followup messages.
You can also attach files:
@discord.command()
def followup(ctx):
def do_followup():
print("Followup task started")
time.sleep(5)
print("Sending a file")
ctx.send(Message(
content="Sending a file",
file=("README.md", open("README.md", "rb"), "text/markdown")))
thread = threading.Thread(target=do_followup)
thread.start()
return "Sending an original message"
Pass files as a tuple, containing:
The filename
A file object (typically the result of a call to
open(filename, "rb")
, but you could use something likeio.BytesIO
too)Optionally, the content-type of the file
See the files
parameter of requests.request()
for details.
The allowed_mentions
parameter can be used to restrict which users and
roles the message should mention. It is a good idea to use this whenever your
bot is echoing user input. You can read more about this parameter on the
Discord API docs.
Full API¶
- class flask_discord_interactions.Message(**kwargs)¶
Represents a Message, often a response to a Discord interaction (incoming webhook) or a followup message (outgoing webhook).
- embed¶
The embed object (dict) accompanying the message.
- Type:
- embeds¶
An array of embed objects. Specify just one of
embed
orembeds
.- Type:
List[Union[flask_discord_interactions.models.embed.Embed, dict]]
- allowed_mentions¶
An object representing which roles and users the message is allowed to mention. See the Discord API docs for details.
- Type:
- deferred¶
Whether the message should be deferred (display a loading state to the user temporarily). Only valid for incoming webhooks.
- Type:
- ephemeral¶
Whether the message should be ephemeral (only displayed temporarily to only the user who used the command). Only valid for incoming webhooks.
- Type:
- update¶
Whether to update the initial message. Only valid for Message Component interactions / custom ID handlers.
- Type:
- file¶
The file to attach to the message. Only valid for outgoing webhooks. Pass a 2-tuple containing the file name and a file object, e.g.
("README.md", open("README.md", "rb"))
See the
files
parameter ofrequests.request()
for details.- Type:
- files¶
A list of files to attach to the message. Specify just one of
file
orfiles
. Only valid for outgoing webhooks.- Type:
List[tuple]
- dump_components()¶
Returns the message components as a list of dicts.
- Returns:
A list of dicts representing the components.
- Return type:
List[dict]
- dump_embeds()¶
Returns the embeds of this Message as a list of dicts.
- Returns:
A list of dicts representing the embeds.
- Return type:
List[dict]
- encode(followup=False)¶
Return this
Message
as a string/mimetype pair for sending to Discord.If the message contains no files, the string will be a serialized JSON object and the mimetype will be
application/json
.If the message contains attachments, the string will be a multipart encoded response and the mimetype will be
multipart/form-data
.- Parameters:
followup (bool) – Whether this is a followup message.
- Returns:
bytes – Bytes containing the message data (either JSON or multipart).
string – The mimetype of the message data.
- property flags¶
The flags sent with this Message, determined by whether it is ephemeral.
- Returns:
Integer representation of the flags.
- Return type:
- classmethod from_return_value(result)¶
Convert a function return value into a Message object. Converts
None
to an empty Message, or any other type tostr
as message content.- Parameters:
result – The function return value to convert into a
Message
object.- Returns:
A
Message
object representing the return value.- Return type:
Embeds¶
Embed objects let you represent Embeds which you can return as part of messages.
from flask_discord_interactions import Embed, embed
@discord.command()
def my_embed(ctx):
"Embeds!"
return Message(embed=Embed(
title="Embeds!",
description="Embeds can be specified as Embed objects.",
fields=[
embed.Field(
name="Can they use markdown?",
value="**Yes!** [link](https://google.com/)"
),
embed.Field(
name="Where do I learn about how to format this object?",
value=("[Try this visualizer!]"
"(https://leovoel.github.io/embed-visualizer/)")
)
]
))
The Embed
class represents a single Embed. You can also use
embed.Field
, embed.Author
, embed.Footer
, or
embed.Provider
for those fields. embed.Media
can be used
for images, videos, and thumbnails.
Full API¶
- class flask_discord_interactions.Embed(**kwargs)¶
Represents an Embed to be sent as part of a Message.
A
Footer
representing the footer of the embed.
- image¶
A
Media
representing the image of the embed.
- thumbnail¶
A
Media
representing the thumbnail of the embed.
- video¶
A
Media
representing the video of the embed.
- provider¶
A
Provider
representing the name and URL of the provider of the embed.
- author¶
A
Author
representing the author of the embed.
- fields¶
A list of
Field
objects representing the fields of the embed.- Type:
- class flask_discord_interactions.embed.Field(name: str, value: str, inline: bool = False)¶
Represents a field on an Embed.
- class flask_discord_interactions.embed.Author(name: Optional[str] = None, url: Optional[str] = None, icon_url: Optional[str] = None, proxy_icon_url: Optional[str] = None)¶
Represents an author of an embed.
Represents the footer of an Embed.
- class flask_discord_interactions.embed.Provider(name: Optional[str] = None, url: Optional[str] = None)¶
Represents a provider of an Embed.