-
-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into ticket-achievement-delete-fix
- Loading branch information
Showing
28 changed files
with
592 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Actions; | ||
|
||
use App\Models\User; | ||
|
||
class FindUserByIdentifierAction | ||
{ | ||
public function execute(?string $identifier): ?User | ||
{ | ||
if ($identifier === null) { | ||
return null; | ||
} | ||
|
||
$ulidLength = 26; | ||
if (mb_strlen($identifier) === $ulidLength) { | ||
return User::whereUlid($identifier)->first(); | ||
} | ||
|
||
return User::query() | ||
->where(function ($query) use ($identifier) { | ||
$query->where('display_name', $identifier) | ||
->orWhere('User', $identifier); | ||
}) | ||
->first(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Http\Actions; | ||
|
||
use GuzzleHttp\Client; | ||
use Illuminate\Support\Facades\Log; | ||
use Throwable; | ||
|
||
class UpdateDiscordNicknameAction | ||
{ | ||
private Client $client; | ||
private string $botToken; | ||
private string $guildId; | ||
|
||
public function __construct() | ||
{ | ||
$this->client = new Client(); | ||
$this->botToken = config('services.discord.rabot_token'); | ||
$this->guildId = config('services.discord.guild_id'); | ||
} | ||
|
||
public function execute(string $oldUsername, string $newUsername): void | ||
{ | ||
if (!$this->botToken || !$this->guildId) { | ||
return; | ||
} | ||
|
||
try { | ||
$member = $this->findMemberByNickname($oldUsername); | ||
if (!$member) { | ||
return; | ||
} | ||
|
||
$this->updateUserNickname($member['user']['id'], $newUsername); | ||
} catch (Throwable $e) { | ||
Log::error("Failed to update Discord nickname: " . $e->getMessage()); | ||
} | ||
} | ||
|
||
private function findMemberByNickname(string $nickname): ?array | ||
{ | ||
$response = $this->client->get( | ||
"https://discord.com/api/v10/guilds/{$this->guildId}/members/search", | ||
[ | ||
'headers' => [ | ||
'Authorization' => "Bot {$this->botToken}", | ||
], | ||
'query' => ['query' => $nickname], | ||
] | ||
); | ||
|
||
$members = json_decode($response->getBody()->getContents(), true); | ||
|
||
// Find a case-insensitive match. | ||
foreach ($members as $member) { | ||
$memberNick = $member['nick'] ?? $member['user']['username']; | ||
if (strcasecmp($memberNick, $nickname) === 0) { | ||
return $member; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
private function updateUserNickname(string $userId, string $newNickname): void | ||
{ | ||
$this->client->patch( | ||
"https://discord.com/api/v10/guilds/{$this->guildId}/members/{$userId}", | ||
[ | ||
'headers' => [ | ||
'Authorization' => "Bot {$this->botToken}", | ||
], | ||
'json' => ['nick' => $newNickname], | ||
] | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.