From: Daniel Karbach Date: Mon, 20 Feb 2023 12:54:59 +0000 (+0100) Subject: add user-sync discord command X-Git-Url: https://git.localhorst.tv/?a=commitdiff_plain;h=3b4f0e5744d150c273bc450268d5a78ac43b2384;p=alttp.git add user-sync discord command --- diff --git a/app/DiscordBotCommands/BaseCommand.php b/app/DiscordBotCommands/BaseCommand.php index b043af2..e6861e0 100644 --- a/app/DiscordBotCommands/BaseCommand.php +++ b/app/DiscordBotCommands/BaseCommand.php @@ -9,6 +9,7 @@ use Discord\Discord; use Discord\Parts\Channel\Channel; use Discord\Parts\Guild\Guild; use Discord\Parts\User\Member; +use Discord\Parts\User\User as DiscordUser; use Illuminate\Support\Facades\App; abstract class BaseCommand { @@ -19,6 +20,8 @@ abstract class BaseCommand { return new PresenceCommand($discord, $cmd); case 'result': return new ResultCommand($discord, $cmd); + case 'sync-user': + return new SyncUserCommand($discord, $cmd); default: throw new Exception('unrecognized command'); } @@ -86,6 +89,18 @@ abstract class BaseCommand { }); } + protected function fetchUser() { + if (isset($this->user)) { + return \React\Promise\resolve($this->user); + } + return $this->discord->users + ->fetch($this->getParameter('user')) + ->then(function (DiscordUser $user) { + $this->user = $user; + return $user; + }); + } + protected function getParameter($name) { return $this->command->parameters[$name]; @@ -125,5 +140,6 @@ abstract class BaseCommand { protected $guild = null; protected $member = null; protected $roundChannel = null; + protected $user = null; } diff --git a/app/DiscordBotCommands/SyncUserCommand.php b/app/DiscordBotCommands/SyncUserCommand.php new file mode 100644 index 0000000..b9ba6d3 --- /dev/null +++ b/app/DiscordBotCommands/SyncUserCommand.php @@ -0,0 +1,48 @@ +fetchUser() + ->then(function (DiscordUser $discordUser) { + $user = User::find($discordUser->id); + if (!$user) { + $user = new User(); + $user->id = $discordUser->id; + $user->username = $discordUser->username; + $user->discriminator = $discordUser->discriminator; + $user->locale = $discordUser->locale ?: 'en'; + $user->verified = $discordUser->verified ?: false; + $user->mfa_enabled = $discordUser->mfaenabled ?: false; + } + $user->avatar = $discordUser->avatarhash ?: null; + + if ($user->avatar) { + try { + $url = $discordUser->getAvatarAttribute('png'); + $content = Http::get($url)->body(); + $media = Storage::disk('media'); + $media->makeDirectory('avatar/'.$user->id); + $media->put('avatar/'.$user->id.'/'.$discordUser->avatarhash.'.png', $content); + $user->avatar_cached = now(); + } catch (\Exception $e) { + } + } + $user->save(); + }); + } + +} diff --git a/app/Models/User.php b/app/Models/User.php index 8373b5d..879624a 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -215,6 +215,7 @@ class User extends Authenticatable 'discriminator' => 'string', 'email' => 'string', 'avatar' => 'string', + 'avatar_cached' => 'boolean', 'verified' => 'boolean', 'locale' => 'string', 'mfa_enabled' => 'boolean', diff --git a/database/migrations/2023_02_20_123827_add_user_avatar_cached.php b/database/migrations/2023_02_20_123827_add_user_avatar_cached.php new file mode 100644 index 0000000..3577998 --- /dev/null +++ b/database/migrations/2023_02_20_123827_add_user_avatar_cached.php @@ -0,0 +1,32 @@ +timestamp('avatar_cached')->nullable()->default(null); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('users', function(Blueprint $table) { + $table->dropColumn('avatar_cached'); + }); + } +};