X-Git-Url: https://git.localhorst.tv/?a=blobdiff_plain;f=app%2FTwitchBot%2FTwitchBot.php;h=63f4236f685e92bf659610257e93177a78cf7a11;hb=HEAD;hp=e5a5aa6e442307b06bbae8b68635a07f4255b463;hpb=609c66ecff96d467f6f267a68d709ab5e609e002;p=alttp.git diff --git a/app/TwitchBot/TwitchBot.php b/app/TwitchBot/TwitchBot.php index e5a5aa6..242ac92 100644 --- a/app/TwitchBot/TwitchBot.php +++ b/app/TwitchBot/TwitchBot.php @@ -23,6 +23,9 @@ class TwitchBot { if (!$this->token) { throw new \Exception('unable to find access token'); } + if ($this->token->hasExpired()) { + $this->token->refresh(); + } $this->connector = new Connector(); $this->connect(); @@ -115,6 +118,10 @@ class TwitchBot { $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(); @@ -127,6 +134,11 @@ class TwitchBot { $this->ready = true; return; } + if ($msg->command == 'GLOBALUSERSTATE') { + // receive own user metadata + $this->handleUserState($msg); + return; + } } public function getMessageChannel(IRCMessage $msg) { @@ -143,6 +155,15 @@ class TwitchBot { public function handlePrivMsg(IRCMessage $msg) { } + public function handleUserState(IRCMessage $msg) { + if (isset($msg->tags['user-id'])) { + $this->user_id = $msg->tags['user-id']; + } + } + + public function handleWhisper(IRCMessage $msg) { + } + public function login() { $this->ws->send('PASS oauth:'.$this->token->access); $this->ws->send('NICK '.$this->nick); @@ -169,6 +190,20 @@ 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 () { @@ -188,6 +223,7 @@ class TwitchBot { private $nick; private $token; + private $user_id = ''; private $connector; private $ws;