From f90e469f411f71521ea1b0c79daf171d8d0a22df Mon Sep 17 00:00:00 2001 From: JoshuaMicallefYBSU <91457812+JoshuaMicallefYBSU@users.noreply.github.com> Date: Sun, 20 Oct 2024 15:30:49 +1000 Subject: [PATCH] Lets Update Discord! --- .../Controllers/DiscordTestController.php | 6 +- app/Jobs/DiscordAccountCheck.php | 145 +++++++++++------- 2 files changed, 95 insertions(+), 56 deletions(-) diff --git a/app/Http/Controllers/DiscordTestController.php b/app/Http/Controllers/DiscordTestController.php index 9ca115e3..b0e4881e 100644 --- a/app/Http/Controllers/DiscordTestController.php +++ b/app/Http/Controllers/DiscordTestController.php @@ -60,9 +60,11 @@ public function SendEmbed() $discord->sendMessageWithEmbed('1274827382250934365', 'Discord Account Not Linked on CZQO', -'A number of users within the Discord do not have their Discord linked with the Gander Oceanic Web Service. +'Dear Gander Members, -We ask that you head to the [Gander Oceanic Website](https://ganderoceanic.ca/my) to link your Discord Account. +If you are seeing this message, it means your discord account is not linked with our Gander Oceanic Website. As such, your access to the discord has been limited until you link your account. + +We ask that you head to the [Gander Oceanic Website](https://ganderoceanic.ca/my) to link your Discord Account. Your roles will be assigned at 0400z each day. This will assist with future upgrades to the Discord Infrastructure. diff --git a/app/Jobs/DiscordAccountCheck.php b/app/Jobs/DiscordAccountCheck.php index 784c7d3b..15815945 100644 --- a/app/Jobs/DiscordAccountCheck.php +++ b/app/Jobs/DiscordAccountCheck.php @@ -30,6 +30,12 @@ class DiscordAccountCheck implements ShouldQueue public function handle() { + // Timeout length (seconds) + ini_set('max_execution_time', 1200); + + // Script Start Time + $start_time = Carbon::now(); + // Initialise some variables $checked_users = 0; $accounts_not_linked = 0; @@ -68,6 +74,7 @@ public function handle() // Check if user is currently in Discord foreach($discord_uids as $discord_uid){ + if($user->discord_user_id == $discord_uid){ ## User is in the Discord @@ -75,57 +82,79 @@ public function handle() $discord_member = $discord->getClient()->get('guilds/'.env('DISCORD_GUILD_ID').'/members/'.$discord_uid); $discord_member = json_decode($discord_member->getBody(), true); - if(in_array('482816721280040964', $discord_member['roles'])){ - continue; + // Discord Account is Linked. Remove from Check + $key = array_search($user->discord_user_id, $discord_uids); + if ($key !== false) { + unset($discord_uids[$key]); } // Update DB information $user->member_of_czqo = 1; - $user->discord_username = $discord_member['username']; - $user->discord_avatar = $user->avatar ? 'https://cdn.discordapp.com/avatars/'.$user->discord_user_id.'/'.$discord_member['avatar'].'.png' : null; + $user->discord_username = $discord_member['user']['username']; + $user->discord_avatar = $user->avatar ? 'https://cdn.discordapp.com/avatars/'.$user->discord_user_id.'/'.$discord_member['user']['avatar'].'.png' : null; $user->save(); - - // Remove ID from the List - foreach ($discord_uids as $key => $discord_member) { - if ($discord_member == $user->discord_user_id) { - // Remove the matched ID from the array - unset($discord_uids[$key]); - - // Optional: break the loop once the match is found and removed - break; - } + + // Skip if User has 'Senior Staff' Role, skip as bot doesnt have power + if(in_array('482816721280040964', $discord_member['roles'])){ + continue; } - - // Update Discord Information - $rolesToAdd = []; - $discordRoleIds = [ - 'guest' => 482835389640343562, - 'training' => 482824058141016075, - 'certified' => 482819739996127259, - 'supervisor' => 720502070683369563, - ]; - - //Add the Member role - array_push($rolesToAdd, $discordRoleIds['guest']); - - //Roster? - if (RosterMember::where('user_id', $user->id)->exists()) { - //What status do they have? - $rosterProfile = RosterMember::where('user_id', $user->id)->first(); - switch ($rosterProfile->certification) { - case 'certified': - array_push($rolesToAdd, $discordRoleIds['certified']); - break; - case 'training': - array_push($rolesToAdd, $discordRoleIds['training']); - break; - } + + // Skip if User has 'Staff Member' Role, lets skip them for now as roles need to be finished + if(in_array('752767906768748586', $discord_member['roles'])){ + continue; } - //Supervisor? - if ($user->rating_short == 'SUP') { - array_push($rolesToAdd, $discordRoleIds['supervisor']); + // Roles Calculation + { + // Generic Roles users can have by default + $rolesToAdd = []; + $discordRoleIds = [ + 'guest' => 482835389640343562, + 'training' => 482824058141016075, + 'certified' => 482819739996127259, + 'supervisor' => 720502070683369563, + ]; + + //Add the Member role to each user + array_push($rolesToAdd, $discordRoleIds['guest']); + + //Roster Member? + if (RosterMember::where('user_id', $user->id)->exists()) { + //What status do they have? + $rosterProfile = RosterMember::where('user_id', $user->id)->first(); + switch ($rosterProfile->certification) { + case 'certified': + array_push($rolesToAdd, $discordRoleIds['certified']); + break; + case 'training': + array_push($rolesToAdd, $discordRoleIds['training']); + break; + } + } + + //Supervisor? + if ($user->rating_short == 'SUP') { + array_push($rolesToAdd, $discordRoleIds['supervisor']); + } + + // Check Assigned Discord Roles, and keep them assigned + $roleIdsToCheck = [ + 635449323089559572, //AFV Dev Role + 634656628335050762, //Shanwick Team + 497351197280174080, //VATCAN Divisional Staff + 497359834010615809, //VATSIM Senior Staff + 1257807978531389561]; //VATSYS Beta Tester + + foreach ($roleIdsToCheck as $roleId) { + if (in_array($roleId, $discord_member['roles'])) { + $rolesToAdd[] = $roleId; // Add the role ID to rolesToAdd if present in user's roles + } + } + + // dd($rolesToAdd); + } + // Update user $discord->getClient()->patch('guilds/'.env('DISCORD_GUILD_ID').'/members/'.$user->discord_user_id, [ @@ -145,30 +174,38 @@ public function handle() } + } - // Add Role to Users not Connected to Gander Oceanic - foreach($discord_uids as $discord_uid){ + // Add Role to Users not Connected to Gander Oceanic + foreach($discord_uids as $discord_uid){ + $accounts_not_linked++; //records that Account Not Linked Role Assigned - } - + sleep(3); + // add role + $discord->getClient()->put('guilds/'.env('DISCORD_GUILD_ID').'/members/'.$discord_uid.'/roles/1297422968472997908'); } - // Variables after looping - // dd($discord_uids); - // dd($discord_uid); - // dd($checked_users); - + // Record Information for Discord + // Beginning + $update_content = "Full list of functions completed this week for Discord Users, <@200426385863344129>"; + $update_content .= "\n\n **__Accounts:__**"; + // Users which are linked in Discord + $update_content .= "\n- Linked to Discord: ".$checked_users." (name/roles updated)"; - // Record Information for Discord + // Accounts not linked + $update_content .= "\n- Not Linked to Discord: ".$accounts_not_linked." (role assigned)"; - // Beginning - $update_content = "Full list of functions completed this week for Discord Users, <@200426385863344129>"; + // Completion Time + $end_time = Carbon::now(); + $seconds = $start_time->diffInSeconds($end_time); + $update_content .= "\n\n**__Script Time:__**"; + $update_content .= "\n- Script Time: ".$seconds." seconds."; $discord->sendMessageWithEmbed(env('DISCORD_WEB_LOGS'), 'WEEKLY: Discord User Update', $update_content); }