Skip to content

Commit

Permalink
Merge pull request #336 from czqoocavatsim/JoshuaBranch
Browse files Browse the repository at this point in the history
Few Little Updates
  • Loading branch information
JoshuaMicallefYBSU authored Aug 30, 2024
2 parents 1738d07 + cbcbcd0 commit 929146f
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 24 deletions.
4 changes: 2 additions & 2 deletions app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ protected function schedule(Schedule $schedule)
$schedule->job(new ProcessSessionLogging())->everyMinute();

//Inactivity checks
$schedule->job(new ProcessRosterInactivity())->cron('00 00 01 JAN,APR,JUL,OCT *');
$schedule->job(new ProcessRosterInactivity())->dailyAt('2:10');

//CRONS FOR INACTIVITY EMAILS 2 weeks
// $schedule->call(function () {
Expand Down Expand Up @@ -111,7 +111,7 @@ protected function schedule(Schedule $schedule)
$schedule->job(new DiscordTrainingWeeklyUpdates())->weeklyOn(6, '6:00');

// Check If Account is Linked
$schedule->job(new DiscordAccountCheck)->dailyAt('7:10');
$schedule->job(new DiscordAccountCheck)->weeklyOn(5, '0:30');
}

/**
Expand Down
20 changes: 15 additions & 5 deletions app/Http/Controllers/DiscordTestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Illuminate\Http\Request;
use App\Services\DiscordClient;
use App\Jobs\DiscordAccountCheck;
use App\Jobs\ProcessRosterInactivity;

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

// Call the handle method directly to get the result synchronously
$result = $job->handle();
Expand All @@ -42,11 +42,15 @@ public function SendEmbed()
//New Applicant in Instructor Channel
$discord = new DiscordClient();

$discord->sendMessageWithEmbed('1273228164255977492', 'Oceanic Training Cancelled!',
$discord->sendMessageWithEmbed('1274827382250934365', 'Discord Account Not Linked on CZQO',

'Your training request with Gander Oceanic has been terminated.
'A number of users within the Discord do not have their Discord linked with the Gander Oceanic Web Service.
If you would like to begin training again, please re-apply via the Gander Website.');
We ask that you head to the [Gander Oceanic Website](https://ganderoceanic.ca/my) to link your Discord Account.
This will assist with future upgrades to the Discord Infrastructure.
<@&1278606316906090527>');
}

public function DiscordRoles()
Expand All @@ -63,4 +67,10 @@ public function DiscordRoles()

dd($discord);
}

public function sendMessage()
{
$discord = new DiscordClient();
$discord->sendDM('200426385863344129', 'Test message');
}
}
6 changes: 6 additions & 0 deletions app/Jobs/DiscordAccountCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,16 @@ public function handle()
$discord = new DiscordClient();

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

// Add one role
$discord_not_linked++;
}

}
}

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

}
54 changes: 39 additions & 15 deletions app/Jobs/ProcessRosterInactivity.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Jobs;

use App\Models\Roster\RosterMember;
use App\Models\Network\SessionLog;
use Carbon\Carbon;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
Expand Down Expand Up @@ -38,28 +39,51 @@ public function __construct()
*/
public function handle()
{
$rosterMembers = RosterMember::whereNotIn('certification', ['not_certified', 'training'])->get();
$roster_controllers = RosterMember::all();

foreach ($rosterMembers as $rosterMember) {
// Lets now go through each RosterMember (gotta update dem all)
foreach($roster_controllers as $roster){

$date = false;
// Get date certified
try {
$certifiedDate = Carbon::createFromFormat('Y-m-d H:i:s', $rosterMember->date_certified);
} catch (\InvalidArgumentException $e) { // Catch exception if date is null
$date = true;
// get sessions within the last 12 months
$sessions = SessionLog::where('cid', $roster->cid)->where('created_at', '>=', Carbon::now()->subMonths(12))->orderBy('created_at', 'asc')->get();
$last_session = SessionLog::where('cid', $roster->cid)->orderBy('created_at', 'desc')->first();
// dd($last_session);

// Set some variables (default values)
$currency = 0; //Assume no connections
$active_status = 1; //Assume roster member is active

// Go through each session to get some information
foreach($sessions as $s){
$currency += $s->duration;
}

if ($date === false){
if ($certifiedDate > Carbon::now()->subDays(2)->startOfQuarter() && $certifiedDate < Carbon::now()->subDays(2)->endOfQuarter()){
continue;
// Currency is less than 1 hour and last connection was 305 days ago.
if($roster->active && $currency < 1 && $last_session->created_at->diffInDays(now()) >= 305){
$active_status = 0;

// Send Message to user that they have been
if($roster->user->member_of_czqo && $roster->user->hasDiscord()){
$discord = new DiscordClient();
$discord->sendDM($roster->user->discord_user_id, 'Roster Status set as Inactive', 'Hello!
Your status has been set as Inactive with Gander Oceanic. This is because you have not controlled at least one hour, within the last 305 days.
You have 60 days to control at least one hour, otherwise you will be removed as a certified controller.
If you have any questions, please reach out to the Gander Oceanic team on our Discord
**Regards,
Gander Oceanic**');

}
}

if ($rosterMember->active && $rosterMember->currency < 3) {
$rosterMember->active = false;
$rosterMember->save();
}
// Save Roster Information
$roster->active = $active_status;
$roster->currency = $currency;
$roster->save();
}

}
}
23 changes: 21 additions & 2 deletions app/Jobs/ProcessSessionLogging.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public function handle()
if (in_array($session->cid, $allRoster)) {
// Controller is authorised, send message if discord_id is not set
if($session->discord_id == null){
// Discord Message
try{
$discord = new DiscordClient();
$discord_id = $discord->ControllerConnection($controller->callsign, $name);
Expand All @@ -88,6 +89,13 @@ public function handle()
$discord = new DiscordClient();
$discord->sendMessageWithEmbed(env('DISCORD_WEB_LOGS'), 'Discord Controller Connect Error', $e->getMessage());
}

// Add Discord Role
if($log->user && $log->user->hasDiscord() && $log->user->member_of_czqo){
$discord = new DiscordClient();

$discord->assignRole($log->user->discord_user_id, '1278868454606377040');
}
}
} else {
// Controller is not authorised. Let Senior Team know.
Expand Down Expand Up @@ -116,6 +124,7 @@ public function handle()
//Check existing sessions in db
$sessionLogs = SessionLog::whereNull('session_end')->get();
foreach ($sessionLogs as $log) {
// Controller has now disconnected
if ((!in_array($log->callsign, $positionsFound)) || $vatsimData->searchCallsign($log->callsign, true)->cid != $log->cid) {
$log->session_end = Carbon::now();
$log->duration = $log->session_start->floatDiffInMinutes(Carbon::now()) / 60;
Expand All @@ -129,24 +138,34 @@ public function handle()
$name = $log->cid;
}

// Discord ID is not null (message has not yet been sent)
if($log->discord_id !== null){
// Update Disconnect Message
try{
$discord = new DiscordClient();
$data = $discord->ControllerDisconnect($log->discord_id, $log->callsign, $name, $log->session_start, $log->duration);
} catch (\Exception $e) {
$discord = new DiscordClient();
$discord->sendMessageWithEmbed(env('DISCORD_WEB_LOGS'), 'Discord Controller Disconnect Error', $e->getMessage());
}

// Remove Discord Role
if($log->user && $log->user->hasDiscord() && $log->user->member_of_czqo){
$discord = new DiscordClient();

$discord->removeRole($log->user->discord_user_id, '1278868454606377040');
}
}

$log->discord_id = null;
$log->save;

//If there is an associated roster member, give them the hours
//If there is an associated roster member, give them the hours and set as active
if ($rosterMember = $log->rosterMember) {
if (($rosterMember->certification == 'certified' || $rosterMember->certification == 'training') && $rosterMember->active) {
if (($rosterMember->certification == 'certified' || $rosterMember->certification == 'training')) {
$rosterMember->currency += $log->session_start->floatDiffInMinutes(Carbon::now()) / 60;
$rosterMember->monthly_hours += $log->session_start->floatDiffInMinutes(Carbon::now()) / 60;
$rosterMember->active = 1;
$rosterMember->save();
}
}
Expand Down
15 changes: 15 additions & 0 deletions app/Services/DiscordClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,21 @@ public function sendMessage($channelId, $message)
return $response->getStatusCode() == 200;
}

public function sendDM($userId, $title, $message)
{
$response = $this->client->post("https://discord.com/api/v10/users/@me/channels", [
'json' => [
'recipient_id' => $userId, // Replace $userId with the Discord user ID
],
]);

$channel = json_decode($response->getBody(), true);
$channelId = $channel['id'];

// Step 2: Send the Message to the DM Channel
$this->sendMessageWithEmbed($channelId, $title, $message);
}

public function sendMessageWithEmbed($channelId, $title, $description)
{
$response = $this->client->post("channels/{$channelId}/messages", [
Expand Down

0 comments on commit 929146f

Please sign in to comment.