From 6adf30e5d96f9f89f9aad25a7832e169979718aa Mon Sep 17 00:00:00 2001 From: Daniel Trolezi Date: Wed, 25 Sep 2024 15:20:52 -0300 Subject: [PATCH] Fix Discord Interactions --- app/Http/Controllers/DiscordController.php | 6 -- .../Requests/DiscordInteractionRequest.php | 68 +++++++++++-------- .../Discord/DiscordInteractionsService.php | 16 +++++ 3 files changed, 57 insertions(+), 33 deletions(-) diff --git a/app/Http/Controllers/DiscordController.php b/app/Http/Controllers/DiscordController.php index d058a00..62b76a3 100644 --- a/app/Http/Controllers/DiscordController.php +++ b/app/Http/Controllers/DiscordController.php @@ -3,15 +3,12 @@ namespace App\Http\Controllers; use App\Http\Requests\DiscordInteractionRequest; -use App\Services\Discord\DiscordAppService; use App\Services\Discord\DiscordInteractionsService; use Illuminate\Http\JsonResponse; -use Illuminate\Support\Facades\Auth; class DiscordController extends Controller { public function __construct( - private DiscordAppService $appService, private DiscordInteractionsService $interactionsService ) { } @@ -20,9 +17,6 @@ public function interactions(DiscordInteractionRequest $request): JsonResponse { $payload = $request->validated(); - $user = $this->appService->findOrCreateUser($payload); - Auth::setUser($user); - return response()->json( $this->interactionsService->handleInteractions($payload) ); diff --git a/app/Http/Requests/DiscordInteractionRequest.php b/app/Http/Requests/DiscordInteractionRequest.php index a8ee085..ac66733 100644 --- a/app/Http/Requests/DiscordInteractionRequest.php +++ b/app/Http/Requests/DiscordInteractionRequest.php @@ -24,45 +24,59 @@ public function authorize(): bool */ public function rules(): array { - $rules = $this->getRulesForInteractionType( - InteractionType::from($this->get('type')) - ); + $rules = [ + 'type' => 'required|integer|in:' . InteractionType::valuesAsString(), + 'user' => 'required|array', + 'user.id' => 'required|string', + 'user.username' => 'required|string', + 'user.global_name' => 'required|string', + ]; - return array_merge([ - 'type' => 'required|integer|in:' . InteractionType::valuesAsString(), + $additionalRules = match ($this->get('type')) { + InteractionType::Command->value => $this->getCommandRules(), + InteractionType::MessageComponent->value => $this->getMessageComponentRules(), + default => [] + }; + + return array_merge($rules, $additionalRules); + } + + /** + * @return array + */ + private function getCommandRules(): array + { + return [ + 'channel.id' => 'required|string', 'data' => 'required|array', + 'data.type' => 'required|int', + 'data.name' => 'required|string', 'data.options' => 'sometimes|array', 'data.options.*' => 'required|array', - 'data.options.*.value' => 'required', - 'user' => 'required|array', - 'user.id' => 'required|string', - 'user.username' => 'required|string', - 'user.global_name' => 'required|string', - 'channel.id' => 'required|string', - 'message.components' => 'sometimes|array' - ], $rules); + 'data.options.*.value' => 'required' + ]; } /** - * @param InteractionType $type * @return array */ - private function getRulesForInteractionType(InteractionType $type): array + private function getMessageComponentRules(): array { - return match ($type) { - InteractionType::Command => [ - 'data.type' => 'required|int', - 'data.name' => 'required|string', - ], - InteractionType::MessageComponent => [ - 'data.component_type' => 'required|int|in:' . ComponentType::valuesAsString(), - 'data.custom_id' => 'required|string', - 'data.values' => 'sometimes|array', - ], - default => [] - }; + return [ + 'channel.id' => 'required|string', + 'data' => 'required|array', + 'data.component_type' => 'required|int|in:' . ComponentType::valuesAsString(), + 'data.custom_id' => 'required|string', + 'data.values' => 'sometimes|array', + 'message.components' => 'sometimes|array' + ]; } + /** + * @param [type] $key + * @param [type] $default + * @return array + */ #[Override] public function validated($key = null, $default = null): array { diff --git a/app/Services/Discord/DiscordInteractionsService.php b/app/Services/Discord/DiscordInteractionsService.php index d9389db..0ea3df1 100644 --- a/app/Services/Discord/DiscordInteractionsService.php +++ b/app/Services/Discord/DiscordInteractionsService.php @@ -8,6 +8,7 @@ use App\Services\Discord\Commands\Contracts\CommandInterface; use App\Services\Discord\Utils\DiscordCallbackUtils; use Illuminate\Support\Arr; +use Illuminate\Support\Facades\Auth; use Illuminate\Support\Str; use InvalidArgumentException; @@ -21,6 +22,10 @@ class DiscordInteractionsService extends DiscordBaseService */ public function handleInteractions(array $payload): array { + if ($payload['type'] !== InteractionType::Ping) { + $this->authenticateUser($payload); + } + switch ($payload['type']) { case InteractionType::Ping: return $this->makeResponse( @@ -44,6 +49,17 @@ public function handleInteractions(array $payload): array } } + /** + * @param array $payload + * @return void + */ + private function authenticateUser(array $payload): void + { + Auth::setUser( + resolve(DiscordAppService::class)->findOrCreateUser($payload) + ); + } + /** * @param string $command * @param array $payload