From d566d913c251fbb05e6bd314cc51f8b5ca49fe57 Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Wed, 27 Apr 2022 13:10:12 +0200 Subject: [PATCH] sync discord channels --- app/Console/Commands/DiscordBotCommand.php | 25 ++++++++- app/Models/DiscordChannel.php | 53 +++++++++++++++++++ app/Models/DiscordGuild.php | 11 ++++ ...7_085723_create_discord_channels_table.php | 37 +++++++++++++ 4 files changed, 125 insertions(+), 1 deletion(-) create mode 100644 app/Models/DiscordChannel.php create mode 100644 database/migrations/2022_04_27_085723_create_discord_channels_table.php diff --git a/app/Console/Commands/DiscordBotCommand.php b/app/Console/Commands/DiscordBotCommand.php index 7d9b65e..4ffbe11 100644 --- a/app/Console/Commands/DiscordBotCommand.php +++ b/app/Console/Commands/DiscordBotCommand.php @@ -2,9 +2,11 @@ namespace App\Console\Commands; +use App\Models\DiscordChannel; use App\Models\DiscordGuild; use App\Models\DiscordRole; use Discord\Discord; +use Discord\Parts\Channel\Channel; use Discord\Parts\Channel\Message; use Discord\Parts\Guild\Guild; use Discord\Parts\Guild\Role; @@ -57,6 +59,27 @@ class DiscordBotCommand extends Command $this->error('guild update: '.$e->getMessage()); } }); + $discord->on(Event::CHANNEL_CREATE, function (Channel $channel, Discord $discord) { + try { + DiscordGuild::onUpstreamCreate($channel); + } catch (\Exception $e) { + $this->error('channel create: '.$e->getMessage()); + } + }); + $discord->on(Event::CHANNEL_UPDATE, function (Channel $channel, Discord $discord, ?Channel $old) { + try { + DiscordGuild::onUpstreamUpdate($channel); + } catch (\Exception $e) { + $this->error('channel update: '.$e->getMessage()); + } + }); + $discord->on(Event::CHANNEL_DELETE, function ($channel, Discord $discord) { + try { + DiscordGuild::onUpstreamDelete($channel); + } catch (\Exception $e) { + $this->error('channel delete: '.$e->getMessage()); + } + }); $discord->on(Event::GUILD_ROLE_CREATE, function (Role $role, Discord $discord) { try { DiscordRole::onUpstreamCreate($role); @@ -64,7 +87,7 @@ class DiscordBotCommand extends Command $this->error('guild role create: '.$e->getMessage()); } }); - $discord->on(Event::GUILD_ROLE_UPDATE, function (Role $role, Discord $discordi, ?Role $old) { + $discord->on(Event::GUILD_ROLE_UPDATE, function (Role $role, Discord $discord, ?Role $old) { try { DiscordRole::onUpstreamUpdate($role); } catch (\Exception $e) { diff --git a/app/Models/DiscordChannel.php b/app/Models/DiscordChannel.php new file mode 100644 index 0000000..ba3d317 --- /dev/null +++ b/app/Models/DiscordChannel.php @@ -0,0 +1,53 @@ +guild_id); + if (!$guild) return; + static::sync($guild, $channel); + } + + public static function onUpstreamUpdate(Channel $channel) { + $guild = DiscordGuild::firstWhere('guild_id', $channel->guild_id); + if (!$guild) return; + static::sync($guild, $channel); + } + + public static function onUpstreamDelete($channel) { + if (!$channel) return; + $model = static::firstWhere('channel_id', $channel->id); + if (!$model) return; + $model->delete(); + } + + public static function sync(DiscordGuild $guild, Channel $channel) { + $model = static::firstOrNew([ + 'channel_id' => $channel->id, + 'discord_guild_id' => $guild->id, + ]); + $model->name = $channel->name; + $model->type = $channel->type; + $model->position = $channel->position; + $model->private = $channel->is_private; + $model->save(); + } + + public function guild() { + return $this->belongsTo(DiscordGuild::class); + } + + protected $fillable = [ + 'channel_id', + 'discord_guild_id', + ]; + +} diff --git a/app/Models/DiscordGuild.php b/app/Models/DiscordGuild.php index 361d6a9..84fd77b 100644 --- a/app/Models/DiscordGuild.php +++ b/app/Models/DiscordGuild.php @@ -35,6 +35,17 @@ class DiscordGuild extends Model $role_ids[] = $role->id; } $model->roles()->whereNotIn('role_id', $role_ids)->delete(); + + $channel_ids = []; + foreach ($guild->channels as $channel) { + DiscordChannel::sync($model, $channel); + $channel_ids[] = $channel->id; + } + $model->channels()->whereNotIn('channel_id', $channel_ids)->delete(); + } + + public function channels() { + return $this->hasMany(DiscordChannel::class); } public function roles() { diff --git a/database/migrations/2022_04_27_085723_create_discord_channels_table.php b/database/migrations/2022_04_27_085723_create_discord_channels_table.php new file mode 100644 index 0000000..8f7ba62 --- /dev/null +++ b/database/migrations/2022_04_27_085723_create_discord_channels_table.php @@ -0,0 +1,37 @@ +id(); + $table->foreignId('discord_guild_id')->nullable()->default(null)->constrained(); + $table->string('channel_id')->unique(); + $table->string('name'); + $table->integer('type'); + $table->integer('position'); + $table->boolean('private'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('discord_channels'); + } +}; -- 2.39.2