]> git.localhorst.tv Git - alttp.git/blobdiff - app/Models/DiscordChannel.php
sync discord channels
[alttp.git] / app / Models / DiscordChannel.php
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',
+       ];
+
+}