]> git.localhorst.tv Git - alttp.git/blob - app/DiscordBotCommands/SyncUserCommand.php
better chat filters
[alttp.git] / app / DiscordBotCommands / SyncUserCommand.php
1 <?php
2
3 namespace App\DiscordBotCommands;
4
5 use App\Models\DiscordBotCommand;
6 use App\Models\User;
7 use Discord\Discord;
8 use Discord\Parts\User\User as DiscordUser;
9 use Illuminate\Support\Facades\Http;
10 use Illuminate\Support\Facades\Storage;
11
12 class SyncUserCommand extends BaseCommand {
13
14         public function __construct(Discord $discord, DiscordBotCommand $cmd) {
15                 parent::__construct($discord, $cmd);
16         }
17
18         public function execute() {
19                 return $this->fetchUser()
20                         ->then(function (DiscordUser $discordUser) {
21                                 $user = User::find($discordUser->id);
22                                 if (!$user) {
23                                         $user = new User();
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;
30                                 }
31                                 $user->avatar = $discordUser->avatarhash ?: null;
32                                 if ($user->username != $discordUser->username) {
33                                         $user->username = $discordUser->username;
34                                         $user->discriminator = $discordUser->discriminator;
35                                 }
36
37                                 $media = Storage::disk('media');
38                                 if ($user->avatar && $media->missing('avatar/'.$user->id.'/'.$user->avatar.'.png')) {
39                                         try {
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) {
46                                         }
47                                 } else {
48                                         $user->avatar_cached = now();
49                                 }
50                                 $user->save();
51                         });
52         }
53
54 }