Skip to content
Advertisement

Changing the content of a message sent by my bot in Discord

I use a slash command to create a channel, and in the channel, a message whose content is like this :

Title : my title
Date  : 23rd Oct. 2021
Desc. : a brief description

Then, the bot pins this message. All that works fine.

Now, I wanna use another command to change that content. Here is how I try to change the message’s content :

    $channel = $interaction->guild->channels->get('id', $idChannel);
    $channel->getPinnedMessages()->done(function ($list) use ($channel) {
        foreach ($list as $msg) {
            if ($msg->author->id == $this->botId) {
                $channel->getMessage($msg->id)->done( function ($pinnedMsg) {
                    echo "nMessage foundn";
                    echo $pinnedMsg->content;
                    $pinnedMsg->content = "new text";
                    echo "nMessage changedn";
                    echo $pinnedMsg->content;
                });
                break;
            }
        }
    });

The “Message changed” is correct in the console, but is NOT changed in Discord. Why ?

Note : I tried $pinnedMsg->edit (I saw this in some answers about Discord.js and tried to adapt it to php) but it didn’t work : the following echo was not displayed.

Advertisement

Answer

Found the solution : you must call save on the messages list :

$channel = $interaction->guild->channels->get('id', $idChannel);
$channel->getPinnedMessages()->done(function ($list) use ($channel) {
    foreach ($list as $msg) {
        if ($msg->author->id == $this->botId) {
            $channel->getMessage($msg->id)->done( function ($pinnedMsg) {
                $pinnedMsg->content = "new text";
                $channel->messages->save($pinnedMsg)->done( function ($x) { echo "done"; } );
            });
            break;
        }
    }
});

Not sure the done(…) after the save (…) is necessary, but it seems to be the syntax required. And, like this, it works.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement