diff --git a/src/ModularDiscord/IntractableConsole.php b/src/ModularDiscord/IntractableConsole.php index 3fb150e..5ba78b5 100644 --- a/src/ModularDiscord/IntractableConsole.php +++ b/src/ModularDiscord/IntractableConsole.php @@ -27,35 +27,39 @@ public static function listenForCommands(ModularDiscord $modDiscord): void if (self::$loaded) return; self::$loaded = true; - $discord = $modDiscord->discord; if (self::isRunningWindows()) { - $discord->getLogger()->warning("Failed to initialize console command listener due to 'stream_set_blocking' function not being supported on Windows."); + $modDiscord->logger->warning("Failed to initialize console command listener due to 'stream_set_blocking' function not being supported on Windows."); return; } - $discord->getLogger()->info('Listening for console commands...'); - if (stream_set_blocking(STDIN, false)) - { - $discord->getLoop()->addPeriodicTimer(1, function () use ($modDiscord, $discord) { - try { - $line = fgets(self::$stdin); - $split = explode(' ', $line); - $name = strtolower($split[0]); - $args = []; - if (count($split) > 1) - $args = array_slice($split, 1); - self::handleCommand($modDiscord, $name, $args); - } catch (Exception | Error $ex) { - $discord->getLogger()->error("Error handling console command: {$ex->getMessage()}", [ - 'line' => $ex->getLine(), - 'file' => $ex->getFile() - ]); - } - }); - return; + try { + $modDiscord->logger->info('Listening for console commands...'); + self::$stdin = STDIN; + if (stream_set_blocking(self::$stdin, false)) { + $modDiscord->discord->getLoop()->addPeriodicTimer(1, function () use ($modDiscord) { + try { + $line = fgets(self::$stdin); + if (empty($line)) + return; + $line = trim(substr($line, 0, -1)); + $exploded = explode(' ', $line); + $name = trim(strtolower(str_contains($line, " ") ? $exploded[0] : $line)); + $args = array_slice($exploded, 1); + self::handleCommand($modDiscord, $name, $args); + } catch (Exception|Error $ex) { + $modDiscord->logger->error("Error handling console command: {$ex->getMessage()}", [ + 'line' => $ex->getLine(), + 'file' => $ex->getFile() + ]); + } + }); + return; + } + $modDiscord->logger->error('stream_set_blocking(...) failed: console input handler not initialized.'); + } catch (Exception $e) { + $modDiscord->logger->error("Failed to initialize console command listener: {$e->getMessage()}", ['type' => get_class($e)]); } - $discord->getLogger()->error('stream_set_blocking(...) failed: console input handler not initialized.'); } public static function closeConsoleStream(): void diff --git a/src/ModularDiscord/Registry.php b/src/ModularDiscord/Registry.php index 28247b7..3619dda 100644 --- a/src/ModularDiscord/Registry.php +++ b/src/ModularDiscord/Registry.php @@ -1,5 +1,7 @@ toArray()); $guild = $guild ?? $command->getGuild(); - if ($guild != null and !$discord->guilds->has([$guild])) { + if ($guild != null and !$discord->guilds->has($guild)) { $this->module->logger->error("Couldn't register '$name' command as guild with an ID of '$guild' does not exist!"); return null; }