X-Git-Url: https://git.localhorst.tv/?a=blobdiff_plain;f=app%2FTwitchBot%2FTwitchChatBot.php;h=6ec517e93f62ba55926e40b5a6bfa15303a3c9a7;hb=HEAD;hp=a403b0b57b716ae488c1056e8f3e6d8be53573f8;hpb=1b9629ce7f600b4aa9c9d51a281e514031871828;p=alttp.git diff --git a/app/TwitchBot/TwitchChatBot.php b/app/TwitchBot/TwitchChatBot.php index a403b0b..7151b2b 100644 --- a/app/TwitchBot/TwitchChatBot.php +++ b/app/TwitchBot/TwitchChatBot.php @@ -4,6 +4,7 @@ namespace App\TwitchBot; use App\Models\Channel; use App\Models\ChatBotLog; +use App\Models\ChatLib; use App\Models\ChatLog; use Illuminate\Support\Arr; use Illuminate\Support\Str; @@ -15,6 +16,8 @@ class TwitchChatBot extends TwitchBot { $this->updateChannels(); $this->startTimer(); $this->listenCommands(); + $this->chatlib = new ChatLib(); + $this->chatlib->loadFrom('de'); } public function joinChannels() { @@ -43,6 +46,15 @@ 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; + } + private function startTimer() { $this->getLoop()->addPeriodicTimer(1, function () { @@ -73,6 +85,10 @@ class TwitchChatBot extends TwitchBot { return; } $text = $this->contextualMsg($channel); + if (!$text && $this->shouldAdlib($channel)) { + $this->performAdlib($channel); + return; + } if (!$text) $text = $this->randomChat($channel); if (!$text) return; $actual_text = is_object($text) ? $text->text_content : $text; @@ -82,6 +98,9 @@ class TwitchChatBot extends TwitchBot { $log->channel()->associate($channel); if (is_object($text)) { $log->origin()->associate($text); + $log->category = $text->classification; + } else { + $log->category = $this->getLastSpecialSent($channel); } $log->text = $actual_text; $log->save(); @@ -139,13 +158,12 @@ class TwitchChatBot extends TwitchBot { $this->tagChannelSpecialSent($channel, $classification); } $this->clearQueuedSpecial($channel); - if ($classification == 'number') { - return $this->randomContextualNumber($channel); - } - if ($classification == 'lol') { - return $this->randomLaughter($channel); - } - return $channel->randomOfClass($classification); + return $this->getRandomOfClass($channel, $classification); + } + $latest_msg = $this->getLatestMessage($channel); + if ($latest_msg->classify() == 'question') { + $response = $latest_msg->getResponseCategory(); + return $this->getRandomOfClass($channel, $response); } $last = $this->getLastSpecialSent($channel); $classifications = $this->collectClassifications($channel); @@ -178,13 +196,8 @@ class TwitchChatBot extends TwitchBot { if (!isset($count_quotas[$classification]) || $count < $count_quotas[$classification]) continue; if (!isset($time_quotas[$classification]) || $this->getTimeSinceSpecial($channel, $classification) < $time_quotas[$classification]) continue; $this->tagChannelSpecialSent($channel, $classification); - if ($classification == 'number') { - return $this->randomContextualNumber($channel); - } - if ($classification == 'lol') { - return $this->randomLaughter($channel); - } - return $channel->randomOfClass($classification); + $reaction = $this->getChimeInReaction($channel, $classification); + return $this->getRandomOfClass($channel, $reaction); } return false; } @@ -248,6 +261,19 @@ class TwitchChatBot extends TwitchBot { return $channel->queryChatlog()->first(); } + private function performAdlib(Channel $channel) { + $db = $this->getChatlibDatabase($channel); + $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(); + $log->channel()->associate($channel); + $log->category = 'adlib'; + $log->text = $text; + $log->save(); + } + private function randomWaitMsgs(Channel $channel) { $min = $channel->getChatSetting('wait_msgs_min', 1); $max = $channel->getChatSetting('wait_msgs_max', 10); @@ -286,21 +312,26 @@ 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 = $this->getResponseTo($tokenized); + $response = $tokenized->getResponseCategory(); if ($response) { $this->queueSpecial($channel, $response); } } } + 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(); @@ -314,6 +345,14 @@ class TwitchChatBot extends TwitchBot { $this->notes[$channel->id]['last_special'][$classification] = time(); } + private function getLatestMessage(Channel $channel) { + $notes = $this->getNotes($channel); + if (!empty($notes['latest_msgs'])) { + return $notes['latest_msgs'][count($notes['latest_msgs']) - 1]; + } + return TokenizedMessage::fromString(''); + } + private function getLastSpecialSent(Channel $channel) { $notes = $this->getNotes($channel); $max_time = 0; @@ -343,6 +382,17 @@ class TwitchChatBot extends TwitchBot { return false; } + private function shouldAdlib(Channel $channel) { + $setting = $channel->getChatSetting('adlib', 50); + if ($setting == 0) { + return false; + } + if ($setting == 100) { + return true; + } + return random_int(0, 100) <= $setting; + } + private function shouldRespond(Channel $channel) { $setting = $channel->getChatSetting('respond', 'yes'); if ($setting == 'yes') { @@ -354,29 +404,34 @@ class TwitchChatBot extends TwitchBot { return false; } - private function getResponseTo(TokenizedMessage $msg) { - switch ($msg->classify()) { - case 'gg': - return ['love', 'eyes', 'thx', 'pog', 'kappa']; - case 'gl': - return ['love', 'eyes', 'thx']; + private function getRandomOfClass(Channel $channel, $classification) { + if ($classification == 'number') { + return $this->randomContextualNumber($channel); + } + if ($classification == 'lol') { + return $this->randomLaughter($channel); + } + return $channel->randomOfClass($classification); + } + + private function getChimeInReaction(Channel $channel, $classification) { + switch ($classification) { case 'hi': - return ['hi', 'love', 'eyes', 'hype', 'pog']; - case 'kappa': - return ['kappa', 'lol', 'eyes']; - case 'love': - return ['hi', 'love', 'eyes', 'thx']; - case 'question': - return ['yes', 'no', 'kappa', 'lol', 'wtf', 'number']; - case 'rage': - return ['kappa', 'lol', 'rage']; + return ['hi', 'love']; + case 'hype': + return ['hype', 'love', 'pog']; + case 'lol': + return ['kappa', 'lol']; + case 'pog': + return ['hype', 'pog']; case 'wtf': - return ['kappa', 'lol', 'rage']; + return ['lol', 'wtf']; } - return false; + return $classification; } private $channels; private $notes = []; + private $chatlib; }