Skip to content

Commit

Permalink
Auto Open Training Threads
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshuaMicallefYBSU committed Aug 18, 2024
1 parent edca530 commit 1f121ad
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
48 changes: 48 additions & 0 deletions app/Jobs/DiscordTrainingUpdates.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,54 @@ class DiscordTrainingUpdates implements ShouldQueue
*/
public function handle()
{
// Check all Training Threads are open and dont expire for one week
{
// Initialize the DiscordClient inside the handle method
$discord = new DiscordClient();

// Number of Messages Sent
$counter = 0;

// Get Active Threads
$response = $discord->getClient()->get('channels/'.env('DISCORD_TRAINING_FORUM').'/threads/archived/public');
$results = json_decode($response->getBody(), true);

// dd($results);

foreach($results['threads'] as $thread){
$archiveTimestamp = Carbon::parse($thread['thread_metadata']['archive_timestamp']);

// Thread was closed within the last 10 days
if ($archiveTimestamp >= Carbon::now()->subDays(10) && $archiveTimestamp <= Carbon::now()) {
// Your code to handle the condition
// dd($archiveTimestamp);

// Get the ID of the Training Thread Recently Closed
if (preg_match('/\d+$/', $thread['name'], $matches)) {
$cid = $matches[0];
} else {
$cid = null;
}

// See if CID is still a student
$student = Student::where('user_id', $cid)->first();
if($student !== null){

// Thread should be active, so lets activate it.
$discord = new DiscordClient();
$data = $discord->getClient()->patch('channels/'.$thread['id'], [
'json' => [
'locked' => false,
'archived' => false,
]
]);
}
}
}

$discord->sendMessageWithEmbed(env('DISCORD_WEB_LOGS'), 'AUTO: Training Thread Opened',$counter. ' Threads were automatically reopended as they had expired (more than 1 week since last activity)');
}

// Function for Training Thread Availability Updates
{
// Initialize the DiscordClient inside the handle method
Expand Down
1 change: 1 addition & 0 deletions app/Services/DiscordClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ public function createTrainingThread($name, $user)
$response = $this->client->post("channels/".env('DISCORD_TRAINING_FORUM')."/threads", [
'json' => [
'name' => $name,
'auto_archive_duration' => 20160,
'applied_tags' => [1271845980865695774], //Tag ID for 'New Request'
'message' => [
'content' => $user.', your application has now been approved. Welcome to Gander Oceanic!
Expand Down

0 comments on commit 1f121ad

Please sign in to comment.