From c13d4436962328a03cf81c6e48ba635a97194b65 Mon Sep 17 00:00:00 2001 From: w33b Date: Wed, 7 Jan 2026 19:04:51 +0100 Subject: [PATCH] Add discord patreon check --- .../Auth/DiscordAuthController.php | 61 +++++++++++++++++++ config/discord.php | 13 +++- 2 files changed, 72 insertions(+), 2 deletions(-) diff --git a/app/Http/Controllers/Auth/DiscordAuthController.php b/app/Http/Controllers/Auth/DiscordAuthController.php index bc34d20..1a869a4 100644 --- a/app/Http/Controllers/Auth/DiscordAuthController.php +++ b/app/Http/Controllers/Auth/DiscordAuthController.php @@ -8,6 +8,8 @@ use App\Http\Controllers\Controller; use Illuminate\Http\RedirectResponse; use Illuminate\Support\Str; use Illuminate\Support\Facades\Auth; +use Illuminate\Support\Facades\Http; +use Illuminate\Support\Facades\Log; use Laravel\Socialite\Facades\Socialite; class DiscordAuthController extends Controller @@ -52,9 +54,68 @@ class DiscordAuthController extends Controller } } + $this->checkDiscordRoles($user); + Auth::login($user, true); return redirect()->route('home.index'); } + /** + * Check Discord Roles if user is Patreon member + */ + private function checkDiscordRoles(User $user): void + { + // Should not ever happen + if (!$user->discord_id) { + return; + } + + $guildId = config('discord.guild_id'); + + $response = Http::withToken(config('discord.discord_bot_token'), 'Bot') + ->timeout(5) + ->get("https://discord.com/api/v10/guilds/{$guildId}/members/{$user->discord_id}"); + + // User is not in the guild + if ($response->status() === 404) { + $user->update([ + 'is_patreon' => false, + ]); + + return; + } + + // Something else failed + if ($response->failed()) { + Log::warning('Discord role check failed', [ + 'user_id' => $user->id, + 'discord_id' => $user->discord_id, + 'status' => $response->status(), + 'body' => $response->body(), + ]); + + return; + } + + $discordRoles = $response->json('roles', []); + $patreonRoles = config('discord.patreon_roles', []); + + $isPatreon = false; + foreach($patreonRoles as $patreonRole) + { + if (in_array($patreonRole, $discordRoles, true)) { + $isPatreon = true; + break; + } + } + + // Only update if something actually changed + if ($user->is_patreon !== $isPatreon) { + $user->update([ + 'is_patreon' => $isPatreon, + ]); + } + } + } diff --git a/config/discord.php b/config/discord.php index 8d32b62..bac3bad 100644 --- a/config/discord.php +++ b/config/discord.php @@ -1,10 +1,19 @@ 'https://discord.gg/yAqgVKNgG5', 'guild_id' => 802233383710228550, - 'patreon_roles' => [841798154999169054, 803329707650187364, 803327903659196416, 803325441942356059, 803322725576736858, 802270568912519198, 802234830384267315], + 'patreon_roles' => [ + 841798154999169054, // ???? + 803329707650187364, // Tier-5 + 803327903659196416, // ???? + 803325441942356059, // Tier-3 + 803322725576736858, // Tier-2 + 802270568912519198, // Tier-1 + 802234830384267315 // admin + ], + + 'discord_bot_token' => env('DISCORD_BOT_TOKEN'), ];