X-Git-Url: https://git.localhorst.tv/?a=blobdiff_plain;f=app%2FTwitchBot%2FTwitchChatBot.php;h=7151b2b682d092dc1081569ee567f9faf676080b;hb=b51ffd97a5ec084ad2e4e7804cb047b464a2b58b;hp=92894262f3b108d42826f76238dca6c909146a94;hpb=8645b77ea2dc402f0265e1c8022ba18302506ca1;p=alttp.git diff --git a/app/TwitchBot/TwitchChatBot.php b/app/TwitchBot/TwitchChatBot.php index 9289426..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; @@ -245,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); @@ -283,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(); @@ -298,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(); @@ -312,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]; } @@ -348,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') { @@ -387,5 +432,6 @@ class TwitchChatBot extends TwitchBot { private $channels; private $notes = []; + private $chatlib; }