Skip to content

Commit

Permalink
Merge pull request #356 from czqoocavatsim/JoshuaBranch
Browse files Browse the repository at this point in the history
DiscordUpdate
  • Loading branch information
JoshuaMicallefYBSU authored Oct 20, 2024
2 parents a9e1a7c + f90e469 commit 8a73f2e
Show file tree
Hide file tree
Showing 3 changed files with 171 additions and 34 deletions.
10 changes: 6 additions & 4 deletions app/Http/Controllers/DiscordTestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Illuminate\Http\Request;
use App\Services\DiscordClient;
use Illuminate\Support\Facades\Http;
use App\Jobs\DiscordTrainingWeeklyUpdates;
use App\Jobs\DiscordAccountCheck;
use App\Jobs\ProcessShanwickController;

class DiscordTestController extends Controller
Expand All @@ -28,7 +28,7 @@ public function EditTagTest()
public function Job()
{
// Dispatch the job
$job = DiscordTrainingWeeklyUpdates::dispatch();
$job = DiscordAccountCheck::dispatch();

// Call the handle method directly to get the result synchronously
$result = $job->handle();
Expand Down Expand Up @@ -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.
Expand Down
185 changes: 160 additions & 25 deletions app/Jobs/DiscordAccountCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use App\Services\DiscordClient;
use App\Models\Training\Instructing\Records\TrainingSession;
use App\Models\Users\User;
use App\Models\Roster\RosterMember;
use App\Notifications\Training\Instructing\RemovedAsStudent;
use App\Models\Training\Instructing\Students\StudentStatusLabel;
use App\Models\Training\Instructing\Links\StudentStatusLabelLink;
Expand All @@ -29,50 +30,184 @@ 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
$discord_not_linked = 0;
$checked_users = 0;
$accounts_not_linked = 0;
$discord_uids = [];

// Get List of Users in Discord
$discord = new DiscordClient();
$response = $discord->getClient()->get('guilds/'.env('DISCORD_GUILD_ID').'/members?limit=1000');
// $response = $discord->getClient()->get('guilds/'.env('DISCORD_GUILD_ID').'/members');
$discord_members = json_decode($response->getBody(), true);
// $discord_members[0]['nick'] = "Joshua Micallef 1342084";

// dd($discord_members);

// Get All Users where member_of_czqo = 0 & used_connect = 1
$users_not_linked = User::where('member_of_czqo', 0)->where('used_connect', 1)->get()->toArray();
// dd($users_not_linked);
// Loop through each Discord User and get some key information
foreach($discord_members as $members){
$discord_uids[] = $members['user']['id'];
// dd($members['user']);
}
// dd($discord_uids);

// foreach ($threads_data['threads'] as $thread) {
// if (strpos($thread['name'], $cid) !== false) {
// Get a complete list of Gander Oceanic Users
$users = User::all();

// Go through each Discord Member
foreach($discord_members as $discord_user){
// Loop through each DB User
foreach($users as $user){

foreach($users_not_linked as $users){
// Skip is discord_user_id is null
if($user->discord_user_id == null){
continue;
} else {
$checked_users++;
}

// dd($users);
// Add a Sleep Timer - Allows API to not block
sleep(2);

// 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

// Get Discord Member Information
$discord_member = $discord->getClient()->get('guilds/'.env('DISCORD_GUILD_ID').'/members/'.$discord_uid);
$discord_member = json_decode($discord_member->getBody(), true);

// 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['user']['username'];
$user->discord_avatar = $user->avatar ? 'https://cdn.discordapp.com/avatars/'.$user->discord_user_id.'/'.$discord_member['user']['avatar'].'.png' : null;
$user->save();

// Skip if User has 'Senior Staff' Role, skip as bot doesnt have power
if(in_array('482816721280040964', $discord_member['roles'])){
continue;
}

// 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;
}

// 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, [
'json' => [
'nick' => $user->FullName('FLC'),
'roles' => $rolesToAdd,
]
]);

} else {
## User is NOT in the discord

// Update DB Information
$user->member_of_czqo = 0;
$user->save();
}

// Variables to be compared
$name = $discord_user['nick'];
$cid = $users['id'];
}

// CID matches ID of user not linked
if (strpos($name, $cid) !== false) {
$discord = new DiscordClient();
}

$discord->assignRole($discord_user['user']['id'], '1278606316906090527');
// 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

// Add one role
$discord_not_linked++;
}
sleep(3);

}
// add role
$discord->getClient()->put('guilds/'.env('DISCORD_GUILD_ID').'/members/'.$discord_uid.'/roles/1297422968472997908');
}

// Tell the log chat
$discord->sendMessageWithEmbed(env('DISCORD_WEB_LOGS'), 'AUTO: Users Not Linked',$discord_not_linked. ' members are not linked with CZQO. Role has been assigned');


// 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)";

// Accounts not linked
$update_content .= "\n- Not Linked to Discord: ".$accounts_not_linked." (role assigned)";

// 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);
}

}
10 changes: 5 additions & 5 deletions app/Jobs/DiscordTrainingWeeklyUpdates.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,14 +185,14 @@ public function handle()
// SendEmbed to ask student to send availability
$discord->sendEmbedInTrainingThread($cid, "Exam Not Requested", 'Hello, <@'.$student_exam->user->discord_user_id.'>
Our records indicate that you have not requested, or completed your exam within a month of your Application being approved.
Our records indicate that you have not requested, or completed your exam within a month of your Application being approved.
Please read the above message in order to understand how to request the exam.
Please read the above message in order to understand how to request the exam.
Should you not request, and pass the exam within 60 days of your application being accepted, your training will be automatically terminated, and you will need to reapply to begin your training once more.
Should you not request, and pass the exam within 60 days of your application being accepted, your training will be automatically terminated, and you will need to reapply to begin your training once more.
**Kind Regards,
Gander Oceanic Training Team**');
**Kind Regards,
Gander Oceanic Training Team**');
}
}

Expand Down

0 comments on commit 8a73f2e

Please sign in to comment.