Add artisan command for adding moderator permissions

This commit is contained in:
Alex Thomassen 2023-11-24 18:21:39 +00:00
parent 66ed47b35b
commit 8fa7e52254
Signed by: Alex
GPG Key ID: 10BD786B5F6FF5DE
2 changed files with 96 additions and 0 deletions

View File

@ -0,0 +1,86 @@
<?php
namespace App\Console\Commands;
use App\Models\ChannelPermission;
use Illuminate\Console\Command;
use App\Models\Trace\Channel as TraceChannel;
use Illuminate\Support\Facades\Http;
class AddChannelPermissions extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'app:add-channel-permissions';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Provide a list of users that should have access to a channel.';
/**
* The list of known bots.
*
* @var array
*/
private $knownBots = [
'moobot',
'nightbot',
'streamelements',
];
/**
* Twitch API endpoint for getting user details.
*
* @var string
*/
private $usersApi = 'https://twitch-api-proxy.cactus.workers.dev/direct/users';
/**
* Execute the console command.
*/
public function handle()
{
// Give the user a list of channels to choose from.
$channels = TraceChannel::all();
$channel = $this->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();
}
}
}

View File

@ -10,4 +10,14 @@ class ChannelPermission extends Model
use HasFactory; use HasFactory;
protected $table = 'channel_permissions'; protected $table = 'channel_permissions';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'user_provider_id',
'channel_provider_id',
];
} }