discord-php / DiscordPHP

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

Author on Message #436

Closed MincoMK closed 3 years ago

MincoMK commented 3 years ago

$message->author Is null

$discord->on('message', function ($message, $discord) {
        $discord_full_id = "{$message->author->user}";
        $discord_id = str_replace(">", "", str_replace("<@", "", $discord_full_id));
        $guild_id = $message->channel->guild_id;
            $channel_id = $message->channel_id;
            $guild = $discord->guilds->get('id', $guild_id);
            $botuser = $guild->members->get('id', 722051357783228498 );
            $channel = $guild->channels->get('id', $channel_id);

        if ($message->author == $botuser) {
          return;
        }

        msg($message);
        if (fnmatch('!*', $message->content)) {
          $message->channel->messages->delete($message);
        }
    });
});

function msg($msg) {
  echo var_dump($msg->author); // [ THIS ]
}

This is My code "[ THIS ]" Returns "NULL"

Why?

I got a null->user->... error.

BotUser code successfully blocks bot-executing commands (I got a very big bug that bot executes same cmd, loop)

I'm both NewBie of English, DiscordPHP, PHP Object. Pls execute it, and read.

++ How to get user by Mention? EX) !dmbot \@Minco#0000 How to make bot DM to \@Minco#0000 when I execute this cmd? (Do not care about \, Just for no-GitHub-mention)

CrazyHenk44 commented 3 years ago

Is this happening directly when you start the bot or when the bot is running for a while? And does the console show a reconnect?

MincoMK commented 3 years ago

Is this happening directly when you start the bot or when the bot is running for a while? And does the console show a reconnect?

No, It happens when I type !command in Discord

eqMFqfFd commented 3 years ago

You have to fetch the user, the reaction (for message reaction listener) and so on. I don't know why this is not done by DiscordPHP itself... @davidcole1340

But the issue in your code is that you've tried to get the Author again from the Message, that won't work. If you want the author in the Message fetch the message.

$channel->messages->fetch($message->id)->done(function($message){
    var_dump($message);
});
davidcole1340 commented 3 years ago

Have a look in your Discord developer settings and enable presence updates and server members:

image

The problem likely is that when you call $discord_full_id = "{$message->author->user}";, $message->author is already a User object, therefore it doesn't have a user parameter

MincoMK commented 3 years ago

Have a look in your Discord developer settings and enable presence updates and server members:

image

The problem likely is that when you call $discord_full_id = "{$message->author->user}";, $message->author is already a User object, therefore it doesn't have a user parameter

But var_dump($msg->author) returns "NULL"

++ How to set my bot's msg like playing Minecraft without "Playing"? ++ I want to make "10 Users using my Bot" ++ How to get all servers, How to announce in all servers?

key2peace commented 3 years ago

lemme redo that code you started with in the first place, you will learn a lot of things from this, as you seem to be example orientated:

$discord->on('message', function ($message, $discord) {
    // $discord->id is the bots user id, the author id is contained in $message->author->id, NOT $message->author
    if ($message->author->id == $discord->id) {
        return;
    }

    // some example extracts you did in a VERY complex way whilst not needed
    $guild = $message->channel->guild;
    $botuser = $guild->members->offsetGet($discord->id);
    $channel = $message->channel;

    // var_dump doesn't require an echo, it already does that
    var_dump($message->author); // [ THIS ]

    if (fnmatch('!*', $message->content)) {
        // way too complext thinking as well here using $message->channel->messages->delete($message);, just
        $message->delete();
    }
});

++ How to set my bot's msg like playing Minecraft without "Playing"? Very simple: NOT, bots are not allowed to use custom status messages.

++ I want to make "10 Users using my Bot" see #420

++ How to get all servers, How to announce in all servers? $discord->guilds contains all guilds the bot is in, however, without knowing a channel in that guild to announce to (and you surely would not want to announce in all channels), what you want to do there ain't easy without knowing more.