Skip to content
SQKo edited this page Jul 24, 2022 · 6 revisions

The command client is an easy way to build your own bot without all the hassle of processing the users command (by message) and separating parameters.

use Discord\DiscordCommandClient;

Basics

The command client is built into the DiscordPHP package and can be imported and used without any extra requirements. The command client is built off the default client so all of the functions and variables you used on the main client can be used in the command client.

Here is basic usage of the command client which will respond with "pong!" when the command "ping" has been detected.

<?php

use Discord\DiscordCommandClient;

$discord = new DiscordCommandClient([
  'token' => 'your-token',
  //'discordOptions' => [ // other options from Discord() here, example:
    //'intents' => Intents::getDefaultIntents(),
    //'loadAllMembers' => false,
  //],
]);

$discord->registerCommand('ping', function ($message) {
  return 'pong!';
}, [
  'description' => 'pong!',
]);

$discord->run();

Options

The command client takes an array of options in the constructor.

Name Description Default
token The Discord authentication token for the bot. Required.
prefix The prefix that the bot looks for when handling commands. The @mention of the bot user.
name The name of the bot. Used for help menus etc. The username of the bot user.
description The description of the bot. Used for help menus etc. A bot made with DiscordPHP.
defaultHelpCommand Whether the command client should register the default help command. true
discordOptions An array of options that will be passed to the main Discord client. Empty array.

Registering Commands

To register a command you must provide a trigger (the command) and a callable or string. If a callable is provided and a string is returned the string will be posted to the channel as a reply to the initial message. If an array is returned, the command client will choose a random value from the array and will post it to the channel as the reply. If it is neither a string or array, it will not post anything. If a string is provided, it will be posted to the channel as a reply. If an array is provided, a random value will be chosen and posted to the channel as a reply.

// Will respond to `ping` with `pong!`.
$discord->registerCommand('ping', 'pong!');

// Will respond to 'hi' with 'hey', 'hello' or 'wussup'.
$discord->registerCommand('hi', ['hey', 'hello', 'wussup']);

// Will respond to 'cool-command' with 'i am a cool command'.
$discord->registerCommand('cool-command', function ($message, $params) {
  $reply = 'i am a cool command';
  // Do some processing etc.

  return $reply;
});

The registerCommand function also has an options parameter which you can pass through any of the following values. None are required.

Name Description Default
description The description of the command. Used for the help command. No description provided.
usage The usage of the command. Used for the help command. Empty.
aliases An array of aliases that the command will also respond to. Empty array.

Here is an expanded example from the last section using options.

// Will respond to 'hi', 'hey', 'hello', 'wassup' with 'hey', 'hello' or 'wussup'.
$discord->registerCommand('hi', ['hey', 'hello', 'wussup'], ['aliases' => ['hey', 'hello', 'wassup']]);

Sub Commands

Sub-commands are commands that inherit the namespace of the main command. An example is @Bot users kick <username> and @Bot users ban <username>. In this case, the kick and ban commands would be the sub-commands and the users command would be the main command.

Here is an example of these commands.

$users = $discord->registerCommand('users', function () {
  // Form a list of users...
});

$users->registerSubCommand('kick', function () {
  // Take the username and kick the user...
});

$users->registerSubCommand('ban', function () {
  // Take the username and ban the user...
});
Clone this wiki locally