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 {
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');
}
});
}
+ 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];
protected $guild = null;
protected $member = null;
protected $roundChannel = null;
+ protected $user = null;
}
--- /dev/null
+<?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();
+ });
+ }
+
+}
'discriminator' => 'string',
'email' => 'string',
'avatar' => 'string',
+ 'avatar_cached' => 'boolean',
'verified' => 'boolean',
'locale' => 'string',
'mfa_enabled' => 'boolean',
--- /dev/null
+<?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');
+ });
+ }
+};