]> git.localhorst.tv Git - alttp.git/blobdiff - app/TwitchBot/TwitchChatBot.php
respond to whispers
[alttp.git] / app / TwitchBot / TwitchChatBot.php
index 6ec517e93f62ba55926e40b5a6bfa15303a3c9a7..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;
        }
@@ -80,7 +85,7 @@ class TwitchChatBot extends TwitchBot {
                        return;
                }
                $text = $this->contextualMsg($channel);
-               if ($this->shouldAdlib($channel)) {
+               if (!$text && $this->shouldAdlib($channel)) {
                        $this->performAdlib($channel);
                        return;
                }
@@ -258,7 +263,8 @@ class TwitchChatBot extends TwitchBot {
 
        private function performAdlib(Channel $channel) {
                $db = $this->getChatlibDatabase($channel);
-               $text = $db->generate();
+               $latest_msg = $this->getLatestMessage($channel);
+               $text = $db->generate($latest_msg->getText());
                $this->tagChannelWrite($channel);
                $this->sendIRCMessage(IRCMessage::privmsg($channel->twitch_chat, $text));
                $log = new ChatBotLog();
@@ -306,12 +312,10 @@ class TwitchChatBot extends TwitchBot {
 
                $tokenized = $msg->tokenize();
                if (!ChatLog::isKnownBot($msg->nick) && !$tokenized->isSpammy()) {
-                       $this->notes[$channel->id]['latest_msgs'][] = $tokenized;
-                       if (count($this->notes[$channel->id]['latest_msgs']) > 10) {
-                               array_shift($this->notes[$channel->id]['latest_msgs']);
-                       }
+                       $this->noteChannelMessage($channel, $tokenized);
                }
                if ($this->isDirectedAtMe($msg->getText()) && $this->shouldRespond($channel)) {
+                       $this->noteChannelMessage($channel, $tokenized);
                        $this->notes[$channel->id]['wait_msgs'] = 0;
                        $this->notes[$channel->id]['wait_time'] = 0;
                        $response = $tokenized->getResponseCategory();
@@ -321,6 +325,13 @@ class TwitchChatBot extends TwitchBot {
                }
        }
 
+       private function noteChannelMessage(Channel $channel, TokenizedMessage $tokenized) {
+               $this->notes[$channel->id]['latest_msgs'][] = $tokenized;
+               if (count($this->notes[$channel->id]['latest_msgs']) > 10) {
+                       array_shift($this->notes[$channel->id]['latest_msgs']);
+               }
+       }
+
        private function tagChannelWrite(Channel $channel) {
                $this->getNotes($channel);
                $this->notes[$channel->id]['last_write'] = time();
@@ -335,7 +346,7 @@ class TwitchChatBot extends TwitchBot {
        }
 
        private function getLatestMessage(Channel $channel) {
-               $this->getNotes($channel);
+               $notes = $this->getNotes($channel);
                if (!empty($notes['latest_msgs'])) {
                        return $notes['latest_msgs'][count($notes['latest_msgs']) - 1];
                }