]> git.localhorst.tv Git - alttp.git/blobdiff - app/TwitchBot/TwitchChatBot.php
fix IRC message parser
[alttp.git] / app / TwitchBot / TwitchChatBot.php
index 8aac9d70bd66652eee0edad17161007732ad0586..7cdc704bedfcd4e6c25a40298929430405070690 100644 (file)
@@ -72,19 +72,12 @@ class TwitchChatBot extends TwitchBot {
                        return;
                }
                $text = $this->contextualMsg($channel);
-               if (!$text) $text = $this->randomMsg($channel);
+               if (!$text) $text = $this->randomChat($channel);
                if (!$text) return;
                $this->tagChannelWrite($channel);
                $this->sendIRCMessage(IRCMessage::privmsg($channel->twitch_chat, $text));
        }
 
-       private function getChatSetting(Channel $channel, $name, $default = null) {
-               if (array_key_exists($name, $channel->chat_settings)) {
-                       return $channel->chat_settings[$name];
-               }
-               return $default;
-       }
-
        private function getNotes(Channel $channel) {
                if (!isset($this->notes[$channel->id])) {
                        $this->notes[$channel->id] = [
@@ -190,11 +183,22 @@ class TwitchChatBot extends TwitchBot {
                return $pogs > 2;
        }
 
+       private function checkForSalute(Channel $channel) {
+               $notes = $this->getNotes($channel);
+               $o7s = 0;
+               foreach ($notes['latest_msgs'] as $text) {
+                       if (ChatLog::classify($text) == 'o7') {
+                               ++$o7s;
+                       }
+               }
+               return $o7s > 2;
+       }
+
        private function contextualMsg(Channel $channel) {
                $last = $this->getNote($channel, 'last_special');
                if ($last != 'gg' && $this->checkForGG($channel)) {
                        $this->setNote($channel, 'last_special', 'gg');
-                       return $this->randomOfClass($channel, 'gg');
+                       return $channel->randomOfClass('gg');
                }
                if ($last != 'number' && $this->checkForNumbers($channel)) {
                        $this->setNote($channel, 'last_special', 'number');
@@ -206,32 +210,32 @@ class TwitchChatBot extends TwitchBot {
                }
                if ($last != 'glhf' && $this->checkForGLHF($channel)) {
                        $this->setNote($channel, 'last_special', 'glhf');
-                       return $this->randomOfClass($channel, 'gl');
+                       return $channel->randomOfClass('gl');
                }
                if ($last != 'hi' && $this->checkForGreeting($channel)) {
                        $this->setNote($channel, 'last_special', 'hi');
-                       return $this->randomOfClass($channel, 'hi');
+                       return $channel->randomOfClass('hi');
                }
                if ($last != 'hype' && $this->checkForHype($channel)) {
                        $this->setNote($channel, 'last_special', 'hype');
-                       return $this->randomOfClass($channel, 'hype');
+                       return $channel->randomOfClass('hype');
                }
                if ($last != 'pog' && $this->checkForPog($channel)) {
                        $this->setNote($channel, 'last_special', 'pog');
-                       return $this->randomOfClass($channel, 'pog');
+                       return $channel->randomOfClass('pog');
+               }
+               if ($last != 'o7' && $this->checkForSalute($channel)) {
+                       $this->setNote($channel, 'last_special', 'o7');
+                       return $channel->randomOfClass('o7');
                }
                return false;
        }
 
-       private function queryChatlog(Channel $channel) {
-               return ChatLog::where('type', '=', 'chat')
-                       ->where('banned', '=', false)
-                       ->where('created_at', '<', now()->sub(1, 'day'))
-                       ->where(function ($query) use ($channel) {
-                               $query->whereNull('detected_language');
-                               $query->orWhereIn('detected_language', $channel->languages);
-                       })
-                       ->inRandomOrder();
+       private function randomChat(Channel $channel) {
+               $line = $channel->queryChatlog()
+                       ->whereIn('classification', ['hi', 'hype', 'lol', 'pog', 'unclassified'])
+                       ->first();
+               return $line->text_content;
        }
 
        private function randomContextualNumber(Channel $channel) {
@@ -248,13 +252,6 @@ class TwitchChatBot extends TwitchBot {
                return random_int($min, $max);
        }
 
-       private function randomOfClass(Channel $channel, $class) {
-               $line = $this->queryChatlog($channel)
-                       ->where('classification', '=', $class)
-                       ->first();
-               return $line->text_content;
-       }
-
        private function randomLaughter(Channel $channel) {
                return Arr::random([
                        ':tf:',
@@ -285,24 +282,24 @@ class TwitchChatBot extends TwitchBot {
                        'SUBprise',
                        'xD',
                        'YouDontSay',
-                       $this->randomOfClass($channel, 'lol'),
+                       $channel->randomOfClass('lol'),
                ]);
        }
 
        private function randomMsg(Channel $channel) {
-               $line = $this->queryChatlog($channel)->first();
+               $line = $channel->queryChatlog()->first();
                return $line->text_content;
        }
 
        private function randomWaitMsgs(Channel $channel) {
-               $min = $this->getChatSetting($channel, 'wait_msgs_min', 1);
-               $max = $this->getChatSetting($channel, 'wait_msgs_max', 10);
+               $min = $channel->getChatSetting('wait_msgs_min', 1);
+               $max = $channel->getChatSetting('wait_msgs_max', 10);
                return random_int($min, $max);
        }
 
        private function randomWaitTime(Channel $channel) {
-               $min = $this->getChatSetting($channel, 'wait_time_min', 1);
-               $max = $this->getChatSetting($channel, 'wait_time_max', 900);
+               $min = $channel->getChatSetting('wait_time_min', 1);
+               $max = $channel->getChatSetting('wait_time_max', 900);
                return random_int($min, $max);
        }
 
@@ -310,9 +307,15 @@ class TwitchChatBot extends TwitchBot {
                $this->getNotes($channel);
                $this->notes[$channel->id]['last_read'] = time();
                ++$this->notes[$channel->id]['read_since_last_write'];
-               $this->notes[$channel->id]['latest_msgs'][] = $msg->getText();
-               if (count($this->notes[$channel->id]['latest_msgs']) > 10) {
-                       array_shift($this->notes[$channel->id]['latest_msgs']);
+               if (!ChatLog::isKnownBot($msg->nick) && !ChatLog::spammyText($msg->getText())) {
+                       $this->notes[$channel->id]['latest_msgs'][] = $msg->getText();
+                       if (count($this->notes[$channel->id]['latest_msgs']) > 10) {
+                               array_shift($this->notes[$channel->id]['latest_msgs']);
+                       }
+               }
+               if ($this->isDirectedAtMe($msg->getText()) && $this->shouldRespond($channel)) {
+                       $this->notes[$channel->id]['wait_msgs'] = 0;
+                       $this->notes[$channel->id]['wait_time'] = 0;
                }
        }
 
@@ -324,6 +327,25 @@ class TwitchChatBot extends TwitchBot {
                $this->notes[$channel->id]['wait_time'] = $this->randomWaitTime($channel);
        }
 
+       private function isDirectedAtMe($raw_text) {
+               $text = strtolower($raw_text);
+               if (strpos($text, 'horstie') !== false) {
+                       return true;
+               }
+               return false;
+       }
+
+       private function shouldRespond(Channel $channel) {
+               $setting = $channel->getChatSetting('respond', 'yes');
+               if ($setting == 'yes') {
+                       return true;
+               }
+               if ($setting == '50') {
+                       return random_int(0, 1);
+               }
+               return false;
+       }
+
        private $channels;
        private $notes = [];