use App\Models\Channel;
use App\Models\ChatBotLog;
+use App\Models\ChatLib;
use App\Models\ChatLog;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
$this->updateChannels();
$this->startTimer();
$this->listenCommands();
+ $this->chatlib = new ChatLib();
+ $this->chatlib->loadFrom('de');
}
public function joinChannels() {
$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 () {
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;
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);
$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();
}
}
+ 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();
}
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];
}
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') {
private $channels;
private $notes = [];
+ private $chatlib;
}