Skip to content

Commit

Permalink
Discord Roles Fixup
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshuaMicallefYBSU committed Aug 22, 2024
1 parent 6ac63e9 commit bc4208c
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 8 deletions.
15 changes: 15 additions & 0 deletions app/Http/Controllers/DiscordTestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,19 @@ public function SendEmbed()
If you would like to begin training again, please re-apply via the Gander Website.');
}

public function DiscordRoles()
{
$discord = new DiscordClient();

//Get role ID based off status
$roles = [
'certified' => 482819739996127259,
'student' => 482824058141016075,
];

$discord->removeRole(200426385863344129, $roles['student']);

dd($discord);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Contracts\Queue\ShouldBeUnique;

class ProcessDiscordRoles implements ShouldQueue
class ProcessDiscordRolesOld implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public $tries = 3;
Expand Down
38 changes: 32 additions & 6 deletions app/Services/DiscordClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,16 +105,42 @@ public function ControllerDisconnect($id, $callsign, $name, $connect_time, $tota

public function assignRole($discordId, $roleId)
{
ProcessDiscordRoles::dispatch(true, $discordId, $roleId);

return;
sleep(1);

try {
$client = new Client();
$client->request('PUT', "https://discord.com/api/v10/guilds/".env('DISCORD_GUILD_ID')."/members/" . $discordId . "/roles/" . $roleId, [
'headers' => [
'Authorization' => 'Bot ' . env('DISCORD_BOT_TOKEN'),
'Content-Type' => 'application/json'
],
]);
} catch (\Exception $e) {
if ($e->getCode() !== 404) {
throw $e;
}
return;
}
}

public function removeRole($discordId, $roleId)
{
ProcessDiscordRoles::dispatch(false, $discordId, $roleId);

return;
sleep(2);

try {
$client = new Client();
$client->request('DELETE', "https://discord.com/api/v10/guilds/".env('DISCORD_GUILD_ID')."/members/" . $discordId . "/roles/" . $roleId, [
'headers' => [
'Authorization' => 'Bot ' . env('DISCORD_BOT_TOKEN'),
'Content-Type' => 'application/json'
],
]);
} catch (\Exception $e) {
if ($e->getCode() !== 404) {
throw $e;
}
return;
}
}

public function changeName($userId, $name)
Expand Down
2 changes: 1 addition & 1 deletion routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@

// Discord shortcut
Route::get('/discord', [DiscordController::class, 'joinShortcut']);
Route::get('/discord/function-test', [DiscordTestController::class, 'Job']);
Route::get('/discord/function-test', [DiscordTestController::class, 'DiscordRoles']);

// Public news articles
Route::get('/news/{id}', [NewsController::class, 'viewArticlePublic'])->name('news.articlepublic')->where('id', '[0-9]+');
Expand Down

0 comments on commit bc4208c

Please sign in to comment.