]> git.localhorst.tv Git - alttp.git/commitdiff
add user-sync discord command
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Mon, 20 Feb 2023 12:54:59 +0000 (13:54 +0100)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Mon, 20 Feb 2023 12:54:59 +0000 (13:54 +0100)
app/DiscordBotCommands/BaseCommand.php
app/DiscordBotCommands/SyncUserCommand.php [new file with mode: 0644]
app/Models/User.php
database/migrations/2023_02_20_123827_add_user_avatar_cached.php [new file with mode: 0644]

index b043af2ecd7f06da98e242381ebf3ceda3f7361f..e6861e03dad79666eaaeb726db05b8d48a862165 100644 (file)
@@ -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 (file)
index 0000000..b9ba6d3
--- /dev/null
@@ -0,0 +1,48 @@
+<?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();
+                       });
+       }
+
+}
index 8373b5d236c134a5cd0f2fc8127505e0dfb53966..879624a2072f58186e66ad107a604fd23e9f0d9f 100644 (file)
@@ -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 (file)
index 0000000..3577998
--- /dev/null
@@ -0,0 +1,32 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+return new class extends Migration
+{
+       /**
+        * Run the migrations.
+        *
+        * @return void
+        */
+       public function up()
+       {
+               Schema::table('users', function(Blueprint $table) {
+                       $table->timestamp('avatar_cached')->nullable()->default(null);
+               });
+       }
+
+       /**
+        * Reverse the migrations.
+        *
+        * @return void
+        */
+       public function down()
+       {
+               Schema::table('users', function(Blueprint $table) {
+                       $table->dropColumn('avatar_cached');
+               });
+       }
+};