]> 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 3e91a4e461d7b17ff22dd1d713475ac1d94b6c2b..bcbd12dd32b835a4b71bcabf774a0e6a1e31f424 100644 (file)
@@ -11,14 +11,45 @@ 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,
                ]);
                $model->name = $guild->name;
                $model->icon_hash = $guild->icon_hash;
+               $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 = [