Skip to content

Commit

Permalink
Tweak deleteMessages(), add Collection::last() (#736)
Browse files Browse the repository at this point in the history
* Update message deleting
- Add reason headers to $message->delete
- Update $channel->deleteMessages by looping through messages and check if they aren't older then 14 days, if they are, remove them from the bulk_delete batch and remove them individually.

* remove delete single message audit log, and tweak

* phpdoc

* fix deleting non cached single message by id

* make individual message delete last queue priority

* workaround for single non old in bulk message

* ugly workaround, remove elseif after return

* overhaul deleteMessages

* add collection::last()

* run php-cs-fixer

Co-authored-by: SQKo <[email protected]>
  • Loading branch information
key2peace and SQKo authored Feb 24, 2022
1 parent 781fcdc commit 397c522
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 34 deletions.
2 changes: 1 addition & 1 deletion src/Discord/Builders/MessageBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class MessageBuilder implements JsonSerializable

/**
* Attachments to send with this message.
*
*
* @var Attachment[]
*/
private $attachments = [];
Expand Down
18 changes: 18 additions & 0 deletions src/Discord/Helpers/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,24 @@ public function first()
return null;
}

/**
* Returns the last element of the collection.
*
* @return mixed
*/
public function last()
{
$last = end($this->items);

if ($last !== false) {
reset($this->items);

return $last;
}

return null;
}

/**
* If the collection has an offset.
*
Expand Down
53 changes: 22 additions & 31 deletions src/Discord/Parts/Channel/Channel.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
use Symfony\Component\OptionsResolver\OptionsResolver;
use Traversable;

use function Discord\getSnowflakeTimestamp;
use function React\Promise\all;
use function React\Promise\reject;
use function React\Promise\resolve;
Expand Down Expand Up @@ -546,45 +547,35 @@ public function deleteMessages($messages, ?string $reason = null): ExtendedPromi
return reject(new \UnexpectedValueException('$messages must be an array or implement Traversable.'));
}

$count = count($messages);

if ($count == 0) {
return resolve();
} elseif ($count == 1 || $this->is_private) {
foreach ($messages as $message) {
if ($message instanceof Message ||
$message = $this->messages->get('id', $message)
) {
return $message->delete();
}
$headers = $promises = $messagesBulk = $messagesSingle = [];
if (isset($reason)) {
$headers['X-Audit-Log-Reason'] = $reason;
}

return $this->http->delete(Endpoint::bind(Endpoint::CHANNEL_MESSAGE, $this->id, $message));
foreach ($messages as $message) {
if ($message instanceof Message) {
$message = $message->id;
}
} else {
$messageID = [];

foreach ($messages as $message) {
if ($message instanceof Message) {
$messageID[] = $message->id;
} else {
$messageID[] = $message;
}
if ($this->is_private || getSnowflakeTimestamp($message) < time() - 1209600) {
$messagesSingle[] = $message;
} else {
$messagesBulk[] = $message;
}
}

$promises = [];

$headers = [];
if (isset($reason)) {
$headers['X-Audit-Log-Reason'] = $reason;
}
while (count($messagesBulk) > 1) {
$promises[] = $this->http->post(Endpoint::bind(Endpoint::CHANNEL_MESSAGES_BULK_DELETE, $this->id), ['messages' => array_slice($messagesBulk, 0, 100)], $headers);
$messagesBulk = array_slice($messagesBulk, 100);
}

while (! empty($messageID)) {
$promises[] = $this->http->post(Endpoint::bind(Endpoint::CHANNEL_MESSAGES_BULK_DELETE, $this->id), ['messages' => array_slice($messageID, 0, 100)], $headers);
$messageID = array_slice($messageID, 100);
}
$messagesSingle = array_merge($messagesSingle, $messagesBulk);

return all($promises);
foreach ($messagesSingle as $message) {
$promises[] = $this->http->delete(Endpoint::bind(Endpoint::CHANNEL_MESSAGE, $this->id, $message));
}

return all($promises);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Discord/Parts/Channel/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
* @property string|null $guild_id The unique identifier of the guild that the channel the message was sent in belongs to.
* @property Guild|null $guild The guild that the message was sent in.
* @property User|null $author The author of the message. Will be a webhook if sent from one.
* @property string $user_id The user id of the author.
* @property string|null $user_id The user id of the author.
* @property Member|null $member The member that sent this message, or null if it was in a private message.
* @property string $content The content of the message if it is a normal message.
* @property Carbon $timestamp A timestamp of when the message was sent.
Expand Down
2 changes: 1 addition & 1 deletion src/Discord/Voice/VoiceClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ class VoiceClient extends EventEmitter
protected $version = 4;

/**
* The Config for DNS Resolver
* The Config for DNS Resolver.
*
* @var string|\React\Dns\Config\Config
*/
Expand Down

0 comments on commit 397c522

Please sign in to comment.