discord-php / DiscordPHP

An API to interact with the popular messaging app Discord
MIT License
985 stars 236 forks source link

discord bot crashes completely after trying to send message to forum channels #1088

Closed Jannik44 closed 1 year ago

Jannik44 commented 1 year ago

Environment

Describe the bug I tried to send messages to normal channels, works as expected, when i send messages into a forum thread i get no message and the program crashes

To Reproduce Steps to reproduce the behavior:


include __DIR__ . '/vendor/autoload.php';

use Discord\Discord;
use Discord\Parts\Channel\Message;
use Discord\WebSockets\Intents;
use Discord\WebSockets\Event;

$discord = new Discord([
    'token' => 'redacted',
    'intents' => Intents::getDefaultIntents()
    //      | Intents::MESSAGE_CONTENT, // Note: MESSAGE_CONTENT is privileged, see https://dis.gd/mcfaq
]);

$discord->on('ready', function (Discord $discord) {
    echo "Bot is ready!", PHP_EOL;

    // Listen for messages.
    $discord->on(Event::MESSAGE_CREATE, function (Message $message, Discord $discord) {
        if ($message->author != "<@1095101543872725092>") {
            echo "{$message->author->username}: {$message->content}", PHP_EOL;

            $channel = $discord->getChannel($message->channel_id);
            if ($message->content == "e" or $message->content == "E")
                $channel->sendMessage('E');
        }
    });
});
$discord->run();

Expected behavior it should send a message in the forum thread

Screenshots grafik grafik

SQKo commented 1 year ago

getChannel() can be only used to get Channel, not Threads. This is not a bug.

If you wish to get the thread or channel where the message was sent, just use $message->channel And you must always check if $channel is not null, because it might be not cached yet.

$message->author != "<@1095101543872725092>" even this line is wrong, to compare author id it must be: $message->author->id != 1095101543872725092