3 namespace App\DiscordBotCommands;
5 use App\Models\DiscordBotCommand;
8 use Discord\Parts\User\User as DiscordUser;
9 use Illuminate\Support\Facades\Http;
10 use Illuminate\Support\Facades\Storage;
12 class SyncUserCommand extends BaseCommand {
14 public function __construct(Discord $discord, DiscordBotCommand $cmd) {
15 parent::__construct($discord, $cmd);
18 public function execute() {
19 return $this->fetchUser()
20 ->then(function (DiscordUser $discordUser) {
21 $user = User::find($discordUser->id);
24 $user->id = $discordUser->id;
25 $user->username = $discordUser->username;
26 $user->discriminator = $discordUser->discriminator;
27 $user->locale = $discordUser->locale ?: 'en';
28 $user->verified = $discordUser->verified ?: false;
29 $user->mfa_enabled = $discordUser->mfaenabled ?: false;
31 $user->avatar = $discordUser->avatarhash ?: null;
32 if ($user->username != $discordUser->username) {
33 $user->username = $discordUser->username;
34 $user->discriminator = $discordUser->discriminator;
37 $media = Storage::disk('media');
38 if ($user->avatar && $media->missing('avatar/'.$user->id.'/'.$user->avatar.'.png')) {
40 $url = $discordUser->getAvatarAttribute('png');
41 $content = Http::get($url)->body();
42 $media->makeDirectory('avatar/'.$user->id);
43 $media->put('avatar/'.$user->id.'/'.$user->avatar.'.png', $content);
44 $user->avatar_cached = now();
45 } catch (\Exception $e) {
48 $user->avatar_cached = now();