]> git.localhorst.tv Git - alttp.git/blob - app/TwitchBot/TwitchChatBot.php
add chat bot
[alttp.git] / app / TwitchBot / TwitchChatBot.php
1 <?php
2
3 namespace App\TwitchBot;
4
5 use App\Models\Channel;
6 use App\Models\ChatLog;
7
8 class TwitchChatBot extends TwitchBot {
9
10         public function __construct() {
11                 parent::__construct('horstiebot');
12                 $this->channels = Channel::where('twitch_chat', '!=', '')->where('chat', '=', true)->get();
13                 foreach ($this->channels as $channel) {
14                         $this->notes[$channel->id] = [
15                                 'last_read' => 0,
16                                 'last_write' => time(),
17                                 'read_since_last_write' => 0,
18                                 'wait_msgs' => $this->randomWaitMsgs($channel),
19                                 'wait_time' => $this->randomWaitTime($channel),
20                         ];
21                 }
22                 $this->startTimer();
23         }
24
25         public function joinChannels() {
26                 $this->getLogger()->info('joining channels');
27                 $names = [];
28                 foreach ($this->channels as $channel) {
29                         $names[] = $channel->twitch_chat;
30                 }
31                 $chunks = array_chunk($names, 10);
32                 foreach ($chunks as $chunk) {
33                         $this->sendIRCMessage(IRCMessage::join($chunk));
34                 }
35         }
36
37         public function logMessage(IRCMessage $msg) {
38                 $channel = $this->getMessageChannel($msg);
39                 if ($channel && !$channel->join) {
40                         $msg->log();
41                 }
42         }
43
44         public function handlePrivMsg(IRCMessage $msg) {
45                 if ($msg->nick == 'horstiebot') return;
46                 $channel = $this->getMessageChannel($msg);
47                 if (!$channel) return;
48                 $this->tagChannelRead($channel);
49         }
50
51
52         private function startTimer() {
53                 $this->getLoop()->addPeriodicTimer(1, function () {
54                         if (!$this->isReady()) return;
55                         foreach ($this->channels as $channel) {
56                                 $this->decideSend($channel);
57                         }
58                 });
59         }
60
61         private function decideSend(Channel $channel) {
62                 $notes = $this->notes[$channel->id];
63                 if ($notes['read_since_last_write'] < $notes['wait_msgs']) {
64                         return;
65                 }
66                 if (time() - $notes['last_write'] < $notes['wait_time']) {
67                         return;
68                 }
69                 if ($notes['read_since_last_write'] == $notes['wait_msgs'] && time() - $notes['last_read'] < 3) {
70                         // don't immediately respond if we crossed the msg threshold last
71                         return;
72                 }
73                 $text = $this->randomMsg($channel);
74                 if (!$text) return;
75                 $this->tagChannelWrite($channel);
76                 $this->sendIRCMessage(IRCMessage::privmsg($channel->twitch_chat, $text));
77         }
78
79         private function randomMsg(Channel $channel) {
80                 $line = ChatLog::where('type', '=', 'chat')->where('banned', '=', false)->inRandomOrder()->first();
81                 return $line->text_content;
82         }
83
84         private function randomWaitMsgs(Channel $channel) {
85                 return random_int(1, 10);
86         }
87
88         private function randomWaitTime(Channel $channel) {
89                 return random_int(1, 1800);
90         }
91
92         private function tagChannelRead(Channel $channel) {
93                 $this->notes[$channel->id]['last_read'] = time();
94                 ++$this->notes[$channel->id]['read_since_last_write'];
95         }
96
97         private function tagChannelWrite(Channel $channel) {
98                 $this->notes[$channel->id]['last_write'] = time();
99                 $this->notes[$channel->id]['read_since_last_write'] = 0;
100                 $this->notes[$channel->id]['wait_msgs'] = $this->randomWaitMsgs($channel);
101                 $this->notes[$channel->id]['wait_time'] = $this->randomWaitTime($channel);
102         }
103
104         private $channels;
105         private $notes = [];
106
107 }