]> git.localhorst.tv Git - alttp.git/blobdiff - app/Models/DiscordGuild.php
track twitch category where chats were sent in
[alttp.git] / app / Models / DiscordGuild.php
index 3accd02fa0db37e275f6096e117535c43983bf45..bcbd12dd32b835a4b71bcabf774a0e6a1e31f424 100644 (file)
@@ -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,28 @@ 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();
+
+               $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)->orderBy('position');
+       }
+
+       public function roles() {
+               return $this->hasMany(DiscordRole::class)->orderBy('position');
        }
 
        protected $fillable = [