]> git.localhorst.tv Git - alttp.git/blob - app/Models/DiscordGuild.php
refine chat gen
[alttp.git] / app / Models / DiscordGuild.php
1 <?php
2
3 namespace App\Models;
4
5 use Discord\Parts\Guild\Guild;
6 use Illuminate\Database\Eloquent\Factories\HasFactory;
7 use Illuminate\Database\Eloquent\Model;
8
9 class DiscordGuild extends Model
10 {
11
12         use HasFactory;
13
14         public static function onUpstreamCreate(Guild $guild) {
15                 static::sync($guild);
16         }
17
18         public static function onUpstreamUpdate(Guild $guild) {
19                 static::sync($guild);
20         }
21
22         public static function sync(Guild $guild) {
23                 $model = static::firstOrNew([
24                         'guild_id' => $guild->id,
25                 ]);
26                 $model->name = $guild->name;
27                 $model->icon_hash = $guild->icon_hash;
28                 $model->owner = $guild->owner_id;
29                 $model->locale = $guild->preferred_locale;
30                 $model->save();
31
32                 $role_ids = [];
33                 foreach ($guild->roles as $role) {
34                         DiscordRole::sync($model, $role);
35                         $role_ids[] = $role->id;
36                 }
37                 $model->roles()->whereNotIn('role_id', $role_ids)->delete();
38
39                 $channel_ids = [];
40                 foreach ($guild->channels as $channel) {
41                         DiscordChannel::sync($model, $channel);
42                         $channel_ids[] = $channel->id;
43                 }
44                 $model->channels()->whereNotIn('channel_id', $channel_ids)->delete();
45         }
46
47         public function channels() {
48                 return $this->hasMany(DiscordChannel::class)->orderBy('position');
49         }
50
51         public function roles() {
52                 return $this->hasMany(DiscordRole::class)->orderBy('position');
53         }
54
55         protected $fillable = [
56                 'guild_id',
57         ];
58
59 }