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();
}
$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;
}
$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)
}
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);