]> git.localhorst.tv Git - alttp.git/blob - app/TwitchBot/TwitchAppBot.php
13499f5d46b38d5913e3994e43ce09dc43250a15
[alttp.git] / app / TwitchBot / TwitchAppBot.php
1 <?php
2
3 namespace App\TwitchBot;
4
5 use App\Models\Channel;
6
7 class TwitchAppBot extends TwitchBot {
8
9         public function __construct() {
10                 parent::__construct('localhorsttv');
11                 $this->listenCommands();
12         }
13
14         public function logMessage(IRCMessage $msg) {
15                 $msg->log();
16         }
17
18         public function handlePrivMsg(IRCMessage $msg) {
19                 $target = $msg->getPrivMsgTarget();
20                 if ($target[0] != '#') return; // direct message
21                 $channel = $this->getMessageChannel($msg);
22                 if (!$channel) return;
23                 $text = $msg->getText();
24                 if ($text[0] == '!') {
25                         $this->handleChatCommand($channel, $msg);
26                 } else if (
27                         $channel->isAcceptingGuesses() &&
28                         !empty($msg->tags['user-id']) &&
29                         !empty($msg->tags['display-name'] &&
30                         $channel->isValidGuess($text))
31                 ) {
32                         $uid = 't:'.$msg->tags['user-id'];
33                         $uname = $msg->tags['display-name'];
34                         try {
35                                 $channel->registerGuess($uid, $uname, $text);
36                         } catch (\Exception $e) {
37                                 $this->getLogger()->warning('error registering guess "'.$text.'" on channel '.$channel->title.': '.$e->getMessage());
38                         }
39                 }
40         }
41
42         public function handleChatCommand(Channel $channel, IRCMessage $msg) {
43                 $cmd = explode(' ', ltrim($msg->getText(), '!'), 2);
44                 if (!isset($channel->chat_commands[$cmd[0]])) return;
45                 $config = $channel->chat_commands[$cmd[0]];
46                 $this->getLogger()->info('got command '.$cmd[0].' on channel '.$channel->title);
47                 try {
48                         $command = ChatCommand::create($this, $channel, $config);
49                         if ($command->checkAccess($msg)) {
50                                 $command->execute($cmd[1] ?? '');
51                         }
52                 } catch (\Exception $e) {
53                         $this->getLogger()->warning('error executing command '.$cmd[0].' on channel '.$channel->title.': '.$e->getMessage());
54                 }
55         }
56
57         public function joinChannels() {
58                 $this->getLogger()->info('joining channels');
59                 $channels = Channel::where('twitch_chat', '!=', '')->where('join', '=', true)->get();
60                 $names = [];
61                 foreach ($channels as $channel) {
62                         $names[] = $channel->twitch_chat;
63                 }
64                 $chunks = array_chunk($names, 10);
65                 foreach ($chunks as $chunk) {
66                         $this->sendIRCMessage(IRCMessage::join($chunk));
67                 }
68         }
69
70 }