]> git.localhorst.tv Git - alttp.git/commitdiff
respond to whispers master
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Fri, 28 Jun 2024 16:40:28 +0000 (18:40 +0200)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Fri, 28 Jun 2024 16:40:28 +0000 (18:40 +0200)
app/Console/Commands/TwitchAuth.php
app/TwitchBot/IRCMessage.php
app/TwitchBot/TwitchBot.php
app/TwitchBot/TwitchChatBot.php

index 4b185c6cfd22b64d712fd15c39929960bc6b079f..94d8adaa71628c28cc304a28011c292b8675dbed 100644 (file)
@@ -33,7 +33,7 @@ class TwitchAuth extends Command {
                if (!$token) {
                        $token = new TwitchToken();
                        $token->nick = $this->argument('nick');
-                       $token->scope = ['chat:read', 'chat:edit', 'whispers:read', 'whispers:edit'];
+                       $token->scope = ['chat:read', 'chat:edit', 'whispers:read', 'user:manage:whispers'];
                }
                $url = $token->getAuthUrl();
                $this->line('Please visit '.$url);
index 0f73c06797a79b4694215dbe09a380392e56620f..b986847792f6898a4d99f8abb9a82f9455e55318 100644 (file)
@@ -140,6 +140,13 @@ class IRCMessage {
                return TokenizedMessage::fromIRC($this);
        }
 
+       public static function capReq($cap) {
+               $msg = new IRCMessage();
+               $msg->command = 'CAP REQ';
+               $msg->params[] = $cap;
+               return $msg;
+       }
+
        public static function join($channels) {
                $msg = new IRCMessage();
                $msg->command = 'JOIN';
@@ -199,6 +206,10 @@ class IRCMessage {
                return $this->command == 'PRIVMSG';
        }
 
+       public function isWhisper() {
+               return $this->command == 'WHISPER';
+       }
+
        public function isOwner() {
                return substr($this->getPrivMsgTarget(), 1) == $this->nick;
        }
index 63f4236f685e92bf659610257e93177a78cf7a11..242ac927021dfd6edace3e196f95f3f36ec87448 100644 (file)
@@ -118,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();
@@ -130,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) {
@@ -146,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);
@@ -172,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 () {
@@ -191,6 +223,7 @@ class TwitchBot {
 
        private $nick;
        private $token;
+       private $user_id = '';
 
        private $connector;
        private $ws;
index 700de25bfb86bff91542b0f95f98f43bb4af9f80..7151b2b682d092dc1081569ee567f9faf676080b 100644 (file)
@@ -46,6 +46,11 @@ class TwitchChatBot extends TwitchBot {
                $this->tagChannelRead($channel, $msg);
        }
 
+       public function handleWhisper(IRCMessage $msg) {
+               $text = $this->chatlib->generate($msg->getText());
+               $this->sendWhisper($msg->tags['user-id'], $text);
+       }
+
        public function getChatlibDatabase(Channel $channel) {
                return $this->chatlib;
        }