]> git.localhorst.tv Git - alttp.git/blobdiff - app/TwitchBot/TwitchChatBot.php
fix IRC message parser
[alttp.git] / app / TwitchBot / TwitchChatBot.php
index 470cb24f5f34fc717402051143213a6bc9842b42..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] = [
@@ -117,8 +110,7 @@ class TwitchChatBot extends TwitchBot {
                $notes = $this->getNotes($channel);
                $ggs = 0;
                foreach ($notes['latest_msgs'] as $text) {
-                       $rawText = strtolower(preg_replace('/[^\w]/', '', $text));
-                       if (Str::startsWith($rawText, 'gg') || Str::endsWith($rawText, 'gg')) {
+                       if (ChatLog::classify($text) == 'gg') {
                                ++$ggs;
                        }
                }
@@ -129,20 +121,40 @@ class TwitchChatBot extends TwitchBot {
                $notes = $this->getNotes($channel);
                $gls = 0;
                foreach ($notes['latest_msgs'] as $text) {
-                       $rawText = strtolower(preg_replace('/[^\w]/', '', $text));
-                       if (Str::contains($rawText, ['glgl', 'glhf', 'hfgl'])) {
+                       if (ChatLog::classify($text) == 'gl') {
                                ++$gls;
                        }
                }
                return $gls > 2;
        }
 
+       private function checkForGreeting(Channel $channel) {
+               $notes = $this->getNotes($channel);
+               $his = 0;
+               foreach ($notes['latest_msgs'] as $text) {
+                       if (ChatLog::classify($text) == 'hi') {
+                               ++$his;
+                       }
+               }
+               return $his > 2;
+       }
+
+       private function checkForHype(Channel $channel) {
+               $notes = $this->getNotes($channel);
+               $hypes = 0;
+               foreach ($notes['latest_msgs'] as $text) {
+                       if (ChatLog::classify($text) == 'hype') {
+                               ++$hypes;
+                       }
+               }
+               return $hypes > 2;
+       }
+
        private function checkForLaughter(Channel $channel) {
                $notes = $this->getNotes($channel);
                $lulz = 0;
                foreach ($notes['latest_msgs'] as $text) {
-                       $rawText = strtolower(preg_replace('/[^\w]/', '', $text));
-                       if (Str::contains($rawText, ['haha', 'kekw', 'lol', 'lul', 'xd'])) {
+                       if (ChatLog::classify($text) == 'lol') {
                                ++$lulz;
                        }
                }
@@ -160,11 +172,33 @@ class TwitchChatBot extends TwitchBot {
                return $numbers > 2;
        }
 
+       private function checkForPog(Channel $channel) {
+               $notes = $this->getNotes($channel);
+               $pogs = 0;
+               foreach ($notes['latest_msgs'] as $text) {
+                       if (ChatLog::classify($text) == 'pog') {
+                               ++$pogs;
+                       }
+               }
+               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->randomGG($channel);
+                       return $channel->randomOfClass('gg');
                }
                if ($last != 'number' && $this->checkForNumbers($channel)) {
                        $this->setNote($channel, 'last_special', 'number');
@@ -176,20 +210,32 @@ class TwitchChatBot extends TwitchBot {
                }
                if ($last != 'glhf' && $this->checkForGLHF($channel)) {
                        $this->setNote($channel, 'last_special', 'glhf');
-                       return $this->randomGLHF($channel);
+                       return $channel->randomOfClass('gl');
+               }
+               if ($last != 'hi' && $this->checkForGreeting($channel)) {
+                       $this->setNote($channel, 'last_special', 'hi');
+                       return $channel->randomOfClass('hi');
+               }
+               if ($last != 'hype' && $this->checkForHype($channel)) {
+                       $this->setNote($channel, 'last_special', 'hype');
+                       return $channel->randomOfClass('hype');
+               }
+               if ($last != 'pog' && $this->checkForPog($channel)) {
+                       $this->setNote($channel, 'last_special', '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) {
@@ -206,22 +252,6 @@ class TwitchChatBot extends TwitchBot {
                return random_int($min, $max);
        }
 
-       private function randomGG(Channel $channel) {
-               $line = $this->queryChatlog($channel)
-                       ->where('text_content', 'LIKE', '%gg')
-                       ->whereRaw('LENGTH(`text_content`) < 30')
-                       ->first();
-               return $line->text_content;
-       }
-
-       private function randomGLHF(Channel $channel) {
-               $line = $this->queryChatlog($channel)
-                       ->where('text_content', 'LIKE', '%glhf%')
-                       ->whereRaw('LENGTH(`text_content`) < 30')
-                       ->first();
-               return $line->text_content;
-       }
-
        private function randomLaughter(Channel $channel) {
                return Arr::random([
                        ':tf:',
@@ -252,23 +282,24 @@ class TwitchChatBot extends TwitchBot {
                        'SUBprise',
                        'xD',
                        'YouDontSay',
+                       $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);
        }
 
@@ -276,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;
                }
        }
 
@@ -290,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 = [];