CommandRouter
Class · Sharkord\Commands\CommandRouter
Class CommandRouter
Responsible for loading, registering, and executing chat commands.
Methods
Section titled “Methods”__construct()
Section titled “__construct()”CommandRouter constructor.
Parameters
| Parameter | Type | Description |
|---|---|---|
$sharkord | Sharkord | The main bot instance. |
register()
Section titled “register()”Registers a single command instance to the router.
Parameters
| Parameter | Type | Description |
|---|---|---|
$command | CommandInterface | The command object to register. |
Example
$sharkord->commands->register(new PingCommand());loadFromDirectory()
Section titled “loadFromDirectory()”Automatically loads and registers all command classes from a specific directory.
Parameters
| Parameter | Type | Description |
|---|---|---|
$directory | string | The 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');handle()
Section titled “handle()”Checks if a received message matches a command pattern and executes it.
Parameters
| Parameter | Type | Description |
|---|---|---|
$message | Message | The received message object. |
$matches | array | The original regex matches |
Example
// Typically called automatically by the framework:if (preg_match('/^!(\w+)\s*(.*)/s', $message->content, $matches)) { $sharkord->commands->handle($message, $matches);}getCommands()
Section titled “getCommands()”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";}