X-Git-Url: https://git.localhorst.tv/?a=blobdiff_plain;f=app%2FTwitchBot%2FTwitchAppBot.php;fp=app%2FTwitchBot%2FTwitchAppBot.php;h=4e2c7d493ff5050c36fa6d0ed37dcfeb0fb0260b;hb=85879ea0c27ce6506919e2c083a139c470c0952c;hp=0000000000000000000000000000000000000000;hpb=e8eb106aa5adab6dd992390cb3836589e4163e72;p=alttp.git diff --git a/app/TwitchBot/TwitchAppBot.php b/app/TwitchBot/TwitchAppBot.php new file mode 100644 index 0000000..4e2c7d4 --- /dev/null +++ b/app/TwitchBot/TwitchAppBot.php @@ -0,0 +1,69 @@ +listenCommands(); + } + + public function logMessage(IRCMessage $msg) { + $msg->log(); + } + + public function handlePrivMsg(IRCMessage $msg) { + $target = $msg->getPrivMsgTarget(); + if ($target[0] != '#') return; // direct message + $text = $msg->getText(); + if ($text[0] != '!') return; + $channel = $this->getMessageChannel($msg); + if (!$channel) return; + $this->handleChatCommand($channel, $msg); + } + + public function handleChatCommand(Channel $channel, IRCMessage $msg) { + $cmd = explode(' ', ltrim($msg->getText(), '!'), 2); + if (!isset($channel->chat_commands[$cmd[0]])) return; + $config = $channel->chat_commands[$cmd[0]]; + $this->getLogger()->info('got command '.$cmd[0].' on channel '.$channel->title); + try { + $command = ChatCommand::create($this, $channel, $config); + $command->execute($cmd[1] ?? ''); + } catch (\Exception $e) { + $this->getLogger()->warning('error executing command '.$cmd[0].' on channel '.$channel->title.': '.$e->getMessage()); + } + } + + public function joinChannels() { + $this->getLogger()->info('joining channels'); + $channels = Channel::where('twitch_chat', '!=', '')->where('join', '=', true)->get(); + $names = []; + foreach ($channels as $channel) { + $names[] = $channel->twitch_chat; + } + $chunks = array_chunk($names, 10); + foreach ($chunks as $chunk) { + $this->sendIRCMessage(IRCMessage::join($chunk)); + } + } + + + private function listenCommands() { + $this->getLoop()->addPeriodicTimer(1, function () { + if (!$this->isReady()) return; + $command = TwitchBotCommand::where('status', '=', 'pending')->oldest()->first(); + if ($command) { + try { + $command->execute($this); + } catch (\Exception $e) { + } + } + }); + } + +}