discord-php / DiscordPHP

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

can not send message on channel that fetched from Guild #522

Closed weichenlin closed 3 years ago

weichenlin commented 3 years ago

Environment

Describe the bug when I get channel by $guild->channels->fetch() the $channel->sendMessage() silent fails

To Reproduce Steps to reproduce the behavior:

// this success
$discord->getChannel($myChannelId)->sendMessage("from getChannel");

$discord->guilds->fetch($myGuildId, true)->done(function (Guild $guild) use ($myChannelId, $discord) {
    $guild->channels->fetch($myChannelId, true)->done(function (Channel $ch) use ($discord) {
        echo "got channel\n"; // this line has executed
        // this fails
        $ch->sendMessage('from fetch channelId');
    });
});

Expected behavior should see both message on discord channel

Additional context use xdebug to trace code, an exception are thrown on Channel.php line 708: $botperms = $this->guild->members->offsetGet($this->discord->id)->getPermissions($this); the exception is : Call to a member function getPermissions() on null

key2peace commented 3 years ago

try ...->sendMessage("your message here")->then(null, function($response) { dump($response); });

also, based on your trace, it means you do not have GUILD_MEMBERS enabled in both the dev console and in the intents, see the changelog

weichenlin commented 3 years ago

this's my config

$discord = new Discord([
    'token' => $myToken,
    'intents' => Intents::getDefaultIntents() | Intents::GUILD_MEMBERS,
    'loadAllMembers' => true,
]);

in discord developer portal, bot page: Screenshot_20210422_010031

I changed code to

$guild->channels->fetch($myChannelId, true)->done(function (Channel $ch) {
    echo "got channel\n";
    $ch->sendMessage('from fetch channelId')->then(null, function($response) { var_dump($response); });
    echo "done\n";
});

and got:

[2021-04-21T17:02:58.884986+00:00] DiscordPHP.DEBUG: REQ GET channels/767679967327682581 successful [] []
got channel
[2021-04-21T17:03:38.658914+00:00] DiscordPHP.DEBUG: sending heartbeat {"seq":4} []

the var_dump and the msg "done" are not appear I didn't see the msg on discord as well

sherwomeister commented 9 months ago

Why was the fix removed? Faced exactly the same problem, fixed it as in the commit:

// src/Discord/Parts/Channel/Channel.php
// public function sendMessage($message, bool $tts = false, $embed = null, $allowed_mentions = null, ?Message $replyTo = null): ExtendedPromiseInterface

if (! $this->is_private && $member = $this->guild->members->offsetGet($this->discord->id)) {
            $botperms = $member->getBotPermissions();
// ...
SQKo commented 9 months ago

Why was the fix removed? Faced exactly the same problem, fixed it as in the commit:

// src/Discord/Parts/Channel/Channel.php
// public function sendMessage($message, bool $tts = false, $embed = null, $allowed_mentions = null, ?Message $replyTo = null): ExtendedPromiseInterface

if (! $this->is_private && $member = $this->guild->members->offsetGet($this->discord->id)) {
            $botperms = $member->getBotPermissions();
// ...

Sorry, the fix was simplified but overlooked, it was never removed. It is another new bug. It will be fixed soon (dev-master# c50d947 ), thanks for reporting!

But in future, please do not follow the practice of freshening Guild & Channel (avoid the code in this issue) when not necessary.