X-Git-Url: https://git.localhorst.tv/?a=blobdiff_plain;f=app%2FTwitchBot%2FTwitchBot.php;h=242ac927021dfd6edace3e196f95f3f36ec87448;hb=refs%2Fheads%2Fmaster;hp=a45de87fcce69a36b32e06cdb2fc567b222144a0;hpb=31127000e16c18389129546b9aa652de565683fb;p=alttp.git diff --git a/app/TwitchBot/TwitchBot.php b/app/TwitchBot/TwitchBot.php index a45de87..242ac92 100644 --- a/app/TwitchBot/TwitchBot.php +++ b/app/TwitchBot/TwitchBot.php @@ -14,18 +14,21 @@ use React\EventLoop\Loop; class TwitchBot { - public function __construct() { + public function __construct($nick) { + $this->nick = $nick; $this->logger = new Logger('TwitchBot'); $this->logger->pushHandler(new StreamHandler('php://stdout', Logger::INFO)); - $this->token = TwitchToken::firstWhere('nick', 'localhorsttv'); + $this->token = TwitchToken::firstWhere('nick', $nick); if (!$this->token) { throw new \Exception('unable to find access token'); } + if ($this->token->hasExpired()) { + $this->token->refresh(); + } $this->connector = new Connector(); $this->connect(); - $this->listenCommands(); $this->startPinger(); } @@ -37,6 +40,10 @@ class TwitchBot { return Loop::get(); } + public function isReady() { + return $this->ready; + } + public function run() { $this->shutting_down = false; $this->getLoop()->run(); @@ -99,10 +106,6 @@ class TwitchBot { public function handleIRCMessage(IRCMessage $msg) { $this->last_contact = time(); - if ($msg->isPrivMsg()) { - $this->handlePrivMsg($msg); - return; - } if ($msg->isPing()) { $this->sendIRCMessage($msg->makePong()); return; @@ -110,6 +113,15 @@ class TwitchBot { if ($msg->isPong()) { return; } + $this->logMessage($msg); + if ($msg->isPrivMsg()) { + $this->handlePrivMsg($msg); + return; + } + if ($msg->isWhisper()) { + $this->handleWhisper($msg); + return; + } if ($msg->isNotice() && $msg->getText() == 'Login authentication failed') { $this->logger->notice('login failed, refreshing access token'); $this->token->refresh(); @@ -122,60 +134,42 @@ class TwitchBot { $this->ready = true; return; } + if ($msg->command == 'GLOBALUSERSTATE') { + // receive own user metadata + $this->handleUserState($msg); + return; + } } - public function handlePrivMsg(IRCMessage $msg) { + public function getMessageChannel(IRCMessage $msg) { $target = $msg->getPrivMsgTarget(); - if ($target[0] != '#') return; - $text = $msg->getText(); - if ($text[0] != '!') return; - $channel = Channel::firstWhere('twitch_chat', '=', $target); - if (!$channel) return; - $this->handleChatCommand($channel, $msg); - } - - public function handleChatCommand(Channel $channel, IRCMessage $msg) { - $cmd = explode(' ', ltrim($msg->getText(), '!'), 2); - if (!isset($channel->chat_commands[$cmd[0]])) return; - $config = $channel->chat_commands[$cmd[0]]; - $this->logger->info('got command '.$cmd[0].' on channel '.$channel->title); - try { - $command = ChatCommand::create($this, $channel, $config); - $command->execute($cmd[1] ?? ''); - } catch (\Exception $e) { - $this->logger->warning('error executing command '.$cmd[0].' on channel '.$channel->title.': '.$e->getMessage()); + if (substr($target, 0, 1) !== '#') { + $target = '#'.$target; } + return Channel::firstWhere('twitch_chat', '=', $target); } - public function login() { - $this->ws->send('PASS oauth:'.$this->token->access); - $this->ws->send('NICK localhorsttv'); + public function logMessage(IRCMessage $msg) { } - public function joinChannels() { - $this->logger->info('joining channels'); - $channels = Channel::where('twitch_chat', '!=', '')->where('join', '=', true)->get(); - $names = []; - foreach ($channels as $channel) { - $names[] = $channel->twitch_chat; - } - $chunks = array_chunk($names, 10); - foreach ($chunks as $chunk) { - $this->sendIRCMessage(IRCMessage::join($chunk)); + public function handlePrivMsg(IRCMessage $msg) { + } + + public function handleUserState(IRCMessage $msg) { + if (isset($msg->tags['user-id'])) { + $this->user_id = $msg->tags['user-id']; } } - private function listenCommands() { - $this->getLoop()->addPeriodicTimer(1, function () { - if (!$this->ready) return; - $command = TwitchBotCommand::where('status', '=', 'pending')->oldest()->first(); - if ($command) { - try { - $command->execute($this); - } catch (\Exception $e) { - } - } - }); + public function handleWhisper(IRCMessage $msg) { + } + + public function login() { + $this->ws->send('PASS oauth:'.$this->token->access); + $this->ws->send('NICK '.$this->nick); + } + + public function joinChannels() { } private function startPinger() { @@ -183,7 +177,7 @@ class TwitchBot { if (!$this->ready) return; if (time() - $this->last_contact < 60) return; try { - $this->sendIRCMessage(IRCMessage::ping()); + $this->sendIRCMessage(IRCMessage::ping($this->nick)); } catch (\Exception $e) { } }); @@ -196,10 +190,40 @@ class TwitchBot { $this->last_contact = time(); } + public function sendWhisper($to, $msg) { + $this->logger->info('sending whisper to '.$to.': '.$msg); + try { + $response = $this->token->request()->post('/whispers?from_user_id='.$this->user_id.'&to_user_id='.$to, [ + 'message' => $msg, + ]); + if (!$response->successful()) { + $this->logger->error('sending whisper to '.$to.': '.$response->status()); + } + } catch (\Exception $e) { + $this->logger->error('sending whisper to '.$to.': '.$e->getMessage()); + } + } + + + protected function listenCommands() { + $this->getLoop()->addPeriodicTimer(1, function () { + if (!$this->isReady()) return; + $command = TwitchBotCommand::where('bot_nick', '=', $this->nick)->where('status', '=', 'pending')->oldest()->first(); + if ($command) { + try { + $command->execute($this); + } catch (\Exception $e) { + } + } + }); + } + private $logger; + private $nick; private $token; + private $user_id = ''; private $connector; private $ws;