Add discord patreon check
This commit is contained in:
@@ -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,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,10 +1,19 @@
|
||||
<?php
|
||||
|
||||
return [
|
||||
|
||||
'invite_link' => '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'),
|
||||
];
|
||||
|
||||
Reference in New Issue
Block a user