From 41a7f22aaf98d11ccb4da55fafc63d9742579bd6 Mon Sep 17 00:00:00 2001 From: Tautvydas Date: Sun, 20 Aug 2023 11:27:47 +0300 Subject: [PATCH 1/8] Exclude example --- .gitattributes | 1 + 1 file changed, 1 insertion(+) create mode 100644 .gitattributes diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..a97fcb6 --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +example/ export-ignore \ No newline at end of file From fbb56444dc9abbf925ab7826493b382b7b9c8efa Mon Sep 17 00:00:00 2001 From: Tautvydas Date: Sun, 20 Aug 2023 11:29:36 +0300 Subject: [PATCH 2/8] Fix silly bug --- src/ModularDiscord/Registry.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) 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; } From 0d7ac20609671e2c03f6862b371775b6a8f46d17 Mon Sep 17 00:00:00 2001 From: Tautvydas Date: Sun, 20 Aug 2023 12:23:58 +0300 Subject: [PATCH 3/8] Remove .gitattributes --- .gitattributes | 1 - 1 file changed, 1 deletion(-) delete mode 100644 .gitattributes diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index a97fcb6..0000000 --- a/.gitattributes +++ /dev/null @@ -1 +0,0 @@ -example/ export-ignore \ No newline at end of file From 294f30847f5b614fbbc877034458873af1b75d32 Mon Sep 17 00:00:00 2001 From: Tautvydas Date: Sun, 20 Aug 2023 12:26:44 +0300 Subject: [PATCH 4/8] Add exception handler to listenForCommands --- src/ModularDiscord/IntractableConsole.php | 45 ++++++++++++----------- 1 file changed, 24 insertions(+), 21 deletions(-) diff --git a/src/ModularDiscord/IntractableConsole.php b/src/ModularDiscord/IntractableConsole.php index 3fb150e..02497a8 100644 --- a/src/ModularDiscord/IntractableConsole.php +++ b/src/ModularDiscord/IntractableConsole.php @@ -34,28 +34,31 @@ public static function listenForCommands(ModularDiscord $modDiscord): void 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 { + $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; + } + $discord->getLogger()->error('stream_set_blocking(...) failed: console input handler not initialized.'); + } catch (Exception $e) { + $discord->getLogger()->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 From fa1551f0894b1ab006d11406f44dbc894ebf20ab Mon Sep 17 00:00:00 2001 From: Tautvydas Date: Sun, 20 Aug 2023 12:37:55 +0300 Subject: [PATCH 5/8] Forgot to set a variable lol --- src/ModularDiscord/IntractableConsole.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/ModularDiscord/IntractableConsole.php b/src/ModularDiscord/IntractableConsole.php index 02497a8..92a1fe1 100644 --- a/src/ModularDiscord/IntractableConsole.php +++ b/src/ModularDiscord/IntractableConsole.php @@ -36,7 +36,8 @@ public static function listenForCommands(ModularDiscord $modDiscord): void try { $discord->getLogger()->info('Listening for console commands...'); - if (stream_set_blocking(STDIN, false)) { + self::$stdin = STDIN; + if (stream_set_blocking(self::$stdin, false)) { $discord->getLoop()->addPeriodicTimer(1, function () use ($modDiscord, $discord) { try { $line = fgets(self::$stdin); From 52a825f8d89370765e68b48567c5df248cd6b130 Mon Sep 17 00:00:00 2001 From: Tautvydas Date: Sun, 20 Aug 2023 12:47:58 +0300 Subject: [PATCH 6/8] Add check for empty input --- src/ModularDiscord/IntractableConsole.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/ModularDiscord/IntractableConsole.php b/src/ModularDiscord/IntractableConsole.php index 92a1fe1..b6005ec 100644 --- a/src/ModularDiscord/IntractableConsole.php +++ b/src/ModularDiscord/IntractableConsole.php @@ -41,6 +41,8 @@ public static function listenForCommands(ModularDiscord $modDiscord): void $discord->getLoop()->addPeriodicTimer(1, function () use ($modDiscord, $discord) { try { $line = fgets(self::$stdin); + if (empty($line)) + return; $split = explode(' ', $line); $name = strtolower($split[0]); $args = []; From ee9fa8d4dca54de950d0a468aeebc788e4341db8 Mon Sep 17 00:00:00 2001 From: Tautvydas Date: Sun, 20 Aug 2023 12:50:43 +0300 Subject: [PATCH 7/8] Fix commands not working --- src/ModularDiscord/IntractableConsole.php | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/ModularDiscord/IntractableConsole.php b/src/ModularDiscord/IntractableConsole.php index b6005ec..51869e6 100644 --- a/src/ModularDiscord/IntractableConsole.php +++ b/src/ModularDiscord/IntractableConsole.php @@ -43,11 +43,10 @@ public static function listenForCommands(ModularDiscord $modDiscord): void $line = fgets(self::$stdin); if (empty($line)) return; - $split = explode(' ', $line); - $name = strtolower($split[0]); - $args = []; - if (count($split) > 1) - $args = array_slice($split, 1); + $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) { $discord->getLogger()->error("Error handling console command: {$ex->getMessage()}", [ From 04f116a503e6abdf82efa76a894a7d20b2df169d Mon Sep 17 00:00:00 2001 From: Tautvydas Date: Sun, 20 Aug 2023 12:54:45 +0300 Subject: [PATCH 8/8] Use custom logger instead of discord's --- src/ModularDiscord/IntractableConsole.php | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/ModularDiscord/IntractableConsole.php b/src/ModularDiscord/IntractableConsole.php index 51869e6..5ba78b5 100644 --- a/src/ModularDiscord/IntractableConsole.php +++ b/src/ModularDiscord/IntractableConsole.php @@ -27,18 +27,17 @@ 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; } try { - $discord->getLogger()->info('Listening for console commands...'); + $modDiscord->logger->info('Listening for console commands...'); self::$stdin = STDIN; if (stream_set_blocking(self::$stdin, false)) { - $discord->getLoop()->addPeriodicTimer(1, function () use ($modDiscord, $discord) { + $modDiscord->discord->getLoop()->addPeriodicTimer(1, function () use ($modDiscord) { try { $line = fgets(self::$stdin); if (empty($line)) @@ -49,7 +48,7 @@ public static function listenForCommands(ModularDiscord $modDiscord): void $args = array_slice($exploded, 1); self::handleCommand($modDiscord, $name, $args); } catch (Exception|Error $ex) { - $discord->getLogger()->error("Error handling console command: {$ex->getMessage()}", [ + $modDiscord->logger->error("Error handling console command: {$ex->getMessage()}", [ 'line' => $ex->getLine(), 'file' => $ex->getFile() ]); @@ -57,9 +56,9 @@ public static function listenForCommands(ModularDiscord $modDiscord): void }); return; } - $discord->getLogger()->error('stream_set_blocking(...) failed: console input handler not initialized.'); + $modDiscord->logger->error('stream_set_blocking(...) failed: console input handler not initialized.'); } catch (Exception $e) { - $discord->getLogger()->error("Failed to initialize console command listener: {$e->getMessage()}", ['type' => get_class($e)]); + $modDiscord->logger->error("Failed to initialize console command listener: {$e->getMessage()}", ['type' => get_class($e)]); } }