3 namespace App\TwitchBot;
5 use App\Models\Channel;
6 use App\Models\ChatLog;
7 use Illuminate\Support\Arr;
8 use Illuminate\Support\Str;
10 class TwitchChatBot extends TwitchBot {
12 public function __construct() {
13 parent::__construct('horstiebot');
14 $this->updateChannels();
16 $this->listenCommands();
19 public function joinChannels() {
20 $this->getLogger()->info('joining channels');
22 foreach ($this->channels as $channel) {
23 $names[] = $channel->twitch_chat;
25 $chunks = array_chunk($names, 10);
26 foreach ($chunks as $chunk) {
27 $this->sendIRCMessage(IRCMessage::join($chunk));
31 public function logMessage(IRCMessage $msg) {
32 $channel = $this->getMessageChannel($msg);
33 if ($channel && !$channel->join) {
38 public function handlePrivMsg(IRCMessage $msg) {
39 if ($msg->nick == 'horstiebot') return;
40 $channel = $this->getMessageChannel($msg);
41 if (!$channel) return;
42 $this->tagChannelRead($channel, $msg);
46 private function startTimer() {
47 $this->getLoop()->addPeriodicTimer(1, function () {
48 if (!$this->isReady()) return;
49 foreach ($this->channels as $channel) {
50 $this->decideSend($channel);
53 $this->getLoop()->addPeriodicTimer(60, function () {
54 $this->updateChannels();
58 private function updateChannels() {
59 $this->channels = Channel::where('twitch_chat', '!=', '')->where('chat', '=', true)->get();
62 private function decideSend(Channel $channel) {
63 $notes = $this->getNotes($channel);
64 if ($notes['read_since_last_write'] < $notes['wait_msgs']) {
67 if (time() - $notes['last_write'] < $notes['wait_time']) {
70 if ($notes['read_since_last_write'] == $notes['wait_msgs'] && time() - $notes['last_read'] < 3) {
71 // don't immediately respond if we crossed the msg threshold last
74 $text = $this->contextualMsg($channel);
75 if (!$text) $text = $this->randomChat($channel);
77 $this->tagChannelWrite($channel);
78 $this->sendIRCMessage(IRCMessage::privmsg($channel->twitch_chat, $text));
81 private function getNotes(Channel $channel) {
82 if (!isset($this->notes[$channel->id])) {
83 $this->notes[$channel->id] = [
86 'last_write' => time(),
88 'read_since_last_write' => 0,
89 'wait_msgs' => $this->randomWaitMsgs($channel),
90 'wait_time' => $this->randomWaitTime($channel),
93 return $this->notes[$channel->id];
96 private function getNote(Channel $channel, $name, $default = null) {
97 $notes = $this->getNotes($channel);
98 if (array_key_exists($name, $notes)) {
104 private function setNote(Channel $channel, $name, $value) {
105 $this->getNotes($channel);
106 $this->notes[$channel->id][$name] = $value;
109 private function checkForGG(Channel $channel) {
110 $notes = $this->getNotes($channel);
112 foreach ($notes['latest_msgs'] as $text) {
113 if (ChatLog::classify($text) == 'gg') {
120 private function checkForGLHF(Channel $channel) {
121 $notes = $this->getNotes($channel);
123 foreach ($notes['latest_msgs'] as $text) {
124 if (ChatLog::classify($text) == 'gl') {
131 private function checkForGreeting(Channel $channel) {
132 $notes = $this->getNotes($channel);
134 foreach ($notes['latest_msgs'] as $text) {
135 if (ChatLog::classify($text) == 'hi') {
142 private function checkForHype(Channel $channel) {
143 $notes = $this->getNotes($channel);
145 foreach ($notes['latest_msgs'] as $text) {
146 if (ChatLog::classify($text) == 'hype') {
153 private function checkForLaughter(Channel $channel) {
154 $notes = $this->getNotes($channel);
156 foreach ($notes['latest_msgs'] as $text) {
157 if (ChatLog::classify($text) == 'lol') {
164 private function checkForNumbers(Channel $channel) {
165 $notes = $this->getNotes($channel);
167 foreach ($notes['latest_msgs'] as $text) {
168 if (is_numeric(trim($text))) {
175 private function checkForPog(Channel $channel) {
176 $notes = $this->getNotes($channel);
178 foreach ($notes['latest_msgs'] as $text) {
179 if (ChatLog::classify($text) == 'pog') {
186 private function checkForSalute(Channel $channel) {
187 $notes = $this->getNotes($channel);
189 foreach ($notes['latest_msgs'] as $text) {
190 if (ChatLog::classify($text) == 'o7') {
197 private function contextualMsg(Channel $channel) {
198 $last = $this->getNote($channel, 'last_special');
199 if ($last != 'gg' && $this->checkForGG($channel)) {
200 $this->setNote($channel, 'last_special', 'gg');
201 return $channel->randomOfClass('gg');
203 if ($last != 'number' && $this->checkForNumbers($channel)) {
204 $this->setNote($channel, 'last_special', 'number');
205 return $this->randomContextualNumber($channel);
207 if ($last != 'lol' && $this->checkForLaughter($channel)) {
208 $this->setNote($channel, 'last_special', 'lol');
209 return $this->randomLaughter($channel);
211 if ($last != 'glhf' && $this->checkForGLHF($channel)) {
212 $this->setNote($channel, 'last_special', 'glhf');
213 return $channel->randomOfClass('gl');
215 if ($last != 'hi' && $this->checkForGreeting($channel)) {
216 $this->setNote($channel, 'last_special', 'hi');
217 return $channel->randomOfClass('hi');
219 if ($last != 'hype' && $this->checkForHype($channel)) {
220 $this->setNote($channel, 'last_special', 'hype');
221 return $channel->randomOfClass('hype');
223 if ($last != 'pog' && $this->checkForPog($channel)) {
224 $this->setNote($channel, 'last_special', 'pog');
225 return $channel->randomOfClass('pog');
227 if ($last != 'o7' && $this->checkForSalute($channel)) {
228 $this->setNote($channel, 'last_special', 'o7');
229 return $channel->randomOfClass('o7');
234 private function randomChat(Channel $channel) {
235 $line = $channel->queryChatlog()
236 ->whereIn('classification', ['hi', 'hype', 'lol', 'pog', 'unclassified'])
238 return $line->text_content;
241 private function randomContextualNumber(Channel $channel) {
242 $notes = $this->getNotes($channel);
245 foreach ($notes['latest_msgs'] as $text) {
246 if (is_numeric(trim($text))) {
247 $number = intval(trim($text));
248 $min = min($min, $number);
249 $max = max($max, $number);
252 return random_int($min, $max);
255 private function randomLaughter(Channel $channel) {
285 $channel->randomOfClass('lol'),
289 private function randomMsg(Channel $channel) {
290 $line = $channel->queryChatlog()->first();
291 return $line->text_content;
294 private function randomWaitMsgs(Channel $channel) {
295 $min = $channel->getChatSetting('wait_msgs_min', 1);
296 $max = $channel->getChatSetting('wait_msgs_max', 10);
297 return random_int($min, $max);
300 private function randomWaitTime(Channel $channel) {
301 $min = $channel->getChatSetting('wait_time_min', 1);
302 $max = $channel->getChatSetting('wait_time_max', 900);
303 return random_int($min, $max);
306 private function tagChannelRead(Channel $channel, IRCMessage $msg) {
307 $this->getNotes($channel);
308 $this->notes[$channel->id]['last_read'] = time();
309 ++$this->notes[$channel->id]['read_since_last_write'];
310 if (!ChatLog::isKnownBot($msg->nick) && !ChatLog::spammyText($msg->getText())) {
311 $this->notes[$channel->id]['latest_msgs'][] = $msg->getText();
312 if (count($this->notes[$channel->id]['latest_msgs']) > 10) {
313 array_shift($this->notes[$channel->id]['latest_msgs']);
316 if ($this->isDirectedAtMe($msg->getText()) && $this->shouldRespond($channel)) {
317 $this->notes[$channel->id]['wait_msgs'] = 0;
318 $this->notes[$channel->id]['wait_time'] = 0;
322 private function tagChannelWrite(Channel $channel) {
323 $this->getNotes($channel);
324 $this->notes[$channel->id]['last_write'] = time();
325 $this->notes[$channel->id]['read_since_last_write'] = 0;
326 $this->notes[$channel->id]['wait_msgs'] = $this->randomWaitMsgs($channel);
327 $this->notes[$channel->id]['wait_time'] = $this->randomWaitTime($channel);
330 private function isDirectedAtMe($raw_text) {
331 $text = strtolower($raw_text);
332 if (strpos($text, 'horstie') !== false) {
338 private function shouldRespond(Channel $channel) {
339 $setting = $channel->getChatSetting('respond', 'yes');
340 if ($setting == 'yes') {
343 if ($setting == '50') {
344 return random_int(0, 1);