{"_id":"57e1a33cb4baa42d00e46a34","__v":0,"category":{"_id":"57590727ace5c30e00cb23d8","__v":0,"version":"57590727ace5c30e00cb23d7","project":"571193ce8103870e00f48b67","sync":{"url":"","isSync":false},"reference":false,"createdAt":"2016-04-16T01:22:22.642Z","from_sync":false,"order":0,"slug":"documentation","title":"Documentation"},"project":"571193ce8103870e00f48b67","user":"571193c63590a50e00bba99d","version":{"_id":"57590727ace5c30e00cb23d7","__v":2,"project":"571193ce8103870e00f48b67","createdAt":"2016-06-09T06:05:27.924Z","releaseDate":"2016-06-09T06:05:27.924Z","categories":["57590727ace5c30e00cb23d8","57590cd318760817001e8a83"],"is_deprecated":false,"is_hidden":false,"is_beta":false,"is_stable":true,"codename":"","version_clean":"4.0.0","version":"4.0.0"},"updates":[],"next":{"pages":[],"description":""},"createdAt":"2016-09-20T20:59:40.625Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","auth":"required","params":[],"url":""},"isReference":false,"order":999,"body":"The command client is an easy way to build your own bot without all the hassle of processing the users command and separating parameters.\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Basics\"\n}\n[/block]\nThe 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.\n\nHere is basic usage of the command client which will respond with `pong!` when the command `ping` has been detected.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"<?php\\n\\nuse Discord\\\\DiscordCommandClient;\\n\\n$discord = new DiscordCommandClient([\\n\\t'token' => 'your-token',\\n]);\\n\\n$discord->registerCommand('ping', function ($message) {\\n\\treturn 'pong!';\\n}, [\\n\\t'description' => 'pong!',\\n]);\\n\\n$discord->run();\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Options\"\n}\n[/block]\nThe command client takes an array of options in the constructor.\n[block:parameters]\n{\n  \"data\": {\n    \"h-0\": \"Name\",\n    \"h-1\": \"Description\",\n    \"h-2\": \"Default\",\n    \"0-0\": \"`token`\",\n    \"0-1\": \"The Discord authentication token for the bot.\",\n    \"0-2\": \"Required.\",\n    \"1-0\": \"`prefix`\",\n    \"1-1\": \"The prefix that the bot looks for when handling commands.\",\n    \"1-2\": \"The `:::at:::mention` of the bot user.\",\n    \"2-0\": \"`name`\",\n    \"2-1\": \"The name of the bot. Used for help menus etc.\",\n    \"2-2\": \"The username of the bot user.\",\n    \"3-0\": \"`description`\",\n    \"3-1\": \"The description of the bot. Used for help menus etc.\",\n    \"3-2\": \"`A bot made with DiscordPHP.`\",\n    \"4-0\": \"`defaultHelpCommand`\",\n    \"4-1\": \"Whether the command client should register the default help command.\",\n    \"4-2\": \"`true`\",\n    \"5-0\": \"`discordOptions`\",\n    \"5-1\": \"An array of options that will be passed to the main Discord client.\",\n    \"5-2\": \"Empty array.\"\n  },\n  \"cols\": 3,\n  \"rows\": 6\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Registering Commands\"\n}\n[/block]\nTo 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.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"<?php\\n\\n// Will respond to `ping` with `pong!`.\\n$discord->registerCommand('ping', 'pong!');\\n\\n// Will respond to 'hi' with 'hey', 'hello' or 'wussup'.\\n$discord->registerCommand('hi', ['hey', 'hello', 'wussup']);\\n\\n// Will respond to 'cool-command' with 'i am a cool command'.\\n$discord->registerCommand('cool-command', function ($message, $params) {\\n\\t$reply = 'i am a cool command';\\n\\t// Do some processing etc.\\n\\n\\treturn $reply;\\n});\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\nThe `registerCommand ` function also has an `options` parameter which you can pass through any of the following values. None are required.\n[block:parameters]\n{\n  \"data\": {\n    \"h-0\": \"Name\",\n    \"h-1\": \"Description\",\n    \"h-2\": \"Default\",\n    \"0-0\": \"`description`\",\n    \"0-1\": \"The description of the command. Used for the help command.\",\n    \"0-2\": \"`No description provided.`\",\n    \"1-0\": \"`usage`\",\n    \"1-1\": \"The usage of the command. Used for the help command.\",\n    \"1-2\": \"Empty.\",\n    \"2-0\": \"`aliases`\",\n    \"2-1\": \"An array of aliases that the command will also respond to.\",\n    \"2-2\": \"Empty array.\"\n  },\n  \"cols\": 3,\n  \"rows\": 3\n}\n[/block]\nHere is an expanded example from the last section using options.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"<?php\\n\\n// Will respond to 'hi', 'hey', 'hello', 'wassup' with 'hey', 'hello' or 'wussup'.\\n$discord->registerCommand('hi', ['hey', 'hello', 'wussup'], ['aliases' => ['hey', 'hello', 'wassup']]);\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Sub-Commands\"\n}\n[/block]\nSub-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.\n\nHere is an example of these commands.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"<?php\\n\\n$users = $discord->registerCommand('users', function () {\\n\\t// Form a list of users...\\n});\\n\\n$users->registerSubCommand('kick', function () {\\n\\t// Take the username and kick the user...\\n});\\n\\n$users->registerSubCommand('ban', function () {\\n\\t// Take the username and ban the user...\\n});\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]","excerpt":"","slug":"command-client","type":"basic","title":"Command Client"}
The command client is an easy way to build your own bot without all the hassle of processing the users command and separating parameters. [block:api-header] { "type": "basic", "title": "Basics" } [/block] 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. [block:code] { "codes": [ { "code": "<?php\n\nuse Discord\\DiscordCommandClient;\n\n$discord = new DiscordCommandClient([\n\t'token' => 'your-token',\n]);\n\n$discord->registerCommand('ping', function ($message) {\n\treturn 'pong!';\n}, [\n\t'description' => 'pong!',\n]);\n\n$discord->run();", "language": "php" } ] } [/block] [block:api-header] { "type": "basic", "title": "Options" } [/block] The command client takes an array of options in the constructor. [block:parameters] { "data": { "h-0": "Name", "h-1": "Description", "h-2": "Default", "0-0": "`token`", "0-1": "The Discord authentication token for the bot.", "0-2": "Required.", "1-0": "`prefix`", "1-1": "The prefix that the bot looks for when handling commands.", "1-2": "The `@mention` of the bot user.", "2-0": "`name`", "2-1": "The name of the bot. Used for help menus etc.", "2-2": "The username of the bot user.", "3-0": "`description`", "3-1": "The description of the bot. Used for help menus etc.", "3-2": "`A bot made with DiscordPHP.`", "4-0": "`defaultHelpCommand`", "4-1": "Whether the command client should register the default help command.", "4-2": "`true`", "5-0": "`discordOptions`", "5-1": "An array of options that will be passed to the main Discord client.", "5-2": "Empty array." }, "cols": 3, "rows": 6 } [/block] [block:api-header] { "type": "basic", "title": "Registering Commands" } [/block] 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. [block:code] { "codes": [ { "code": "<?php\n\n// Will respond to `ping` with `pong!`.\n$discord->registerCommand('ping', 'pong!');\n\n// Will respond to 'hi' with 'hey', 'hello' or 'wussup'.\n$discord->registerCommand('hi', ['hey', 'hello', 'wussup']);\n\n// Will respond to 'cool-command' with 'i am a cool command'.\n$discord->registerCommand('cool-command', function ($message, $params) {\n\t$reply = 'i am a cool command';\n\t// Do some processing etc.\n\n\treturn $reply;\n});", "language": "php" } ] } [/block] The `registerCommand ` function also has an `options` parameter which you can pass through any of the following values. None are required. [block:parameters] { "data": { "h-0": "Name", "h-1": "Description", "h-2": "Default", "0-0": "`description`", "0-1": "The description of the command. Used for the help command.", "0-2": "`No description provided.`", "1-0": "`usage`", "1-1": "The usage of the command. Used for the help command.", "1-2": "Empty.", "2-0": "`aliases`", "2-1": "An array of aliases that the command will also respond to.", "2-2": "Empty array." }, "cols": 3, "rows": 3 } [/block] Here is an expanded example from the last section using options. [block:code] { "codes": [ { "code": "<?php\n\n// Will respond to 'hi', 'hey', 'hello', 'wassup' with 'hey', 'hello' or 'wussup'.\n$discord->registerCommand('hi', ['hey', 'hello', 'wussup'], ['aliases' => ['hey', 'hello', 'wassup']]);", "language": "php" } ] } [/block] [block:api-header] { "type": "basic", "title": "Sub-Commands" } [/block] 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. [block:code] { "codes": [ { "code": "<?php\n\n$users = $discord->registerCommand('users', function () {\n\t// Form a list of users...\n});\n\n$users->registerSubCommand('kick', function () {\n\t// Take the username and kick the user...\n});\n\n$users->registerSubCommand('ban', function () {\n\t// Take the username and ban the user...\n});", "language": "php" } ] } [/block]