]> git.localhorst.tv Git - alttp.git/blobdiff - app/TwitchBot/TwitchBot.php
first simple twitch commands
[alttp.git] / app / TwitchBot / TwitchBot.php
index 3ca2db9283e6651e521fa2fe4aeaa596d94f9e2e..3cba5d958bac4975fd2e3fdf93fd901208ffcc18 100644 (file)
@@ -15,7 +15,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) {
@@ -122,7 +122,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() {