]> git.localhorst.tv Git - alttp.git/blob - app/TwitchBot/TwitchAppBot.php
a103a8847316c3ff8595a85a13a5459ea94e1f1b
[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                 $cmd_name = strtolower($cmd[0]);
45                 if (!isset($channel->chat_commands[$cmd_name])) return;
46                 $config = $channel->chat_commands[$cmd_name];
47                 $this->getLogger()->info('got command '.$cmd_name.' on channel '.$channel->title);
48                 try {
49                         $command = ChatCommand::create($this, $channel, $config);
50                         if ($command->checkAccess($msg)) {
51                                 $command->execute($cmd[1] ?? '');
52                         }
53                 } catch (\Exception $e) {
54                         $this->getLogger()->warning('error executing command '.$cmd_name.' on channel '.$channel->title.': '.$e->getMessage());
55                 }
56         }
57
58         public function joinChannels() {
59                 $this->getLogger()->info('joining channels');
60                 $channels = Channel::where('twitch_chat', '!=', '')->where('join', '=', true)->get();
61                 $names = [];
62                 foreach ($channels as $channel) {
63                         $names[] = $channel->twitch_chat;
64                 }
65                 $chunks = array_chunk($names, 10);
66                 foreach ($chunks as $chunk) {
67                         $this->sendIRCMessage(IRCMessage::join($chunk));
68                 }
69         }
70
71 }