+<?php
+
+namespace App\DiscordBotCommands;
+
+use App\Models\DiscordBotCommand;
+use App\Models\User;
+use Discord\Discord;
+use Discord\Parts\User\User as DiscordUser;
+use Illuminate\Support\Facades\Http;
+use Illuminate\Support\Facades\Storage;
+
+class SyncUserCommand extends BaseCommand {
+
+ public function __construct(Discord $discord, DiscordBotCommand $cmd) {
+ parent::__construct($discord, $cmd);
+ }
+
+ public function execute() {
+ return $this->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();
+ });
+ }
+
+}