Message

Represents a message from Discord.

Attributes

activity : None, MessageActivity

Sent with rich presence related embeds. Defaults to None.

application : None, MessageApplication

Sent with rich presence related embeds. Defaults to None.

application_id : int

The application's identifier who sent the message. Defaults to 0.

attachments : None, tuple of Attachment

Attachments sent with the message. Defaults to None.

author : UserBase

The author of the message. Can be any user type and if not found, then set as ZEROUSER.

call : None, MessageCall

Call information of the message. Applicable if the message's type is .call. Defaults to None.

channel_id : int

The channel's identifier where the message is sent. Defaults to 0

components : None, tuple of Component

Components attached to the message. Defaults to None.

content : None, str

The message's content. Defaults to None.

edited_at : None, datetime

The time when the message was edited, or None if it was not.

Pinning or (un)suppressing a message will not change it's edited value.

Defaults to None.

embeds : None, tuple of Embed

Embeds included with the message.

If a message contains links then those links' embeds might not be included with the initial payload, but only with a followup one.

Defaults to None.

flags : MessageFlag

The message's flags. Defaults to MessageFlag(0).

guild_id : int

The channel's guild's identifier. Defaults to 0Ë™.

id : int

The unique identifier number of the message. Defaults to 0.

interaction : None, MessageInteraction

Present if the message is a response to an InteractionEvent. Defaults to None.

mentioned_channels_cross_guild : None, tuple of Channel

Cross guild channel mentions of a crosspost message if applicable. Defaults to None.

mentioned_everyone : bool

Whether the message contains @everyone, @here. Defaults to False.

mentioned_role_ids : None, tuple of int

The mentioned roles' identifiers. Defaults to None.

mentioned_users : None, tuple of ClientUserBase

The mentioned users by the message. Defaults to None.

nonce : None, str

A nonce that is used for optimistic message sending. If a message is created with a nonce, then it should be shown up at the message's received payload as well. Defaults to None.

pinned : bool

Whether the message is pinned. Defaults to False.

reactions : None, ReactionMapping

A dictionary like object that contains the reactions on the message. Defaults to None.

If all reactions are removed from a message, .reactions will not default back to None.

referenced_message : None, Message

The referenced message. The message can be partial.

Set when the message is a reply, a crosspost or when is a pin message.

Defaults to None.

resolved : None, Resolved

Resolved entities of selected options.

Defaults to None.

role_subscription : None, MessageRoleSubscription

Additional role subscription information attached to the message. Defaults to None.

stickers : None, tuple of Sticker

The stickers sent with the message. Defaults to None.

thread : None, Channel

The thread that was started from this message. Defaults to None.

tts : bool

Whether the message is "text to speech". Defaults to False.

The type of the message. Defaults to MessageType.default.

_cache_mentioned_channels : None, tuple of Channel

Mentioned channels by the message. Parsed from .content. Defaults to None.

Cache field used by .mentioned_channels.

_state : int

Bitwise mask used to track the message's state.

Notes

Message instances are weakreferable.

The content, embeds, attachments and the components fields are restricted for the message content intent.

Properties

attachment

Returns the first attachment in the message.

Returns

attachment : None, Attachment

channel

Returns the message's channel's guild.

Returns

guild : Channel

channel_mentions

Deprecated and will be removed in 2023 November.

clean_content

Returns the message's clean content, what actually depends on the message's type. By default it is the message's content with transformed mentions, but for different message types it means different things.

Returns

clean_content : None, str

Notes

The converting can not display join messages, call messages and private channel names correctly.

clean_embeds

Returns the message's not link typed embeds with converted content without mentions.

Returns

clean_embeds : list of Embed

Notes

Not changes the original embeds of the message.

contents

A list of all of the contents sent in the message. It means the message's content if it has and the content of the message's embeds.

Returns

contents : list of str

created_at

When the entity was created.

Returns

created_at : datetime

cross_mentions

Deprecated and will be removed in 2023 November.

deleted

Returns whether the message is deleted.

Defaults to False.

Returns

deleted : bool

embed

Returns the first embed in the message.

Returns

embed : None, Embed

everyone_mention

Deprecated and will be removed in 2023 November.

guild

Returns the message's channel's guild.

Returns

guild : None, Guild

mentioned_channels

The mentioned channels by the message. If there is non returns None.

Returns

mentioned_channels : None, tuple of Channel

mentioned_roles

The mentioned roles by the message. If there is non, returns None.

Returns

role_mentions : None, tuple of Role

mentions

Returns a list of all the mentions sent at the message.

Returns

mentions : list of (str('everyone'), ClientUserBase, Role, Channel)

partial

Returns whether the message is partial.

Returns

partial : bool

role_mention_ids

Deprecated and will be removed in 2023 November.

role_mentions

Deprecated and will be removed in 2023 November.

sticker

Returns the first sticker in the message.

Returns

sticker : None, Sticker

url

Returns a jump url to the message. If the message's channel is a partial guild channel, returns None.

This function is a shared property of Message-s.

Returns

url : str

user_mentions

Returns whether the message has .user_mentions set as its non-default value.

Defaults to None.

Returns

user_mentions : None, tuple of UserBase

Methods

__new__(...)

Creates a new partial message with the given fields.

ParameterTypeOptionalKeyword onlyDescription
activity

None, MessageActivity

Message's activity information, sent with rich presence related embeds.

application

None, MessageApplication

Message's application information, sent with rich presence related embeds.

application_id

None, Application

The application or its identifier who sent the message.

attachments

None, iterable of Attachment

Attachments sent with the message.

author

UserBase

The author of the message.

call

None, MessageCall

Call information of the message.

components

None, iterable of Component

Components attached to the message.

content

None, str

The message's content.

edited_at

None, datetime

The time when the message was edited.

embeds

None, iterable of Embed

Embeds included with the message.

flags

MessageFlag, int

The message's flags.

interaction

None, MessageInteraction

Present if the message is a response to an InteractionEvent.

mentioned_channels_cross_guild

None, iterable of Channel

Cross guild channel mentions of a crosspost message if applicable.

mentioned_everyone

bool

Whether the message contains @everyone, @here.

mentioned_role_ids

None, iterable of (int, Role)

The mentioned roles' identifiers.

mentioned_users

None, iterable of ClientUserBase

The mentioned users by the message.

message_type

MessageType, int

The type of the message.

nonce

None, str

A nonce that is used for optimistic message sending. If a message is created with a nonce, then it should be shown up at the message's received payload as well.

reactions

None, ReactionMapping(or compatible)

A dictionary like object that contains the reactions on the message

referenced_message

None, Message

The referenced message.

resolved

None, Resolved

Resolved entities of selected options.

role_subscription

None, MessageRoleSubscription

Additional role subscription information attached to the message.

pinned

bool

Whether the message is pinned.

stickers

None, iterable of Sticker

The stickers sent with the message.

thread

None, Channel

The thread that was started from this message.

tts

bool

Whether the message is "text to speech".

Raises

TypeError

  • If a parameter's type is incorrect.

ValueError

  • If a parameter's value is incorrect.

copy()

Copies the message returning a new partial one.

Returns

new : instance<type<self>>

copy_with(...)

Copies the message with the given fields.

ParameterTypeOptionalKeyword onlyDescription
activity

None, MessageActivity

Message's activity information, sent with rich presence related embeds.

application

None, MessageApplication

Message's application information, sent with rich presence related embeds.

application_id

None, Application

The application or its identifier who sent the message.

attachments

None, iterable of Attachment

Attachments sent with the message.

author

UserBase

The author of the message.

call

None, MessageCall

Call information of the message.

components

None, iterable of Component

Components attached to the message.

content

None, str

The message's content.

edited_at

None, datetime

The time when the message was edited.

embeds

None, iterable of Embed

Embeds included with the message.

flags

MessageFlag, int

The message's flags.

interaction

None, MessageInteraction

Present if the message is a response to an InteractionEvent.

mentioned_channels_cross_guild

None, iterable of Channel

Cross guild channel mentions of a crosspost message if applicable.

mentioned_everyone

bool

Whether the message contains @everyone, @here.

mentioned_role_ids

None, iterable of (int, Role)

The mentioned roles' identifiers.

mentioned_users

None, iterable of ClientUserBase

The mentioned users by the message.

message_type

MessageType, int

The type of the message.

nonce

None, str

A nonce that is used for optimistic message sending. If a message is created with a nonce, then it should be shown up at the message's received payload as well.

reactions

None, ReactionMapping(or compatible)

A dictionary like object that contains the reactions on the message

referenced_message

None, Message

The referenced message.

resolved

None, Resolved

Resolved entities of selected options.

role_subscription

None, MessageRoleSubscription

Additional role subscription information attached to the message.

pinned

bool

Whether the message is pinned.

stickers

None, iterable of Sticker

The stickers sent with the message.

thread

None, Channel

The thread that was started from this message.

tts

bool

Whether the message is "text to speech".

Returns

new : instance<type<self>>

Raises

TypeError

  • If a parameter's type is incorrect.

ValueError

  • If a parameter's value is incorrect.

custom()

Deprecated and will be removed in 2024 April.

did_react(reaction, user)

Returns whether the given user reacted with the given emoji on the message.

ParameterTypeDescription
reaction

Reaction, Emoji

The reaction.

user

ClientUserBase

The reactor.

Returns

did_react : bool

from_data(data)

Creates a new message object form the given message payload. If the message already exists, picks it up.

ParameterTypeDescription
data

dict of (str, object) items

Message data.

Returns

self : instance<cls>

has_activity()

Returns whether the message has .activity set as its non-default value.

Returns

has_activity : bool

has_any_content_field()

Returns whether the message has any content field. Can be used to check whether the bot receiving / requesting the message has the message content intent.

Returns

has_any_content_field : bool

has_application()

Returns whether the message has .application set as its non-default value.

Returns

has_application : bool

has_application_id()

Returns whether the message has .application_id set as its non-default value.

Returns

has_application_id : bool

has_attachments()

Returns whether the message has .attachments set as its non-default value.

Returns

has_attachments : bool

has_channel_mentions()

Deprecated and will be removed in 2023 November.

has_components()

Returns whether the message has .components set as its non-default value.

Returns

has_components : bool

has_content()

Returns whether the message has .content set as its non-default value.

Returns

has_content : bool

has_cross_mentions()

Deprecated and will be removed in 2023 November.

has_deleted()

Deprecated and will be removed in 2023 November.

has_edited_at()

Returns whether the message has .edited_at set as its non-default value.

Returns

has_edited_at : bool

has_embeds()

Returns whether the message has .embeds set as its non-default value.

Returns

has_embeds : bool

has_everyone_mention()

Deprecated and will be removed in 2023 November.

has_flags()

Returns whether the message has .flags set as its non-default value.

Returns

has_flags : bool

has_interaction()

Returns whether the message has .interaction set.

Returns

has_interaction : bool

has_mentioned_channels()

Returns whether the message has .mentioned_channels set as its non-default value.

Returns

has_channel_mentions : bool

has_mentioned_channels_cross_guild()

Returns whether the message has .mentioned_channels_cross_guild set as its non-default value.

Returns

has_mentioned_channels_cross_guild : bool

has_mentioned_everyone()

Returns whether the message has .mentioned_everyone set as its non-default value.

Returns

has_mentioned_everyone : bool

has_mentioned_role_ids()

Returns whether the message has .mentioned_role_ids set as its non-default value.

Returns

has_role_mention_ids : bool

has_mentioned_roles()

Returns whether the message has .has_mentioned_roles set as its non-default value.

Returns

has_mentioned_roles : bool

has_mentioned_users()

Returns whether the message has .mentioned_users.

Returns

has_mentioned_users : bool

has_nonce()

Returns whether the message has .nonce set as its non-default value.

Returns

has_nonce : bool

has_partial()

Deprecated and will be removed in 2023 November.

has_pinned()

Returns whether the message has .pinned set as its non-default value.

Returns

has_pinned : bool

has_reactions()

Returns whether the message has any reactions.

A message can have no reactions even if its .reactions is not set as its default value. This can happen if reactions were removed from the message.

Returns

has_reactions : bool

has_referenced_message()

Returns whether the message has .referenced_message set as its non-default value.

Returns

has_referenced_message : bool

has_role_mention_ids()

Deprecated and will be removed in 2023 November.

has_role_mentions()

Deprecated and will be removed in 2023 November.

has_role_subscription()

Returns whether the message has .role_subscription set as its non-default value.

Returns

has_role_subscription : bool

has_stickers()

Returns whether the message has .stickers set as its non-default value.

Returns

has_stickers : bool

has_thread()

Returns whether the message has .thread set as its non-default value.

Returns

has_thread : bool

has_tts()

Returns whether the message has .tts set as its non-default value.

Returns

has_tts : bool

has_type()

Returns whether the message has .type set as its non-default value.

Returns

has_type : bool

has_user_mentions()

Deprecated and will be removed in 2023 November.

hash_resolved()

Returns whether the message has .resolved set as its non-default value.

Returns

has_resolved : bool

is_deletable()

Returns whether the message can be deleted.

Returns

is_deletable : bool

iter_attachments()

Iterates over the attachments of the message.

This method is an iterable generator.

Yields

attachment : Attachment

iter_components()

Iterates over the components of the message.

This method is an iterable generator.

Yields

component : Component

iter_contents()

Iterates over the contents of the message.

This method is an iterable generator.

Yields

content : str

iter_embeds()

Iterates over the embeds of the message.

This method is an iterable generator.

Yields

embed : Embed

iter_mentioned_channels()

Iterates over the mentioned channels in the message.

This method is an iterable generator.

Yields

mentioned_channel : Channel

iter_mentioned_channels_cross_guild()

Iterates over the mentioned channels from an other guild.

This method is an iterable generator.

Yields

mentioned_channel : Channel

iter_mentioned_role_ids()

Iterates over the mentioned roles' identifiers in the message.

This method is an iterable generator.

Yields

mentioned_role_id : int

iter_mentioned_roles()

Iterates over the mentioned roles in the message.

This method is an iterable generator.

Yields

mentioned_role : Role

iter_mentioned_users()

Iterates over the mentioned users in the message.

This method is an iterable generator.

Yields

mentioned_user : ClientUserBase

iter_stickers()

Iterates over the stickers of the message.

This method is an iterable generator.

Yields

sticker : Sticker

precreate(message_id, ...)

Precreates the message with the given parameters. Precreated messages are picked up when the message's data is received with the same id.

First tries to find whether a message exists with the given id. If it does and it is partial, updates it with the given parameters, else it creates a new one.

ParameterTypeOptionalKeyword onlyDescription
message_id

int

The message's id.

**keyword_parameters

keyword parameters

Additional predefined attributes for the message.

activity

None, MessageActivity

Message's activity information, sent with rich presence related embeds.

application

None, MessageApplication

Message's application information, sent with rich presence related embeds.

application_id

None, Application

The application or its identifier who sent the message.

attachments

None, iterable of Attachment

Attachments sent with the message.

author

UserBase

The author of the message.

call

None, MessageCall

Call information of the message.

channel

int, Channel

Alternative for channel_id.

channel_id

int, Channel

The channel's identifier where the message was created at.

components

None, iterable of Component

Components attached to the message.

content

None, str

The message's content.

edited_at

None, datetime

The time when the message was edited.

embeds

None, iterable of Embed

Embeds included with the message.

flags

MessageFlag, int

The message's flags.

guild

int, Guild

Alternative for guild_id.

guild_id

int, Guild

The guild's identifier where the message was created at.

interaction

None, MessageInteraction

Present if the message is a response to an InteractionEvent.

mentioned_channels_cross_guild

None, iterable of Channel

Cross guild channel mentions of a crosspost message if applicable.

mentioned_everyone

bool

Whether the message contains @everyone, @here.

mentioned_role_ids

None, iterable of (int, Role)

The mentioned roles' identifiers.

mentioned_roles

None, iterable of (int, Role)

Alternative for mentioned_role_ids.

mentioned_users

None, iterable of ClientUserBase

The mentioned users by the message.

message_type

MessageType, int

The type of the message.

nonce

None, str

A nonce that is used for optimistic message sending. If a message is created with a nonce, then it should be shown up at the message's received payload as well.

reactions

None, ReactionMapping(or compatible)

A dictionary like object that contains the reactions on the message

referenced_message

None, Message

The referenced message.

resolved

None, Resolved

Resolved entities of selected options.

role_subscription

None, MessageRoleSubscription

Additional role subscription information attached to the message.

pinned

bool

Whether the message is pinned.

stickers

None, iterable of Sticker

The stickers sent with the message.

thread

None, Channel

The thread that was started from this message.

tts

bool

Whether the message is "text to speech".

Returns

self : instance<cls>

Raises

TypeError

If a parameter's type is invalid.

ValueError

If a parameter's value is invalid.

to_data(...)

Tries to convert the message back to json serializable dictionary.

ParameterTypeOptionalKeyword onlyDefaultDescription
defaults

bool

False

Whether fields with their default value should be included as well.

include_internals

bool

False

Whether internal fields should be included as well.

recursive

bool

True

Whether the referenced message's data should be included as well.

Returns

data : dict of (str, object) items

to_message_reference_data()

Tries to convert the message to json serializable dictionary representing a message reference.

Returns

data : dict of (str, object)

_add_reaction(reaction, user)

Adds a reaction to the message.

ParameterTypeDescription
reaction

Reaction

The reaction.

user

ClientUserBase

The reactor user.

_clear_cache()

Clears the message's cache fields.

_create_empty(message_id, ...)

Creates a message with default parameters set.

ParameterTypeOptionalDefaultDescription
message_id

int

The message's identifier.

channel_id

int

0

The channel's identifier where the message was created at.

guild_id

int

0

The guild's identifier where the message was created at.

Returns

self : Message

_create_from_partial_data(data)

Creates a message from message reference data.

If the message is loaded already, returns that instead.

ParameterTypeDescription
data

dict of (str, object) items

Message reference data.

Returns

self : instance<cls>

_create_from_partial_fields(message_id, channel_id, guild_id)

Creates a new message from the given fields describing it.

ParameterTypeDescription
message_id

int

The unique identifier number of the represented message.

channel_id

int

The respective message's channel's identifier.

guild_id

int

The respective message's guild's identifier.

Returns

self : instance<cls>

_create_message_was_up_to_date(data)

Creates a new message object form the given message payload. If the message already exists, picks it up.

ParameterTypeDescription
data

dict of (str, object) items

Message data.

Returns

self : instance<cls>

The created or found message instance.

was_up_to_date : bool

Whether the message was found in the cache.

_difference_update_attributes(data)

Updates the message and returns it's overwritten attributes as a dict with a attribute-name- old-value relation.

A special case is if a message is (un)pinned or (un)suppressed , because then the returned dict is not going to contain 'edited_at', only 'pinned', 'flags'. If the embeds are (un)suppressed of the message, then the returned dict might contain also an 'embeds' key.

ParameterTypeDescription
data

dict of (str, object) items

Message data received from Discord.

Returns

old_attributes : dict of (str, object) items

All item in the returned dict is optional.

Returned Data Structure

KeysValues
attachmentsNone, tuple of Attachment
callNone, MessageCall
componentsNone, tuple of Component
contentNone, str
edited_atNone, datetime
embedsNone, tuple of Embed
flagsMessageFlag
mentioned_channels_cross_guildNone, tuple of Channel
mentioned_everyonebool
mentioned_role_idsNone, tuple of int
mentioned_usersNone, tuple of ClientUserBase
pinnedbool
resolvedNone, Resolved

_get_hash_partial()

Returns a partial message's hash value.

Returns

hash_value : int

_get_mentioned_channels()

Looks up the .contents of the message and searches channel mentions in them.

Invalid channel mentions are ignored.

Returns

channel_mentions : None, tuple of Channel

The parsed channel mentions.

_is_equal_same_type(other)

Returns whether self is equal to other. Other must be same type as self.

ParameterTypeDescription
other

instance<type<self>>

The other instance.

Returns

is_equal : bool

_late_init(data)

Some message fields might be missing after receiving a payload. This method is called to check and set those if multiple payload is received.

The fields are:

  • content
  • components
  • embeds
  • interaction

Since we update the content fields anyways, in this method we only update the interaction field.

ParameterTypeDescription
data

dict of (str, object) items

Message data.

_remove_reaction(reaction, user)

Removes a reaction to the message.

ParameterTypeDescription
reaction

Reaction

The reaction.

user

ClientUserBase

The user who removed their reaction.

_remove_reaction_emoji(emoji)

Removes all reactions of an emoji from the message.

ParameterTypeDescription
emoji

Emoji

The emoji to remove it's reactions.

Returns

removed_reactions : None, dict of (Reaction, ReactionMappingLine) items

_set_attributes(data, ...)

Finishes the message's initialization process by setting it's attributes.

This method required .id and .reactions to be set already.

ParameterTypeOptionalDefaultDescription
data

dict of (str, object) items

Message data.

creation

bool

True

Whether the entity was just created.

_update_attributes(data)

Updates the message with the given data by overwriting it's old attributes.

ParameterTypeDescription
data

dict of (str, object) items

Message data received from Discord.

_update_content_fields(data)

Updates the message's content attributes with the given data by overwriting it's old attributes.

ParameterTypeDescription
data

dict of (str, object) items

Message data received from Discord.

_update_embed(data)

After getting a message, it's embeds might be updated from links, or with image, video sizes. If it happens this method is called.

ParameterTypeDescription
data

dict of (str, object) items

Message data received from Discord.

Returns

change_state: int

Possible values:

Respective nameValue
EMBED_UPDATE_NONE0
EMBED_UPDATE_SIZE_UPDATE1
EMBED_UPDATE_EMBED_ADD2
EMBED_UPDATE_EMBED_REMOVE3

_update_embed_no_return(data)

Updates the message's embeds.

ParameterTypeDescription
data

dict of (str, object) items

Message data received from Discord.

__eq__()

Returns whether the two messages are equal.

__format__(code)

Formats the message in a format string.

ParameterTypeDescription
code

str

The option on based the result will be formatted.

Returns

message : str

Raises

ValueError

Unknown format code.

Examples

>>>> from hata import Message
>>>> message = Message.precreate(103589856105356645, content = 'Fluffy nekos')
>>>> message
<Message id = 103589856105356645, length = 12, author = <User name = '#0000'>>
>>>> # No code stands for str(message), what is same as repr(message) for the time being.
>>>> f'{message}'
'<Message id = 103589856105356645, length = 12, author = <User name = '#0000'>>'
>>>> # 'c' stands for created at.
>>>> f'{message:c}'
'2015-10-13 20:29:06'
>>>> # 'e' stands for edited.
>>>> f'{message:e}'
'never'
>>>> from datetime import datetime
>>>> message = Message(edited_at = datetime.utcnow())
>>>> message
<Message id = 0, length = 0, <User name = '#0000'>>
>>>> f'{message:e}'
'2015-10-13 20:29:06'

__ge__()

Whether this entity's id is greater or equal than the other's.

__getattr__()

Drops a rich attribute error.

__gt__()

Whether this entity's id is greater than the other's.

__hash__()

Returns the message's hash.

__le__()

Whether this entity's id is less or equal than the other's.

__len__()

Returns the message's total length.

__lt__()

Whether this entity's id is less than the other's.

__ne__()

Returns whether the two messages are not equal.

__repr__()

Returns the representation of the message.