]> git.localhorst.tv Git - alttp.git/blob - app/Console/Commands/SyncAvatars.php
periodically sync avatars
[alttp.git] / app / Console / Commands / SyncAvatars.php
1 <?php
2
3 namespace App\Console\Commands;
4
5 use App\Models\DiscordBotCommand;
6 use App\Models\User;
7 use Illuminate\Console\Command;
8 use Illuminate\Database\Eloquent\Builder;
9
10 class SyncAvatars extends Command
11 {
12         /**
13          * The name and signature of the console command.
14          *
15          * @var string
16          */
17         protected $signature = 'sync:avatars';
18
19         /**
20          * The console command description.
21          *
22          * @var string
23          */
24         protected $description = 'Resync outdated avatars';
25
26         /**
27          * Execute the console command.
28          *
29          * @return int
30          */
31         public function handle()
32         {
33                 $users = User::whereNotNull('avatar')
34                         ->where(function (Builder $query) {
35                                 $query->whereNull('avatar_cached');
36                                 $query->orWhereColumn('avatar_cached', '<', 'updated_at');
37                                 $query->orWhere('avatar_cached', '<', now()->subtract(30, 'days'));
38                         })
39                         ->get();
40
41                 foreach ($users as $user) {
42                         try {
43                                 DiscordBotCommand::syncUser($user->id);
44                         } catch (\Exception $e) {
45                                 $this->error('error syncing avatar of user '.$user->id.': '.$e->getMessage());
46                         }
47                 }
48
49                 return 0;
50         }
51 }