5 use Discord\Parts\Guild\Guild;
6 use Illuminate\Database\Eloquent\Factories\HasFactory;
7 use Illuminate\Database\Eloquent\Model;
9 class DiscordGuild extends Model
14 public static function onUpstreamCreate(Guild $guild) {
18 public static function onUpstreamUpdate(Guild $guild) {
22 public static function sync(Guild $guild) {
23 $model = static::firstOrNew([
24 'guild_id' => $guild->id,
26 $model->name = $guild->name;
27 $model->icon_hash = $guild->icon_hash;
28 $model->owner = $guild->owner_id;
29 $model->locale = $guild->preferred_locale;
33 foreach ($guild->roles as $role) {
34 DiscordRole::sync($model, $role);
35 $role_ids[] = $role->id;
37 $model->roles()->whereNotIn('role_id', $role_ids)->delete();
40 foreach ($guild->channels as $channel) {
41 DiscordChannel::sync($model, $channel);
42 $channel_ids[] = $channel->id;
44 $model->channels()->whereNotIn('channel_id', $channel_ids)->delete();
47 public function channels() {
48 return $this->hasMany(DiscordChannel::class)->orderBy('position');
51 public function roles() {
52 return $this->hasMany(DiscordRole::class)->orderBy('position');
55 protected $fillable = [