updateChannels(); $this->startTimer(); $this->listenCommands(); } public function joinChannels() { $this->getLogger()->info('joining channels'); $names = []; foreach ($this->channels as $channel) { $names[] = $channel->twitch_chat; } $chunks = array_chunk($names, 10); foreach ($chunks as $chunk) { $this->sendIRCMessage(IRCMessage::join($chunk)); } } public function logMessage(IRCMessage $msg) { $channel = $this->getMessageChannel($msg); if ($channel && !$channel->join) { $msg->log(); } } public function handlePrivMsg(IRCMessage $msg) { if ($msg->nick == 'horstiebot') return; $channel = $this->getMessageChannel($msg); if (!$channel) return; $this->tagChannelRead($channel); } private function startTimer() { $this->getLoop()->addPeriodicTimer(1, function () { if (!$this->isReady()) return; foreach ($this->channels as $channel) { $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->getNotes($channel); if ($notes['read_since_last_write'] < $notes['wait_msgs']) { return; } if (time() - $notes['last_write'] < $notes['wait_time']) { return; } if ($notes['read_since_last_write'] == $notes['wait_msgs'] && time() - $notes['last_read'] < 3) { // don't immediately respond if we crossed the msg threshold last return; } $text = $this->randomMsg($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] = [ 'last_read' => 0, 'last_write' => time(), 'read_since_last_write' => 0, 'wait_msgs' => $this->randomWaitMsgs($channel), 'wait_time' => $this->randomWaitTime($channel), ]; } return $this->notes[$channel->id]; } private function randomMsg(Channel $channel) { $line = 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() ->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); 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); return random_int($min, $max); } private function tagChannelRead(Channel $channel) { $this->getNotes($channel); $this->notes[$channel->id]['last_read'] = time(); ++$this->notes[$channel->id]['read_since_last_write']; } 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); $this->notes[$channel->id]['wait_time'] = $this->randomWaitTime($channel); } private $channels; private $notes = []; }