]> git.localhorst.tv Git - alttp.git/commitdiff
sync discord channels
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Wed, 27 Apr 2022 11:10:12 +0000 (13:10 +0200)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Wed, 27 Apr 2022 11:10:12 +0000 (13:10 +0200)
app/Console/Commands/DiscordBotCommand.php
app/Models/DiscordChannel.php [new file with mode: 0644]
app/Models/DiscordGuild.php
database/migrations/2022_04_27_085723_create_discord_channels_table.php [new file with mode: 0644]

index 7d9b65e6e358401e2880d4c2a80788a3bec8b6f4..4ffbe119f440dbf5d308d05283025cb9557bec57 100644 (file)
@@ -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 (file)
index 0000000..ba3d317
--- /dev/null
@@ -0,0 +1,53 @@
+<?php
+
+namespace App\Models;
+
+use Discord\Parts\Channel\Channel;
+use Illuminate\Database\Eloquent\Factories\HasFactory;
+use Illuminate\Database\Eloquent\Model;
+
+class DiscordChannel extends Model
+{
+       use HasFactory;
+
+       public static function onUpstreamCreate(Channel $channel) {
+               $guild = DiscordGuild::firstWhere('guild_id', $channel->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',
+       ];
+
+}
index 361d6a911b54875ec8afdcf0d9b88c9495aaa0c0..84fd77bc4af0b785ff7df37aaf3ff3a1f899dc9d 100644 (file)
@@ -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 (file)
index 0000000..8f7ba62
--- /dev/null
@@ -0,0 +1,37 @@
+<?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::create('discord_channels', function (Blueprint $table) {
+                       $table->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');
+       }
+};