Skip to content

CommandRouter

Class  ·  Sharkord\Commands\CommandRouter

Class CommandRouter

Responsible for loading, registering, and executing chat commands.

CommandRouter constructor.

Parameters

ParameterTypeDescription
$sharkordSharkordThe main bot instance.

Registers a single command instance to the router.

Parameters

ParameterTypeDescription
$commandCommandInterfaceThe command object to register.

Example

$sharkord->commands->register(new PingCommand());

Automatically loads and registers all command classes from a specific directory.

Parameters

ParameterTypeDescription
$directorystringThe absolute path to the directory containing command classes.
$namespace (optional)string(Optional) The namespace used in the command files. Default is empty (global).

Example

$sharkord->commands->loadFromDirectory(__DIR__ . '/Commands', 'App\\Commands');

Checks if a received message matches a command pattern and executes it.

Parameters

ParameterTypeDescription
$messageMessageThe received message object.
$matchesarrayThe original regex matches

Example

// Typically called automatically by the framework:
if (preg_match('/^!(\w+)\s*(.*)/s', $message->content, $matches)) {
$sharkord->commands->handle($message, $matches);
}

Retrieves all registered commands. Useful for generating “Help” menus.

Returns array<string,\CommandInterface>

Example

foreach ($sharkord->commands->getCommands() as $name => $command) {
echo "!{$name} — {$command->getDescription()}\n";
}