Skip to content
SQKo edited this page Dec 9, 2023 · 8 revisions

Messages are part of Channel (Text Channel or Direct Message)

use Discord\Parts\Channel\Message;

Repositories

Requires GUILD_MESSAGES intent for guild, or DIRECT_MESSAGES intent for direct message

MESSAGE_CREATE

MESSAGE_DELETE

MESSAGE_UPDATE

MESSAGE_DELETE_BULK


Get a specific Message

You must first get the Channel object to use the code below. Change 123123123123123123 below with the Message ID you want to retrieve

Cached, synchrounous:

$message = $channel->messages->get('id', '123123123123123123');

Message can be also retrieved from related objects:

  • Message $reply = $message->referenced_message; reply referenced to the message
  • Reaction $message = $reaction->message; message where the reaction is part of

If the code above returns null, you may need to fetch it first (Promise, asynchrounous):

$channel->messages->fetch('123123123123123123')->then(function (Message $message) {
    // ...
})->done();

https://discord.com/developers/docs/resources/channel#get-channel-message

Delete a message

You must first get the Message object to use the code below.

$message->delete()->then(function (Message $message) {
  // ...
})->done();

Edit a message

You must first get the Message object to use the code below. You also need the MessageBuilder to create a message.

$message->edit(MessageBuilder::new()->setContent('new message content!'))->then(function (Message $message) {
  // ...
})->done();
Clone this wiki locally