I use a slash command to create a channel, and in the channel, a message whose content is like this :
JavaScript
x
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 :
JavaScript
$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 :
JavaScript
$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.