diff --git a/src/Discord/WebSockets/Event.php b/src/Discord/WebSockets/Event.php index 96dc1a64e..295786b73 100644 --- a/src/Discord/WebSockets/Event.php +++ b/src/Discord/WebSockets/Event.php @@ -150,6 +150,21 @@ public function __construct(Http $http, Factory $factory, Discord $discord) */ abstract public function handle(Deferred &$deferred, $data); + /** + * Cache User repository from Event data + * + * @param object $userdata + */ + protected function cacheUser($userdata) + { + // User caching + if ($user = $this->discord->users->get('id', $userdata->id)) { + $user->fill((array) $userdata); + } else { + $this->discord->users->pushItem($this->factory->part(\Discord\Parts\User\User::class, (array) $userdata, true)); + } + } + public function __debugInfo(): array { return []; diff --git a/src/Discord/WebSockets/Events/GuildBanAdd.php b/src/Discord/WebSockets/Events/GuildBanAdd.php index b2d482aeb..5b20aeecb 100644 --- a/src/Discord/WebSockets/Events/GuildBanAdd.php +++ b/src/Discord/WebSockets/Events/GuildBanAdd.php @@ -29,6 +29,8 @@ public function handle(Deferred &$deferred, $data): void $this->discord->guilds->push($guild); } + $this->cacheUser($data->user); + $deferred->resolve($ban); } } diff --git a/src/Discord/WebSockets/Events/GuildBanRemove.php b/src/Discord/WebSockets/Events/GuildBanRemove.php index a78e52ac7..51bcb9158 100644 --- a/src/Discord/WebSockets/Events/GuildBanRemove.php +++ b/src/Discord/WebSockets/Events/GuildBanRemove.php @@ -29,6 +29,8 @@ public function handle(Deferred &$deferred, $data): void $this->discord->guilds->push($guild); } + $this->cacheUser($data->user); + $deferred->resolve($ban); } } diff --git a/src/Discord/WebSockets/Events/GuildEmojisUpdate.php b/src/Discord/WebSockets/Events/GuildEmojisUpdate.php index f8a7a6de9..81faf3f11 100644 --- a/src/Discord/WebSockets/Events/GuildEmojisUpdate.php +++ b/src/Discord/WebSockets/Events/GuildEmojisUpdate.php @@ -32,7 +32,10 @@ public function handle(Deferred &$deferred, $data): void } foreach ($data->emojis as $emoji) { - if (! isset($emoji->user) && $oldPart = $oldParts->offsetGet($emoji->id)) { + if (isset($emoji->user)) { + // User caching from emoji uploader + $this->cacheUser($emoji->user); + } elseif ($oldPart = $oldParts->offsetGet($emoji->id)) { $emoji->user = $oldPart->user; } $emojiPart = $this->factory->create(Emoji::class, $emoji, true); diff --git a/src/Discord/WebSockets/Events/GuildMemberAdd.php b/src/Discord/WebSockets/Events/GuildMemberAdd.php index 14eb88d98..e35250ef8 100644 --- a/src/Discord/WebSockets/Events/GuildMemberAdd.php +++ b/src/Discord/WebSockets/Events/GuildMemberAdd.php @@ -30,7 +30,8 @@ public function handle(Deferred &$deferred, $data): void ++$guild->member_count; } - $this->discord->users->push($member->user); + $this->cacheUser($data->user); + $deferred->resolve($member); } } diff --git a/src/Discord/WebSockets/Events/GuildMemberRemove.php b/src/Discord/WebSockets/Events/GuildMemberRemove.php index b6cc96625..ab4cba73f 100644 --- a/src/Discord/WebSockets/Events/GuildMemberRemove.php +++ b/src/Discord/WebSockets/Events/GuildMemberRemove.php @@ -31,6 +31,8 @@ public function handle(Deferred &$deferred, $data): void $this->discord->guilds->push($guild); } + $this->cacheUser($data->user); + $deferred->resolve($member); } } diff --git a/src/Discord/WebSockets/Events/GuildMemberUpdate.php b/src/Discord/WebSockets/Events/GuildMemberUpdate.php index 346fc4c4e..96697705d 100644 --- a/src/Discord/WebSockets/Events/GuildMemberUpdate.php +++ b/src/Discord/WebSockets/Events/GuildMemberUpdate.php @@ -34,9 +34,7 @@ public function handle(Deferred &$deferred, $data): void $guild->members->push($memberPart); } - if ($user = $this->discord->users->get('id', $data->user->id)) { - $user->fill((array) $data->user); - } + $this->cacheUser($data->user); $deferred->resolve([$memberPart, $old]); } diff --git a/src/Discord/WebSockets/Events/GuildScheduledEventCreate.php b/src/Discord/WebSockets/Events/GuildScheduledEventCreate.php index e8981dd39..b06203947 100644 --- a/src/Discord/WebSockets/Events/GuildScheduledEventCreate.php +++ b/src/Discord/WebSockets/Events/GuildScheduledEventCreate.php @@ -29,6 +29,10 @@ public function handle(Deferred &$deferred, $data): void $guild->guild_scheduled_events->push($scheduled_event); } + if (isset($data->creator)) { + $this->cacheUser($data->creator); + } + $deferred->resolve($scheduled_event); } } diff --git a/src/Discord/WebSockets/Events/GuildScheduledEventDelete.php b/src/Discord/WebSockets/Events/GuildScheduledEventDelete.php index 490357390..f11387468 100644 --- a/src/Discord/WebSockets/Events/GuildScheduledEventDelete.php +++ b/src/Discord/WebSockets/Events/GuildScheduledEventDelete.php @@ -29,6 +29,10 @@ public function handle(Deferred &$deferred, $data): void $guild->guild_scheduled_events->pull($scheduled_event->id); } + if (isset($data->creator)) { + $this->cacheUser($data->creator); + } + $deferred->resolve($scheduled_event); } } diff --git a/src/Discord/WebSockets/Events/GuildScheduledEventUpdate.php b/src/Discord/WebSockets/Events/GuildScheduledEventUpdate.php index 3afd50794..2d9d4a4ad 100644 --- a/src/Discord/WebSockets/Events/GuildScheduledEventUpdate.php +++ b/src/Discord/WebSockets/Events/GuildScheduledEventUpdate.php @@ -30,6 +30,8 @@ public function handle(Deferred &$deferred, $data): void $guild->guild_scheduled_events->push($scheduled_event); } + $this->cacheUser($data->creator); + $deferred->resolve([$scheduled_event, $old]); } } diff --git a/src/Discord/WebSockets/Events/GuildStickersUpdate.php b/src/Discord/WebSockets/Events/GuildStickersUpdate.php index a311754b0..2a5504c43 100644 --- a/src/Discord/WebSockets/Events/GuildStickersUpdate.php +++ b/src/Discord/WebSockets/Events/GuildStickersUpdate.php @@ -32,7 +32,10 @@ public function handle(Deferred &$deferred, $data): void } foreach ($data->stickers as $sticker) { - if (! isset($sticker->user) && $oldPart = $oldParts->offsetGet($sticker->id)) { + if (isset($sticker->user)) { + // User caching from sticker uploader + $this->cacheUser($sticker->user); + } elseif($oldPart = $oldParts->offsetGet($sticker->id)) { $sticker->user = $oldPart->user; } $stickerPart = $this->factory->create(Sticker::class, $sticker, true); diff --git a/src/Discord/WebSockets/Events/IntegrationCreate.php b/src/Discord/WebSockets/Events/IntegrationCreate.php index 02a7461df..b0db2b62a 100644 --- a/src/Discord/WebSockets/Events/IntegrationCreate.php +++ b/src/Discord/WebSockets/Events/IntegrationCreate.php @@ -29,13 +29,8 @@ public function handle(Deferred &$deferred, $data): void $guild->integrations->pushItem($integration); } - // User caching - if (! isset($data->user)) { - if ($user = $this->discord->users->get('id', $data->user->id)) { - $user->fill((array) $data->user); - } else { - $this->discord->users->pushItem($this->factory->part(User::class, (array) $data->user, true)); - } + if (isset($data->user)) { + $this->cacheUser($data->user); } $deferred->resolve($integration); diff --git a/src/Discord/WebSockets/Events/IntegrationUpdate.php b/src/Discord/WebSockets/Events/IntegrationUpdate.php index 5dc616da6..7f9eb0d3e 100644 --- a/src/Discord/WebSockets/Events/IntegrationUpdate.php +++ b/src/Discord/WebSockets/Events/IntegrationUpdate.php @@ -29,13 +29,8 @@ public function handle(Deferred &$deferred, $data): void $guild->integrations->pushItem($integration); } - // User caching - if (! isset($data->user)) { - if ($user = $this->discord->users->get('id', $data->user->id)) { - $user->fill((array) $data->user); - } else { - $this->discord->users->pushItem($this->factory->part(User::class, (array) $data->user, true)); - } + if (isset($data->user)) { + $this->cacheUser($data->user); } $deferred->resolve($integration); diff --git a/src/Discord/WebSockets/Events/InteractionCreate.php b/src/Discord/WebSockets/Events/InteractionCreate.php index cd0ac2a19..c296d334d 100644 --- a/src/Discord/WebSockets/Events/InteractionCreate.php +++ b/src/Discord/WebSockets/Events/InteractionCreate.php @@ -30,8 +30,7 @@ public function handle(Deferred &$deferred, $data): void if ($userPart = $this->discord->users->get('id', $snowflake)) { $userPart->fill((array) $user); } else { - $userPart = $this->factory->create(User::class, $user, true); - $this->discord->users->pushItem($userPart); + $this->discord->users->pushItem($this->factory->part(User::class, (array) $user, true)); } } @@ -59,6 +58,16 @@ public function handle(Deferred &$deferred, $data): void } } + if (isset($data->member->user)) { + // User caching from member + $this->cacheUser($data->member->user); + } + + if (isset($data->user)) { + // User caching from user dm + $this->cacheUser($data->user); + } + $deferred->resolve($interaction); } } diff --git a/src/Discord/WebSockets/Events/InviteCreate.php b/src/Discord/WebSockets/Events/InviteCreate.php index 5a3a54ac4..035fe8c25 100644 --- a/src/Discord/WebSockets/Events/InviteCreate.php +++ b/src/Discord/WebSockets/Events/InviteCreate.php @@ -24,6 +24,16 @@ public function handle(Deferred &$deferred, $data): void { $invite = $this->factory->create(Invite::class, $data, true); + if (isset($data->inviter)) { + // User caching from inviter + $this->cacheUser($data->inviter); + } + + if (isset($data->target_user)) { + // User caching from target user + $this->cacheUser($data->target_user); + } + $deferred->resolve($invite); } } diff --git a/src/Discord/WebSockets/Events/MessageReactionAdd.php b/src/Discord/WebSockets/Events/MessageReactionAdd.php index c874a23a9..bac40728d 100644 --- a/src/Discord/WebSockets/Events/MessageReactionAdd.php +++ b/src/Discord/WebSockets/Events/MessageReactionAdd.php @@ -14,7 +14,6 @@ use Discord\Parts\WebSockets\MessageReaction; use Discord\WebSockets\Event; use Discord\Helpers\Deferred; -use Discord\Parts\Channel\Reaction; class MessageReactionAdd extends Event { @@ -53,6 +52,10 @@ public function handle(Deferred &$deferred, $data): void } } + if (isset($data->member->user)) { + $this->cacheUser($data->member->user); + } + $deferred->resolve($reaction); } } diff --git a/src/Discord/WebSockets/Events/TypingStart.php b/src/Discord/WebSockets/Events/TypingStart.php index 216cb556e..4b4b2716e 100644 --- a/src/Discord/WebSockets/Events/TypingStart.php +++ b/src/Discord/WebSockets/Events/TypingStart.php @@ -24,6 +24,10 @@ public function handle(Deferred &$deferred, $data): void { $typing = $this->factory->create(TypingStartPart::class, $data, true); + if (isset($data->member->user)) { + $this->cacheUser($data->member->user); + } + $deferred->resolve($typing); } } diff --git a/src/Discord/WebSockets/Events/VoiceStateUpdate.php b/src/Discord/WebSockets/Events/VoiceStateUpdate.php index 0e8695f87..34779dada 100644 --- a/src/Discord/WebSockets/Events/VoiceStateUpdate.php +++ b/src/Discord/WebSockets/Events/VoiceStateUpdate.php @@ -50,6 +50,8 @@ public function handle(Deferred &$deferred, $data): void $this->discord->guilds->offsetSet($state->guild->id, $state->guild); } + $this->cacheUser($data->member->user); + $deferred->resolve([$state, $old_state]); } }