discord-php / DiscordPHP

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

GUILD_MEMBER_UPDATE Error #1195

Closed Insecure05 closed 2 months ago

Insecure05 commented 6 months ago

Environment

To Reproduce

<?php

use Discord\Discord;
use Discord\WebSockets\Intents;
use Discord\WebSockets\Event;

use Discord\Parts\Channel;
use Discord\Parts\Channel\Invite;
use Discord\Parts\Channel\Message;
use Discord\Parts\Channel\Overwrite;
use Discord\Parts\Channel\Reaction;
use Discord\Parts\Channel\StageInstance;
use Discord\Parts\Channel\Webhook;

use Discord\Parts\Thread;
use Discord\Parts\Thread\Member;

use Discord\Parts\User;
use Discord\Parts\User\Activity;
use Discord\Parts\User\Client;

include __DIR__.'/vendor/autoload.php';
require_once 'function.commands.php';

$discord = new Discord([
    'token' => 'REMOVED',

    'intents' => [
        Intents::GUILDS, Intents::GUILD_MEMBERS, Intents::MESSAGE_CONTENT, Intents::GUILD_MESSAGES, Intents::DIRECT_MESSAGES, Intents::GUILD_PRESENCES
    ],

    'loadAllMembers' => true,
]);

$discord->on('init', function (Discord $discord) {

    **$discord->on(Event::GUILD_MEMBER_UPDATE, function (Member $member, Discord $discord, ?Member $oldMember) {
        echo GUILD_MEMBER_UPDATE . " action found.";
    });**

    $discord->on(Event::GUILD_MEMBER_ADD, function (Member $member, Discord $discord) {
        // ...
    });

    $discord->on(Event::THREAD_MEMBER_UPDATE, function (Member $threadMember, Discord $discord) {
        echo $threadMember;
    });

    $discord->on(Event::GUILD_MEMBER_ADD, function (Member $member, Discord $discord) {
        echo $member;
    });

    // Listen for messages.
    $discord->on(Event::MESSAGE_CREATE, function (Message $message, Discord $discord) {
        echo "Time: {$message->timestamp} {$message->author->username}: {$message->content}", PHP_EOL;
        // Note: MESSAGE_CONTENT intent must be enabled to get the content if the bot is not mentioned/DMed.
    });

});

$discord->run();

-- I have made the section in question bold. I get this error.

PHP Fatal error:  Uncaught TypeError: {closure}(): Argument #1 ($member) must be of type Discord\Parts\Thread\Member, Discord\Parts\User\Member given, called in /home/insecure/discord/bot/vendor/evenement/evenement/src/EventEmitterTrait.php on line 143 and defined in /home/insecure/discord/bot/narcos.php:39
Stack trace:

I cannot figure out why. I have tried so many different options but this keeps getting me.

Insecure05 commented 6 months ago

I am wondering if you have any suggestions. I'm not new to PHP but new to React and Discord Bots. I admit I'm not an power user with PHP, just average.

key2peace commented 6 months ago
$discord->on(Event::GUILD_MEMBER_UPDATE, function ($member, Discord $discord, $oldMember) {
        echo GUILD_MEMBER_UPDATE . " action found.";
});

as in, drop the typecasting

Insecure05 commented 6 months ago

I will try this when I get home.

I copied the code directly from the wiki page. Most of them have the typecasting in it.

This page.

https://discord-php.github.io/DiscordPHP/guide/events/guilds.html

Insecure05 commented 6 months ago

I actually was able to try it now. I get the same error with the change.

    $discord->on(Event::GUILD_MEMBER_UPDATE, function ($member, Discord $discord, $oldMember) {
        echo GUILD_MEMBER_UPDATE . " action found.";
    });
PHP Fatal error:  Uncaught TypeError: {closure}(): Argument #1 ($member) must be of type Discord\Parts\Thread\Member, Discord\Parts\User\Member given, called in /home/insecure/discord/bot/vendor/evenement/evenement/src/EventEmitterTrait.php on line 143 and defined in /home/insecure/discord/bot/narcos.php:39
Stack trace:
key2peace commented 6 months ago

can you try to get rid of all the Member typecasting in there?

valzargaming commented 2 months ago

Closed as fix was provided but no confirmation for 4 months