Skip to content

Commit

Permalink
Fix Discord Interactions
Browse files Browse the repository at this point in the history
  • Loading branch information
danieltrolezi committed Sep 25, 2024
1 parent e7f0205 commit 6adf30e
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 33 deletions.
6 changes: 0 additions & 6 deletions app/Http/Controllers/DiscordController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
) {
}
Expand All @@ -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)
);
Expand Down
68 changes: 41 additions & 27 deletions app/Http/Requests/DiscordInteractionRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
16 changes: 16 additions & 0 deletions app/Services/Discord/DiscordInteractionsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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(
Expand All @@ -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
Expand Down

0 comments on commit 6adf30e

Please sign in to comment.