]> git.localhorst.tv Git - alttp.git/blob - app/Models/DiscordChannel.php
refine chat gen
[alttp.git] / app / Models / DiscordChannel.php
1 <?php
2
3 namespace App\Models;
4
5 use Discord\Parts\Channel\Channel;
6 use Illuminate\Database\Eloquent\Factories\HasFactory;
7 use Illuminate\Database\Eloquent\Model;
8
9 class DiscordChannel extends Model
10 {
11         use HasFactory;
12
13         public static function onUpstreamCreate(Channel $channel) {
14                 $guild = DiscordGuild::firstWhere('guild_id', $channel->guild_id);
15                 if (!$guild) return;
16                 static::sync($guild, $channel);
17         }
18
19         public static function onUpstreamUpdate(Channel $channel) {
20                 $guild = DiscordGuild::firstWhere('guild_id', $channel->guild_id);
21                 if (!$guild) return;
22                 static::sync($guild, $channel);
23         }
24
25         public static function onUpstreamDelete($channel) {
26                 if (!$channel) return;
27                 $model = static::firstWhere('channel_id', $channel->id);
28                 if (!$model) return;
29                 $model->delete();
30         }
31
32         public static function sync(DiscordGuild $guild, Channel $channel) {
33                 $model = static::firstOrNew([
34                         'channel_id' => $channel->id,
35                         'discord_guild_id' => $guild->id,
36                 ]);
37                 $model->name = $channel->name;
38                 $model->type = $channel->type;
39                 $model->position = $channel->position;
40                 $model->private = $channel->is_private;
41                 $model->parent_id = $channel->parent_id;
42                 $model->save();
43         }
44
45         public function guild() {
46                 return $this->belongsTo(DiscordGuild::class, 'discord_guild_id');
47         }
48
49         protected $fillable = [
50                 'channel_id',
51                 'discord_guild_id',
52         ];
53
54 }