Represents a text message received from Discord. This represents messages received from MessageChannels.
Overview
The Message interface provides access to all message data including content, attachments, embeds, and reactions. Messages are immutable - JDA does not track changes to messages.
Key Properties
Message Content
The raw textual content of this message. Does not resolve mentions.Requires MESSAGE_CONTENT intent.String content = message.getContentRaw();
The textual content with mentions resolved to be visually like the Discord client.Requires MESSAGE_CONTENT intent.String displayContent = message.getContentDisplay();
Gets the textual content and strips markdown formatting characters.Requires MESSAGE_CONTENT intent.String strippedContent = message.getContentStripped();
The author of this Message.User author = message.getAuthor();
System.out.println("From: " + author.getAsTag());
Returns the author of this Message as a Member.Returns null if the message was not sent in a guild, or if sent by a webhook.Member member = message.getMember();
if (member != null) {
System.out.println("Roles: " + member.getRoles());
}
Indicates if this Message was sent by a Webhook or an app, instead of a User.if (message.isWebhookMessage()) {
return; // Ignore webhook messages
}
Channel and Guild
Returns the MessageChannel that this message was sent in.MessageChannel channel = message.getChannel();
channel.sendMessage("Reply").queue();
Returns the GuildMessageChannel that this message was sent in if it was sent in a Guild.if (message.isFromGuild()) {
GuildMessageChannel guildChannel = message.getGuildChannel();
}
Returns the Guild that this message was sent in.if (message.isFromGuild()) {
Guild guild = message.getGuild();
}
Whether this message was sent in a Guild.if (message.isFromGuild()) {
// Handle guild message
}
Gets the ChannelType that this message was received from.ChannelType type = message.getChannelType();
if (type == ChannelType.PRIVATE) {
System.out.println("DM message");
}
Attachments and Embeds
An immutable list of Attachments that are attached to this message.Requires MESSAGE_CONTENT intent.List<Message.Attachment> attachments = message.getAttachments();
for (Message.Attachment attachment : attachments) {
System.out.println("File: " + attachment.getFileName());
System.out.println("URL: " + attachment.getUrl());
}
An immutable list of MessageEmbeds that are part of this Message.Requires MESSAGE_CONTENT intent.List<MessageEmbed> embeds = message.getEmbeds();
for (MessageEmbed embed : embeds) {
System.out.println("Title: " + embed.getTitle());
}
Components and Interactions
getComponents()
List<MessageTopLevelComponentUnion>
Layouts of interactive components, usually ActionRows.Requires MESSAGE_CONTENT intent.List<MessageTopLevelComponentUnion> components = message.getComponents();
All buttons attached to this message.List<Button> buttons = message.getButtons();
Reactions
All MessageReactions that are on this Message.List<MessageReaction> reactions = message.getReactions();
for (MessageReaction reaction : reactions) {
System.out.println(reaction.getEmoji().getName() + ": " + reaction.getCount());
}
The time this message was created.OffsetDateTime created = message.getTimeCreated();
Returns whether or not this Message has been edited before.if (message.isEdited()) {
OffsetDateTime edited = message.getTimeEdited();
}
Provides the OffsetDateTime defining when this Message was last edited.Returns null if the message has never been edited.OffsetDateTime editedTime = message.getTimeEdited();
Returns the jump-to URL for this message. Clicking this URL in Discord will jump to the message.String jumpUrl = message.getJumpUrl();
Whether this message is pinned in its channel.if (message.isPinned()) {
System.out.println("This is a pinned message");
}
References
Returns the MessageReference for this Message.Returns null if this message has no reference.MessageReference reference = message.getMessageReference();
if (reference != null) {
Message referenced = reference.getMessage();
}
The referenced message for replies.Returns null if not a reply or if the referenced message wasn’t loaded.Message replyTo = message.getReferencedMessage();
if (replyTo != null) {
System.out.println("Replying to: " + replyTo.getContentDisplay());
}
Mentions
The Mentions used in this message.Includes Members, GuildChannels, Roles, and CustomEmojis.List<User> mentionedUsers = message.getMentions().getUsers();
List<Role> mentionedRoles = message.getMentions().getRoles();
boolean mentionsEveryone = message.getMentions().mentionsEveryone();
Message Actions
Editing
editMessage(CharSequence newContent)
Edits this message and updates the content.message.editMessage("Updated content")
.queue();
editMessage(MessageEditData data)
Edits this message using the provided MessageEditData.MessageEditData edit = new MessageEditBuilder()
.setContent("New content")
.setEmbeds(embed)
.build();
message.editMessage(edit).queue();
editMessageEmbeds(Collection<MessageEmbed> embeds)
Edits this message using the provided MessageEmbeds.message.editMessageEmbeds(newEmbed)
.queue();
editMessageComponents(Collection<MessageTopLevelComponent> components)
Edits this message using the provided components.message.editMessageComponents(ActionRow.of(button))
.queue();
Deletion
delete()
AuditableRestAction<Void>
Deletes this message from the channel.message.delete()
.reason("Inappropriate content")
.queue();
Reactions
Adds a reaction to this message.message.addReaction(Emoji.fromUnicode("👍"))
.queue();
removeReaction(Emoji emoji)
Removes the bot’s reaction from this message.message.removeReaction(Emoji.fromUnicode("👍"))
.queue();
Removes all reactions from this message.message.clearReactions()
.queue();
Pinning
Pins this message in its channel.
Unpins this message from its channel.message.unpin()
.queue();
Replies
reply(CharSequence content)
Replies to this message with the provided content.message.reply("Got it!")
.queue();
reply(MessageCreateData data)
Replies to this message with the provided MessageCreateData.message.reply(MessageCreateData.fromContent("Reply"))
.setEphemeral(true)
.queue();
Constants
The maximum amount of characters sendable in one message: 2000
The maximum sendable file size: 10485760 (10 MiB)
The maximum amount of files sendable within a single message: 10
The maximum amount of Embeds that can be added to one message: 10
The maximum amount of reactions that can be added to one message: 20
Usage Example
// Get message information
Message message = event.getMessage();
System.out.println("Content: " + message.getContentDisplay());
System.out.println("Author: " + message.getAuthor().getAsTag());
// Check attachments
if (!message.getAttachments().isEmpty()) {
for (Message.Attachment attachment : message.getAttachments()) {
System.out.println("File: " + attachment.getFileName());
}
}
// Reply to message
message.reply("Received your message!")
.queue();
// Add reaction
message.addReaction(Emoji.fromUnicode("✅"))
.queue();
// Edit message (if bot's own message)
if (message.getAuthor().equals(jda.getSelfUser())) {
message.editMessage("Updated content")
.queue();
}
// Delete message
message.delete()
.reason("Cleanup")
.queueAfter(5, TimeUnit.SECONDS);
// Check mentions
List<User> mentioned = message.getMentions().getUsers();
for (User user : mentioned) {
System.out.println("Mentioned: " + user.getAsTag());
}
See Also