X-Git-Url: https://git.localhorst.tv/?a=blobdiff_plain;f=app%2FTwitchBot%2FTwitchBot.php;h=ddc51fe046b2e68d448a18b04cc272ab8702a8c3;hb=e10222af705e3475fcea6e0b17d1c9984a62db26;hp=3ca2db9283e6651e521fa2fe4aeaa596d94f9e2e;hpb=898d01d4ac5ccaa23621abda0761a893ff8c1074;p=alttp.git diff --git a/app/TwitchBot/TwitchBot.php b/app/TwitchBot/TwitchBot.php index 3ca2db9..ddc51fe 100644 --- a/app/TwitchBot/TwitchBot.php +++ b/app/TwitchBot/TwitchBot.php @@ -3,6 +3,7 @@ namespace App\TwitchBot; use App\Models\Channel; +use App\Models\TwitchBotCommand; use App\Models\TwitchToken; use Monolog\Handler\StreamHandler; use Monolog\Logger; @@ -15,7 +16,7 @@ class TwitchBot { public function __construct() { $this->logger = new Logger('TwitchBot'); - $this->logger->pushHandler(new StreamHandler('php://stdout', Logger::DEBUG)); + $this->logger->pushHandler(new StreamHandler('php://stdout', Logger::INFO)); $this->token = TwitchToken::firstWhere('nick', 'localhorsttv'); if (!$this->token) { @@ -24,6 +25,7 @@ class TwitchBot { $this->connector = new Connector(); $this->connect(); + $this->listenCommands(); } public function getLogger() { @@ -75,12 +77,13 @@ class TwitchBot { public function handleWsMessage(Message $message, WebSocket $ws) { $irc_messages = explode("\r\n", rtrim($message->getPayload(), "\r\n")); foreach ($irc_messages as $irc_message) { - $this->logger->debug('received IRC message '.$irc_message); + $this->logger->info('received IRC message '.$irc_message); $this->handleIRCMessage(IRCMessage::fromString($irc_message)); } } public function handleWsClose(int $op, string $reason) { + $this->ready = false; $this->logger->info('websocket connection closed: '.$reason.' ['.$op.']'); if (!$this->shutting_down) { $this->logger->info('reconnecting in 10 seconds'); @@ -111,6 +114,7 @@ class TwitchBot { if ($msg->command == '001') { // successful login $this->joinChannels(); + $this->ready = true; return; } } @@ -122,7 +126,20 @@ class TwitchBot { if ($text[0] != '!') return; $channel = Channel::firstWhere('twitch_chat', '=', $target); if (!$channel) return; - $this->logger->info('got command '.$text.' on channel '.$channel->title); + $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->logger->info('got command '.$cmd[0].' on channel '.$channel->title); + try { + $command = ChatCommand::create($this, $channel, $config); + $command->execute($cmd[1] ?? ''); + } catch (\Exception $e) { + $this->logger->warning('error executing command '.$cmd[0].' on channel '.$channel->title.': '.$e->getMessage()); + } } public function login() { @@ -132,7 +149,7 @@ class TwitchBot { public function joinChannels() { $this->logger->info('joining channels'); - $channels = Channel::where('twitch_chat', '!=', '')->get(); + $channels = Channel::where('twitch_chat', '!=', '')->where('join', '=', true)->get(); $names = []; foreach ($channels as $channel) { $names[] = $channel->twitch_chat; @@ -143,9 +160,23 @@ class TwitchBot { } } + private function listenCommands() { + $this->getLoop()->addPeriodicTimer(1, function () { + if (!$this->ready) return; + $command = TwitchBotCommand::where('status', '=', 'pending')->oldest()->first(); + if ($command) { + try { + $command->execute($this); + } catch (\Exception $e) { + } + } + }); + + } + public function sendIRCMessage(IRCMessage $msg) { $irc_message = $msg->encode(); - $this->logger->debug('sending IRC message '.$irc_message); + $this->logger->info('sending IRC message '.$irc_message); $this->ws->send($irc_message); } @@ -156,6 +187,7 @@ class TwitchBot { private $connector; private $ws; + private $ready = false; private $shutting_down = false; }