From 8fa7e52254f6a73c8403aa2c9d80497e584b5d6b Mon Sep 17 00:00:00 2001 From: Alex Thomassen Date: Fri, 24 Nov 2023 18:21:39 +0000 Subject: [PATCH] Add artisan command for adding moderator permissions --- .../Commands/AddChannelPermissions.php | 86 +++++++++++++++++++ app/Models/ChannelPermission.php | 10 +++ 2 files changed, 96 insertions(+) create mode 100644 app/Console/Commands/AddChannelPermissions.php diff --git a/app/Console/Commands/AddChannelPermissions.php b/app/Console/Commands/AddChannelPermissions.php new file mode 100644 index 0000000..7c16a59 --- /dev/null +++ b/app/Console/Commands/AddChannelPermissions.php @@ -0,0 +1,86 @@ +choice('Which channel would you like to add permissions to?', $channels->pluck('channel_login')->toArray()); + $channel = $channels->where('channel_login', $channel)->first(); + + $users = $this->ask('Which users (moderators) would you like to add to this channel? (comma separated list of usernames)'); + + $users = explode(',', $users); + $users = array_map('trim', $users); + + // Filter out any known bots. + $users = array_filter($users, function ($user) { + return !in_array(strtolower($user), $this->knownBots); + }); + + $users = array_values($users); + + // Get the user details from the Twitch API. + $response = Http::get($this->usersApi, [ + 'login' => $users, + ]); + + $result = $response->json(); + $users = $result['data']; + + foreach ($users as $user) { + $this->info('Adding ' . $user['display_name'] . ' to ' . $channel->channel_login); + + $permission = ChannelPermission::firstOrCreate([ + 'user_provider_id' => $user['id'], + 'channel_provider_id' => $channel->channel_id, + ]); + + $permission->save(); + } + } +} diff --git a/app/Models/ChannelPermission.php b/app/Models/ChannelPermission.php index 8b82bbb..a209302 100644 --- a/app/Models/ChannelPermission.php +++ b/app/Models/ChannelPermission.php @@ -10,4 +10,14 @@ class ChannelPermission extends Model use HasFactory; protected $table = 'channel_permissions'; + + /** + * The attributes that are mass assignable. + * + * @var array + */ + protected $fillable = [ + 'user_provider_id', + 'channel_provider_id', + ]; }