discord-php / DiscordPHP

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

Question about "repositories" #1058

Closed MayorUshanka closed 1 year ago

MayorUshanka commented 1 year ago

How can I iterate all elements of a repository?

I want to archive (save to local files) all the messages in a channel. To do this, I must iterate over all messages in a channel. ChatGTP suggested this code:

    $channel = $discord->getChannel($channel_id);

    $limit = 100;
    $before = null;
    do {
        $messages = $channel->messages->all([
            'limit' => $limit,
            'before' => $before,
        ]);
        foreach ($messages as $message) {
            echo $message->content . PHP_EOL;
            $before = $message->id;
        }
    } while (count($messages) == $limit);

But alas, all is not actually a method that exists. The only contender I read on your documentation is get, but that requires a specific message id. The point is, I don't have a specific message, I just want to enumerate ALL of them. How can I do this?

FoxWorn3365 commented 1 year ago

getMessageHistory() with a promise should work _(You must have the MESSAGE_CONTENT intent enabled)_