Skip to content

Commit

Permalink
Merge pull request #1 from ItsTauTvyDas/dev
Browse files Browse the repository at this point in the history
Fix console listener and commands not working
  • Loading branch information
ItsTauTvyDas authored Aug 20, 2023
2 parents c539d69 + 04f116a commit dea0a8d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 24 deletions.
50 changes: 27 additions & 23 deletions src/ModularDiscord/IntractableConsole.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion src/ModularDiscord/Registry.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

/** @noinspection PhpParamsInspection */

namespace ModularDiscord;

use Closure;
Expand Down Expand Up @@ -103,7 +105,7 @@ public function registerCommand(AbstractCommand $command, ?string $name = null,
$discordCommand = new Command($discord, $builder->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;
}
Expand Down

0 comments on commit dea0a8d

Please sign in to comment.