From 684ad09eaf505f39749a001fd8b23fc30fdf8b3f Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Wed, 27 Apr 2022 10:44:17 +0200 Subject: [PATCH] sync discord roles --- app/Console/Commands/DiscordBotCommand.php | 37 +++++++++++-- app/Models/DiscordGuild.php | 19 +++++++ app/Models/DiscordRole.php | 53 +++++++++++++++++++ .../2022_04_27_073842_add_discord_roles.php | 37 +++++++++++++ 4 files changed, 143 insertions(+), 3 deletions(-) create mode 100644 app/Models/DiscordRole.php create mode 100644 database/migrations/2022_04_27_073842_add_discord_roles.php diff --git a/app/Console/Commands/DiscordBotCommand.php b/app/Console/Commands/DiscordBotCommand.php index a049eba..7d9b65e 100644 --- a/app/Console/Commands/DiscordBotCommand.php +++ b/app/Console/Commands/DiscordBotCommand.php @@ -3,9 +3,11 @@ namespace App\Console\Commands; use App\Models\DiscordGuild; +use App\Models\DiscordRole; use Discord\Discord; use Discord\Parts\Channel\Message; use Discord\Parts\Guild\Guild; +use Discord\Parts\Guild\Role; use Discord\WebSockets\Event; use Illuminate\Console\Command; use Monolog\Handler\StreamHandler; @@ -42,10 +44,39 @@ class DiscordBotCommand extends Command 'token' => config('discord.token'), ]); $discord->on(Event::GUILD_CREATE, function (Guild $guild, Discord $discord) { - DiscordGuild::sync($guild); + try { + DiscordGuild::onUpstreamCreate($guild); + } catch (\Exception $e) { + $this->error('guild create: '.$e->getMessage()); + } }); - $discord->on(Event::GUILD_UPDATE, function (Guild $guild, Discord $discord, Guild $old) { - DiscordGuild::sync($guild); + $discord->on(Event::GUILD_UPDATE, function (Guild $guild, Discord $discord, ?Guild $old) { + try { + DiscordGuild::onUpstreamUpdate($guild); + } catch (\Exception $e) { + $this->error('guild update: '.$e->getMessage()); + } + }); + $discord->on(Event::GUILD_ROLE_CREATE, function (Role $role, Discord $discord) { + try { + DiscordRole::onUpstreamCreate($role); + } catch (\Exception $e) { + $this->error('guild role create: '.$e->getMessage()); + } + }); + $discord->on(Event::GUILD_ROLE_UPDATE, function (Role $role, Discord $discordi, ?Role $old) { + try { + DiscordRole::onUpstreamUpdate($role); + } catch (\Exception $e) { + $this->error('guild role update: '.$e->getMessage()); + } + }); + $discord->on(Event::GUILD_ROLE_DELETE, function ($role, Discord $discord) { + try { + DiscordRole::onUpstreamDelete($role); + } catch (\Exception $e) { + $this->error('guild role delete: '.$e->getMessage()); + } }); $discord->on('ready', function (Discord $discord) { $discord->on(Event::MESSAGE_CREATE, function (Message $message, Discord $discord) { diff --git a/app/Models/DiscordGuild.php b/app/Models/DiscordGuild.php index 3accd02..361d6a9 100644 --- a/app/Models/DiscordGuild.php +++ b/app/Models/DiscordGuild.php @@ -11,6 +11,14 @@ class DiscordGuild extends Model use HasFactory; + public static function onUpstreamCreate(Guild $guild) { + static::sync($guild); + } + + public static function onUpstreamUpdate(Guild $guild) { + static::sync($guild); + } + public static function sync(Guild $guild) { $model = static::firstOrNew([ 'guild_id' => $guild->id, @@ -20,6 +28,17 @@ class DiscordGuild extends Model $model->owner = $guild->owner_id; $model->locale = $guild->preferred_locale; $model->save(); + + $role_ids = []; + foreach ($guild->roles as $role) { + DiscordRole::sync($model, $role); + $role_ids[] = $role->id; + } + $model->roles()->whereNotIn('role_id', $role_ids)->delete(); + } + + public function roles() { + return $this->hasMany(DiscordRole::class); } protected $fillable = [ diff --git a/app/Models/DiscordRole.php b/app/Models/DiscordRole.php new file mode 100644 index 0000000..637665d --- /dev/null +++ b/app/Models/DiscordRole.php @@ -0,0 +1,53 @@ +guild_id); + if (!$guild) return; + static::sync($guild, $role); + } + + public static function onUpstreamUpdate(Role $role) { + $guild = DiscordGuild::firstWhere('guild_id', $role->guild_id); + if (!$guild) return; + static::sync($guild, $role); + } + + public static function onUpstreamDelete($role) { + if (!$role) return; + $model = static::firstWhere('role_id', $role->id); + if (!$model) return; + $model->delete(); + } + + public static function sync(DiscordGuild $guild, Role $role) { + $model = static::firstOrNew([ + 'discord_guild_id' => $guild->id, + 'role_id' => $role->id, + ]); + $model->name = $role->name; + $model->color = $role->color; + $model->position = $role->position; + $model->permissions = $role->permissions->bitwise; + $model->save(); + } + + public function guild() { + return $this->belongsTo(DiscordGuild::class); + } + + protected $fillable = [ + 'discord_guild_id', + 'role_id', + ]; + +} diff --git a/database/migrations/2022_04_27_073842_add_discord_roles.php b/database/migrations/2022_04_27_073842_add_discord_roles.php new file mode 100644 index 0000000..ce0684c --- /dev/null +++ b/database/migrations/2022_04_27_073842_add_discord_roles.php @@ -0,0 +1,37 @@ +id(); + $table->foreignId('discord_guild_id')->constrained(); + $table->string('role_id')->unique(); + $table->string('name'); + $table->integer('color'); + $table->integer('position'); + $table->bigInteger('permissions'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('discord_roles'); + } +}; -- 2.39.2