-
Notifications
You must be signed in to change notification settings - Fork 6
Home
phpcord is neither the first discord api wrapper in php nor official. It's a project I wanted to do a longer time ago, and finished it's first version in the beginnings of 2021. The current major version is v3, updates are not planned yet, but there are things to adapt to in the future.
Before using it, please make sure to have these two options enabled, as it will provide a more stable workflow.
In the following documents, you will often find pseudocode to prevent misunderstandings.
This works as following:
Non-Static functions: ClassName->functionName()
Static functions: ClassName::staticFunctionName()
Constants: ClassName::CONSTANT_NAME
The parameters of functions will be expressed either as their type (e.g string
, int
, GuildMember
(yes, an object)) or as an expected value (e.g 20
, Guild->memberCount
)
phpcord comes with a basic example of it's code to be started successfully. It's the same code as this one:
<?php
use phpcord\Discord;
use phpcord\event\message\MessageSendEvent;
use phpcord\message\sendable\MessageBuilder;
require 'phpcord/src/phpcord/Discord.php';
$discord = new Discord();
$discord->listen(MessageSendEvent::class, function (MessageSendEvent $event): void {
if (!$event->getMessage()->getAuthor()->isBot()) {
$event->getMessage()->reply(MessageBuilder::build('This is a dummy message.'));
}
});
$discord->login('your bot token');
(you obviously need to replace your bot token
with the real token of your bot)
NOTICE Any code beyond Disord->login()
will NOT be executed.
You can use any file in any directory, make sure to require Discord.php
with the correct path.
The event system used for phpcord is pretty similar to PocketMine-MP, which makes it easier to understand for most of the users.
Discord->listen()
provides an easy support for this.
You have two options here:
-
Using an object for listening
Discord->listen(object)
(e.g$discord->listen(new EventListener());
)This will register every function in this class that follows the rules:
It must be a public and non-static function that contains 1 parameter, the Eventclass (the example uses
MessageSendEvent
) -
Using a Closure for listening
Discord->listen(event_class, Closure)
(e.g$discord->listen(MessageSendEvent::class, function (MessageSendEvent $event): void { });
This will register only one listener for the
event_class
passed (e.gMessageSendEvent::class
). Every time this event is called, the Closure will be executed. Take a look at the default entry file for an example.