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