snowrunescape / discord-bot-php
Discord Bot PHP
v3.2.0
2025-05-01 01:00 UTC
Requires
- textalk/websocket: ^1.6.3
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.66
- phpunit/phpunit: ^9.5
This package is auto-updated.
Last update: 2025-05-01 01:01:24 UTC
README
DiscordBot-PHP is a powerful PHP module that allows you to easily interact with the Discord API.
Installation
Preferred way to install is with Composer.
composer require snowrunescape/discord-bot-php
PHP 7.4 or newer is required.
Example usage
You can see an example of how to use it by clicking here.
require_once "vendor/autoload.php"; use DiscordPHP\Discord; $discord = new Discord("YOU_DISCORD_BOT_TOKEN"); $discord->run();
Register commands and events before triggering the $discord->run();
To register a command use the code
$discord->event->registerCommand(new Ping($discord));
To register events use the code
$discord->event->registerEventHandler(new MESSAGE_CREATE($discord));
Example Command
class Ping extends DiscordCommand { public function getCommand() { return "!ping"; } public function onInit() { Logger::Info("Starting command..."); } public function run(array $event, array $args) { $this->discord->discordAPI->createMessage("Pong!", $event["channel_id"]); } }
Events can be created inside commands, to keep the code organized
public function MESSAGE_CREATE($event) { Logger::Info("This event handler has been called!"); }
Example eventHandler
class MESSAGE_CREATE extends DiscordEventHandler { public function onInit() { Logger::Info("Starting eventHandler..."); } public function run(array $event) { Logger::Info("This event handler has been called!"); } }
Credits
Help
If you don't understand something in the documentation, you are experiencing problems, or you just need a gentle nudge in the right direction, please don't hesitate to join our official DiscordBot-PHP Server.