athenahr/app/Helpers/Discord.php

117 lines
3.7 KiB
PHP
Raw Normal View History

<?php
/*
* Copyright © 2020 Miguel Nogueira
*
* This file is part of Raspberry Staff Manager.
*
* Raspberry Staff Manager is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Raspberry Staff Manager is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Raspberry Staff Manager. If not, see <https://www.gnu.org/licenses/>.
*/
namespace App\Helpers;
use App\Exceptions\AccountNotLinkedException;
use App\User;
use Illuminate\Http\Client\RequestException;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Http;
class Discord
{
/**
* The current working guild. Default is Home guild from app config
* @var string
*/
protected string $workingGuild;
public function __construct() {
if (!is_null($this->workingGuild)) {
$this->setWorkingGuild(config('services.discord.home_guild'));
}
}
/**
* Sets the working guild
*
* @param string $workingGuild
* @return Discord
*/
public function setWorkingGuild(string $workingGuild): Discord
{
$this->workingGuild = $workingGuild;
return $this;
}
/**
* Adds sepcified user to specified guild. Bot must be member of target guild, and account must be linked
*
* @param User $user
* @return object|bool A GuildMember object; false if member is already in guild
* @throws AccountNotLinkedException Thrown when user didn't link/create their account yet
* @throws RequestException Any client and server errors
* @see https://discord.com/developers/docs/resources/guild#guild-member-object
*/
public function addGuildMember(User $user): object|bool
{
if ($user->hasDiscordConnection()) {
$params = [
'access_token' => $user->discord_token
];
$member = Http::withBody(json_encode($params), 'application/json')
->withHeaders([
'Authorization' => 'Bot ' . config('services.discord.token')
])->put(config('services.discord.base_url') . "/guilds/{$this->workingGuild}/members/{$user->discord_user_id}")
->throw();
if ($member->successful() && $member->status() == 204) {
return false;
} else {
return (object) $member->json();
}
}
throw new AccountNotLinkedException('Specified Discord user has not linked their account yet.');
}
/**
* Bans a specified user from the guild.
* May be called from the suspension service optionally by the banning user
*
* @param User $user The user to ban from the Guild
* @param string $reason The reason to supply Discord with
* @return void Nothing on success
* @throws RequestException
*/
public function addGuildBan(User $user, string $reason): void {
if ($user->hasDiscordConnection()) {
$req = Http::withHeaders([
'Authorization' => 'Bot ' . config('services.discord.token'),
'X-Audit-Log-Reason' => $reason
])->put(config('services.discord.base_url') . "/guilds/{$this->workingGuild}/bans/{$user->discord_user_id}")
->throw();
}
}
}