DiscordBot-PHP is a powerful PHP module that allows you to easily interact with the Discord API.
Preferred way to install is with Composer.
composer require snowrunescape/discord-bot-php
PHP 7.4 or newer is required.
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));
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!");
}
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!");
}
}
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.