From: Daniel Karbach Date: Wed, 27 Apr 2022 11:10:12 +0000 (+0200) Subject: sync discord channels X-Git-Url: https://git.localhorst.tv/?a=commitdiff_plain;ds=inline;h=d566d913c251fbb05e6bd314cc51f8b5ca49fe57;p=alttp.git sync discord channels --- 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'); + } +};