3 namespace App\Console\Commands;
5 use App\Models\Channel;
6 use App\Models\TwitchToken;
7 use Illuminate\Console\Command;
8 use Illuminate\Http\Client\RequestException;
9 use Illuminate\Support\Facades\Http;
11 class TwitchChannelInfo extends Command {
14 * The name and signature of the console command.
18 protected $signature = 'twitch:channel-info';
21 * The console command description.
25 protected $description = 'Refresh twitch channel info';
28 * Execute the console command.
32 public function handle()
34 $this->token = TwitchToken::firstWhere('nick', 'localhorsttv');
36 $this->line('please acquire a token for localhorsttv first');
39 $channels = Channel::where('twitch_chat', '!=', '')->where('twitch_id', '=', '')->get();
40 foreach ($channels as $channel) {
42 $this->updateChannel($channel);
43 } catch (RequestException $e) {
44 if ($e->response->status() == 401) {
45 $this->token->refresh();
46 $this->updateChannel($channel);
50 return Command::SUCCESS;
53 private function updateChannel(Channel $channel) {
54 $this->line($channel->twitch_chat);
55 $login = substr($channel->twitch_chat, 1);
56 $rsp = $this->token->request()
61 foreach ($rsp['data'] as $user) {
62 if ($user['login'] != $login) continue;
63 $channel->twitch_id = $user['id'];