X-Git-Url: https://git.localhorst.tv/?a=blobdiff_plain;f=app%2FModels%2FDiscordGuild.php;h=bcbd12dd32b835a4b71bcabf774a0e6a1e31f424;hb=abdc2ea1ade1fb12ceacc28660890750e69ae36f;hp=3e91a4e461d7b17ff22dd1d713475ac1d94b6c2b;hpb=a058ba02c473649714add36edd021faab7a96e4c;p=alttp.git diff --git a/app/Models/DiscordGuild.php b/app/Models/DiscordGuild.php index 3e91a4e..bcbd12d 100644 --- a/app/Models/DiscordGuild.php +++ b/app/Models/DiscordGuild.php @@ -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 = [