X-Git-Url: https://git.localhorst.tv/?a=blobdiff_plain;f=app%2FTwitchBot%2FTwitchChatBot.php;h=0896c2b98017b77808630929ff4f47ce9c0b8852;hb=e49b130505f5712075dca2ff990e5a63fc90ce3c;hp=7a6eb1c1357cb9c59d12c2bfc9af81467383ae53;hpb=06fbdc15c8db57590c9b6a38ee1f00d5f349cff9;p=alttp.git diff --git a/app/TwitchBot/TwitchChatBot.php b/app/TwitchBot/TwitchChatBot.php index 7a6eb1c..0896c2b 100644 --- a/app/TwitchBot/TwitchChatBot.php +++ b/app/TwitchBot/TwitchChatBot.php @@ -9,16 +9,7 @@ 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(); } @@ -57,10 +48,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; } @@ -77,6 +75,26 @@ class TwitchChatBot extends TwitchBot { $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) @@ -91,19 +109,25 @@ class TwitchChatBot extends TwitchBot { } 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, 900); + $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);