X-Git-Url: https://git.localhorst.tv/?a=blobdiff_plain;f=app%2FTwitchBot%2FTwitchChatBot.php;h=470cb24f5f34fc717402051143213a6bc9842b42;hb=532dea7c62f3619a04cd8d7aa895f8db1f3f98d2;hp=5864c5f6beeb392cb21926c5ef544224eb32e4e0;hpb=d2e290f1264425f9b01fffd8943717dafe6867a2;p=alttp.git diff --git a/app/TwitchBot/TwitchChatBot.php b/app/TwitchBot/TwitchChatBot.php index 5864c5f..470cb24 100644 --- a/app/TwitchBot/TwitchChatBot.php +++ b/app/TwitchBot/TwitchChatBot.php @@ -4,22 +4,16 @@ namespace App\TwitchBot; use App\Models\Channel; use App\Models\ChatLog; +use Illuminate\Support\Arr; +use Illuminate\Support\Str; class TwitchChatBot extends TwitchBot { public function __construct() { parent::__construct('horstiebot'); - $this->channels = Channel::where('twitch_chat', '!=', '')->where('chat', '=', true)->get(); - foreach ($this->channels as $channel) { - $this->notes[$channel->id] = [ - 'last_read' => 0, - 'last_write' => time(), - 'read_since_last_write' => 0, - 'wait_msgs' => $this->randomWaitMsgs($channel), - 'wait_time' => $this->randomWaitTime($channel), - ]; - } + $this->updateChannels(); $this->startTimer(); + $this->listenCommands(); } public function joinChannels() { @@ -45,7 +39,7 @@ class TwitchChatBot extends TwitchBot { if ($msg->nick == 'horstiebot') return; $channel = $this->getMessageChannel($msg); if (!$channel) return; - $this->tagChannelRead($channel); + $this->tagChannelRead($channel, $msg); } @@ -56,10 +50,17 @@ class TwitchChatBot extends TwitchBot { $this->decideSend($channel); } }); + $this->getLoop()->addPeriodicTimer(60, function () { + $this->updateChannels(); + }); + } + + private function updateChannels() { + $this->channels = Channel::where('twitch_chat', '!=', '')->where('chat', '=', true)->get(); } private function decideSend(Channel $channel) { - $notes = $this->notes[$channel->id]; + $notes = $this->getNotes($channel); if ($notes['read_since_last_write'] < $notes['wait_msgs']) { return; } @@ -70,35 +71,219 @@ class TwitchChatBot extends TwitchBot { // don't immediately respond if we crossed the msg threshold last return; } - $text = $this->randomMsg($channel); + $text = $this->contextualMsg($channel); + if (!$text) $text = $this->randomMsg($channel); if (!$text) return; $this->tagChannelWrite($channel); $this->sendIRCMessage(IRCMessage::privmsg($channel->twitch_chat, $text)); } - private function randomMsg(Channel $channel) { - $line = ChatLog::where('type', '=', 'chat') + 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] = [ + 'last_read' => 0, + 'last_special' => '', + 'last_write' => time(), + 'latest_msgs' => [], + 'read_since_last_write' => 0, + 'wait_msgs' => $this->randomWaitMsgs($channel), + 'wait_time' => $this->randomWaitTime($channel), + ]; + } + return $this->notes[$channel->id]; + } + + private function getNote(Channel $channel, $name, $default = null) { + $notes = $this->getNotes($channel); + if (array_key_exists($name, $notes)) { + return $notes[$name]; + } + return $default; + } + + private function setNote(Channel $channel, $name, $value) { + $this->getNotes($channel); + $this->notes[$channel->id][$name] = $value; + } + + private function checkForGG(Channel $channel) { + $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')) { + ++$ggs; + } + } + return $ggs > 2; + } + + private function checkForGLHF(Channel $channel) { + $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'])) { + ++$gls; + } + } + return $gls > 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'])) { + ++$lulz; + } + } + return $lulz > 2; + } + + private function checkForNumbers(Channel $channel) { + $notes = $this->getNotes($channel); + $numbers = 0; + foreach ($notes['latest_msgs'] as $text) { + if (is_numeric(trim($text))) { + ++$numbers; + } + } + return $numbers > 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); + } + if ($last != 'number' && $this->checkForNumbers($channel)) { + $this->setNote($channel, 'last_special', 'number'); + return $this->randomContextualNumber($channel); + } + if ($last != 'lol' && $this->checkForLaughter($channel)) { + $this->setNote($channel, 'last_special', 'lol'); + return $this->randomLaughter($channel); + } + if ($last != 'glhf' && $this->checkForGLHF($channel)) { + $this->setNote($channel, 'last_special', 'glhf'); + return $this->randomGLHF($channel); + } + return false; + } + + private function queryChatlog(Channel $channel) { + return ChatLog::where('type', '=', 'chat') ->where('banned', '=', false) ->where('created_at', '<', now()->sub(1, 'day')) - ->inRandomOrder() + ->where(function ($query) use ($channel) { + $query->whereNull('detected_language'); + $query->orWhereIn('detected_language', $channel->languages); + }) + ->inRandomOrder(); + } + + private function randomContextualNumber(Channel $channel) { + $notes = $this->getNotes($channel); + $min = 100000; + $max = 0; + foreach ($notes['latest_msgs'] as $text) { + if (is_numeric(trim($text))) { + $number = intval(trim($text)); + $min = min($min, $number); + $max = max($max, $number); + } + } + 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:', + '4Head', + 'CarlSmile', + 'CruW', + 'DendiFace', + 'EleGiggle', + 'GunRun', + 'heh', + 'Hhhehehe', + 'HypeLUL', + 'Jebaited', + 'Jebasted', + 'KEKW', + 'KEKHeim', + 'KKona', + 'KomodoHype', + 'MaxLOL', + 'MingLee', + 'lol', + 'LOL!', + 'LUL', + 'OneHand', + 'SeemsGood', + 'ShadyLulu', + 'SoonerLater', + 'SUBprise', + 'xD', + 'YouDontSay', + ]); + } + + private function randomMsg(Channel $channel) { + $line = $this->queryChatlog($channel)->first(); + return $line->text_content; + } + private function randomWaitMsgs(Channel $channel) { - return random_int(1, 10); + $min = $this->getChatSetting($channel, 'wait_msgs_min', 1); + $max = $this->getChatSetting($channel, 'wait_msgs_max', 10); + return random_int($min, $max); } private function randomWaitTime(Channel $channel) { - return random_int(1, 1800); + $min = $this->getChatSetting($channel, 'wait_time_min', 1); + $max = $this->getChatSetting($channel, 'wait_time_max', 900); + return random_int($min, $max); } - private function tagChannelRead(Channel $channel) { + private function tagChannelRead(Channel $channel, IRCMessage $msg) { + $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']); + } } private function tagChannelWrite(Channel $channel) { + $this->getNotes($channel); $this->notes[$channel->id]['last_write'] = time(); $this->notes[$channel->id]['read_since_last_write'] = 0; $this->notes[$channel->id]['wait_msgs'] = $this->randomWaitMsgs($channel);