Skip to content

Commit

Permalink
DiscordUserUpdates
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshuaMicallefYBSU committed Oct 17, 2024
1 parent cbb0cd2 commit 4c7a3c8
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 32 deletions.
4 changes: 2 additions & 2 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
148 changes: 123 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 @@ -30,49 +31,146 @@ class DiscordAccountCheck implements ShouldQueue
public function handle()
{
// 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);
// dd($discord_members);

// foreach ($threads_data['threads'] as $thread) {
// if (strpos($thread['name'], $cid) !== false) {
// 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);

// Go through each Discord Member
foreach($discord_members as $discord_user){
// Get a complete list of Gander Oceanic Users
$users = User::all();

foreach($users_not_linked as $users){
// Loop through each DB User
foreach($users as $user){

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

// Variables to be compared
$name = $discord_user['nick'];
$cid = $users['id'];
// 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);

if(in_array('482816721280040964', $discord_member['roles'])){
continue;
}

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

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

//Supervisor?
if ($user->rating_short == 'SUP') {
array_push($rolesToAdd, $discordRoleIds['supervisor']);
}

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

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

$discord->assignRole($discord_user['user']['id'], '1278606316906090527');

// Add one role
$discord_not_linked++;
}
// Add Role to Users not Connected to Gander Oceanic
foreach($discord_uids as $discord_uid){

}


}

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

$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 4c7a3c8

Please sign in to comment.