refactor: reduce repetiton: add fluent conditional setter for users
Upon setting a user, common validation will be performed, thus sparing the other methods of having to do this all the time.
This commit is contained in:
parent
988e5c4579
commit
b23b32dc4c
@ -29,6 +29,7 @@ use Illuminate\Support\Facades\Auth;
|
|||||||
use Illuminate\Support\Facades\Crypt;
|
use Illuminate\Support\Facades\Crypt;
|
||||||
use Illuminate\Support\Facades\Http;
|
use Illuminate\Support\Facades\Http;
|
||||||
|
|
||||||
|
// Small wrapper for the necessary sections of the Discord API; A library is overkill here
|
||||||
class Discord
|
class Discord
|
||||||
{
|
{
|
||||||
|
|
||||||
@ -39,6 +40,15 @@ class Discord
|
|||||||
protected string $workingGuild;
|
protected string $workingGuild;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Current user.
|
||||||
|
*
|
||||||
|
* @var User The user all methods will affect.
|
||||||
|
*/
|
||||||
|
protected User $user;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public function __construct() {
|
public function __construct() {
|
||||||
if (!is_null($this->workingGuild)) {
|
if (!is_null($this->workingGuild)) {
|
||||||
$this->setWorkingGuild(config('services.discord.home_guild'));
|
$this->setWorkingGuild(config('services.discord.home_guild'));
|
||||||
@ -59,25 +69,42 @@ class Discord
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds sepcified user to specified guild. Bot must be member of target guild, and account must be linked
|
* Sets the current user, upon validation
|
||||||
*
|
*
|
||||||
* @param User $user
|
* @param User $user
|
||||||
|
* @return Discord
|
||||||
|
* @throws AccountNotLinkedException
|
||||||
|
*/
|
||||||
|
public function setUser(User $user): Discord
|
||||||
|
{
|
||||||
|
if ($user->hasDiscordConnection()) {
|
||||||
|
$this->user = $user;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new AccountNotLinkedException('Specified website user has not linked their Discord account yet.');
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds current working user to current working guild. Bot must be member of target guild, and account must be linked
|
||||||
|
*
|
||||||
* @return object|bool A GuildMember object; false if member is already in guild
|
* @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
|
* @throws RequestException Any client and server errors
|
||||||
* @see https://discord.com/developers/docs/resources/guild#guild-member-object
|
* @see https://discord.com/developers/docs/resources/guild#guild-member-object
|
||||||
*/
|
*/
|
||||||
public function addGuildMember(User $user): object|bool
|
public function addGuildMember(): object|bool
|
||||||
{
|
{
|
||||||
if ($user->hasDiscordConnection()) {
|
|
||||||
$params = [
|
$params = [
|
||||||
'access_token' => $user->discord_token
|
'access_token' => $this->user->discord_token
|
||||||
];
|
];
|
||||||
|
|
||||||
$member = Http::withBody(json_encode($params), 'application/json')
|
$member = Http::withBody(json_encode($params), 'application/json')
|
||||||
->withHeaders([
|
->withHeaders([
|
||||||
'Authorization' => 'Bot ' . config('services.discord.token')
|
'Authorization' => 'Bot ' . config('services.discord.token')
|
||||||
])->put(config('services.discord.base_url') . "/guilds/{$this->workingGuild}/members/{$user->discord_user_id}")
|
])->put(config('services.discord.base_url') . "/guilds/{$this->workingGuild}/members/{$this->user->discord_user_id}")
|
||||||
->throw();
|
->throw();
|
||||||
|
|
||||||
if ($member->successful() && $member->status() == 204) {
|
if ($member->successful() && $member->status() == 204) {
|
||||||
@ -87,29 +114,24 @@ class Discord
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new AccountNotLinkedException('Specified Discord user has not linked their account yet.');
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Bans a specified user from the guild.
|
* Bans a specified user from the guild.
|
||||||
* May be called from the suspension service optionally by the banning user
|
* 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
|
* @param string $reason The reason to supply Discord with
|
||||||
* @return void Nothing on success
|
* @return void Nothing on success
|
||||||
* @throws RequestException
|
* @throws RequestException
|
||||||
|
* @throws AccountNotLinkedException
|
||||||
*/
|
*/
|
||||||
public function addGuildBan(User $user, string $reason): void {
|
public function addGuildBan(string $reason): void {
|
||||||
|
Http::withHeaders([
|
||||||
if ($user->hasDiscordConnection()) {
|
|
||||||
$req = Http::withHeaders([
|
|
||||||
'Authorization' => 'Bot ' . config('services.discord.token'),
|
'Authorization' => 'Bot ' . config('services.discord.token'),
|
||||||
'X-Audit-Log-Reason' => $reason
|
'X-Audit-Log-Reason' => $reason
|
||||||
])->put(config('services.discord.base_url') . "/guilds/{$this->workingGuild}/bans/{$user->discord_user_id}")
|
])->put(config('services.discord.base_url') . "/guilds/{$this->workingGuild}/bans/{$this->user->discord_user_id}")
|
||||||
->throw();
|
->throw();
|
||||||
}
|
|
||||||
|
|
||||||
|
throw new AccountNotLinkedException('Specified website user has not linked their Discord account yet.');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user