]> git.localhorst.tv Git - alttp.git/blob - app/TwitchBot/TwitchAppBot.php
chat bot settings
[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                 $text = $msg->getText();
22                 if ($text[0] != '!') return;
23                 $channel = $this->getMessageChannel($msg);
24                 if (!$channel) return;
25                 $this->handleChatCommand($channel, $msg);
26         }
27
28         public function handleChatCommand(Channel $channel, IRCMessage $msg) {
29                 $cmd = explode(' ', ltrim($msg->getText(), '!'), 2);
30                 if (!isset($channel->chat_commands[$cmd[0]])) return;
31                 $config = $channel->chat_commands[$cmd[0]];
32                 $this->getLogger()->info('got command '.$cmd[0].' on channel '.$channel->title);
33                 try {
34                         $command = ChatCommand::create($this, $channel, $config);
35                         $command->execute($cmd[1] ?? '');
36                 } catch (\Exception $e) {
37                         $this->getLogger()->warning('error executing command '.$cmd[0].' on channel '.$channel->title.': '.$e->getMessage());
38                 }
39         }
40
41         public function joinChannels() {
42                 $this->getLogger()->info('joining channels');
43                 $channels = Channel::where('twitch_chat', '!=', '')->where('join', '=', true)->get();
44                 $names = [];
45                 foreach ($channels as $channel) {
46                         $names[] = $channel->twitch_chat;
47                 }
48                 $chunks = array_chunk($names, 10);
49                 foreach ($chunks as $chunk) {
50                         $this->sendIRCMessage(IRCMessage::join($chunk));
51                 }
52         }
53
54 }