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 like io.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).

content

The message body/content.

Type:

str

tts

Whether the message should be sent with text-to-speech.

Type:

bool

embed

The embed object (dict) accompanying the message.

Type:

Union[flask_discord_interactions.models.embed.Embed, dict]

embeds

An array of embed objects. Specify just one of embed or embeds.

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:

dict

deferred

Whether the message should be deferred (display a loading state to the user temporarily). Only valid for incoming webhooks.

Type:

bool

ephemeral

Whether the message should be ephemeral (only displayed temporarily to only the user who used the command). Only valid for incoming webhooks.

Type:

bool

update

Whether to update the initial message. Only valid for Message Component interactions / custom ID handlers.

Type:

bool

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 of requests.request() for details.

Type:

tuple

files

A list of files to attach to the message. Specify just one of file or files. Only valid for outgoing webhooks.

Type:

List[tuple]

components

An array of Component objects representing message components.

Type:

List[flask_discord_interactions.models.component.Component]

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:

int

classmethod from_return_value(result)

Convert a function return value into a Message object. Converts None to an empty Message, or any other type to str 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:

Message


class flask_discord_interactions.ResponseType

Represents the different response type integers.

PONG = 1
CHANNEL_MESSAGE_WITH_SOURCE = 4
DEFERRED_CHANNEL_MESSAGE_WITH_SOURCE = 5
DEFERRED_UPDATE_MESSAGE = 6
UPDATE_MESSAGE = 7
APPLICATION_COMMAND_AUTOCOMPLETE_RESULT = 8
MODAL = 9

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.

title

The title of the embed.

Type:

str

description

The description in the embed.

Type:

str

url

The URL that the embed title links to.

Type:

str

timestamp

An ISO8601 timestamp included in the embed.

Type:

str

color

An integer representing the color of the sidebar of the embed.

Type:

int

footer

A Footer representing the footer of the embed.

Type:

flask_discord_interactions.models.embed.Footer

image

A Media representing the image of the embed.

Type:

flask_discord_interactions.models.embed.Media

thumbnail

A Media representing the thumbnail of the embed.

Type:

flask_discord_interactions.models.embed.Media

video

A Media representing the video of the embed.

Type:

flask_discord_interactions.models.embed.Media

provider

A Provider representing the name and URL of the provider of the embed.

Type:

flask_discord_interactions.models.embed.Provider

author

A Author representing the author of the embed.

Type:

flask_discord_interactions.models.embed.Author

fields

A list of Field objects representing the fields of the embed.

Type:

List[flask_discord_interactions.models.embed.Field]

dump()

Returns this Embed as a dictionary, removing fields which are None.

Returns:

A dictionary representation of this Embed.

Return type:

dict


class flask_discord_interactions.embed.Field(name: str, value: str, inline: bool = False)

Represents a field on an Embed.

inline: bool = False
name: str
value: str

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.

icon_url: str = None
name: str = None
proxy_icon_url: str = None
url: str = None

class flask_discord_interactions.embed.Footer(text: str, icon_url: Optional[str] = None, proxy_icon_url: Optional[str] = None)

Represents the footer of an Embed.

icon_url: str = None
proxy_icon_url: str = None
text: str

class flask_discord_interactions.embed.Provider(name: Optional[str] = None, url: Optional[str] = None)

Represents a provider of an Embed.

name: str = None
url: str = None

class flask_discord_interactions.embed.Media(url: Optional[str] = None, proxy_url: Optional[str] = None, height: Optional[int] = None, width: Optional[int] = None)

Represents a thumbnail, image, or video on an Embed.

height: int = None
proxy_url: str = None
url: str = None
width: int = None