discord php is a PHP library developed to include and simplify the creation and connection between websites, online applications and Discord.
Just PHP 5.6+ is required. Let's start downloading the discord-php library, then extract it to your_server_path/
.
A quickway is to import the Autoload.php
:
require_once("/your/custom/path/to/discord-php/Autoload.php");
Also, you can include only your needed classes:
// Required
require_once("./Discord.php");
require_once("./Client.php");
// Optional
include_once("./Discord/Guilds.php");
include_once("./Discord/Channels.php");
Before using the discord php lib, You must create your Discord App, then create a "New Configuration Object". as following:
$configs = array();
$configs["token"] = "YOUR_BOT_TOKEN";
$configs["authType"] = "Bot"; // Optional (Default: "Bot")
$configs["client_id"] = "YOUR_CLIENT_ID"; // Optional (Default: null)
$configs["client_secret"] = "YOUR_CLIENT_SECRET"; // Optional (Default: null)
$configs["public_key"] = "YOUR_PUBLIC_KEY"; // Optional (Default: null)
$configs["stash"] = array( // Optional
"Guild Name" => array(
"guild_id" => "YOUR_SERVER_ID",
"channels" => array(
"Channel Name" => "YOUR_CHANNEL_ID"
),
"roles" => array(
"Role Name" => "YOUR_ROLE_ID"
),
"members" => array(
"Member Name" => "YOUR_MEMBER_USER_ID"
)
)
);
$discord_configs = New DiscordConfigs($configs);
Where:
client_id
= @me
- Required for oAuth2 / Token Exchange requests;;client_secret
= Required for oAuth2 / Token Exchange requests;token
= Required for any API Request such as New DiscordClientUsers("USER_ID");
;Also you can easily add all of your Discord Application infos by using the file ./Configs.php
, then including as following:
$discord_configs = New DiscordConfigs(include("./Configs.php"));
Create a Discord Client based on your Discord Configs, to send Discord API requests as following:
$discord = New DiscordClient($discord_configs);
You are now ready to use the discord php lib as you wish! Everything you need to know about the library, such as Classes and "How To Use", is described here in the Wiki. I hope this library will help you coding for Discord in PHP. Any suggestion or improvement is always welcome.
require_once("./Autoload.php");
$configs = include("./Configs.php");
$discord = New DiscordClient(New DiscordConfigs($configs));
// Guilds
$guild = New DiscordClientGuilds($discord, "GUILD_ID");
$members = $guild->members();
$specific_member = $guild->members("USER_ID"); // Will return a DiscordClientMembers Object
$channels = $guild->channels();
$specific_channel = $guild->channels("CHANNEL_ID"); // Will return a DiscordClientChannels Object
// Channels
$channel = New DiscordClientChannels($discord, "CHANNEL_ID");
$messages = $channel->messages();
$specific_message = $channel->message("MESSAGE_ID"); // Will return a DiscordClientMessages Object
$discord->stash
Before starting, you've to learn more about a "Stash" object: First of all, a stash is an optional array to setup during the creation of a new Discord Configuration. Respecting the standard proposed in the example, you can easily use the "Discord Client" to make API responses, as following:
$configs = array(
"token" => "YOUR_BOT_TOKEN",
"stash" => array(
"CUSTOM_GUILD_NAME" => array(
"guild_id" => "YOUR_GUILD_ID"
"channels" => array(
"CUSTOM_CHANNEL_NAME" => "YOUR_CHANNEL_ID"
)
),
"CUSTOM_GUILD_NAME_2" => array(
"channels" => array(
"CUSTOM_CHANNEL_NAME_2" => "YOUR_CHANNEL_ID_2"
)
)
)
);
$discord = New DiscordClient(New DiscordConfigs($configs));
// Get a DiscordClientGuilds directly from the client using your Stash
// Please, make sure you've correctly imported ./Discord/Guilds.php class before.
// Also, make sure you're using the right $config standard and "GUILD_NAME" in order to get automatically a GUILD_ID
$guild = $discord->Guilds("CUSTOM_GUILD_NAME");
// You can also send a custom Guild ID
$guild = $discord->Guilds("GUILD_ID");
// You can do the same for the channels
// Make sure you've correctly imported ./Discord/Channels.php class before
$channel = $discord->Channels("CUSTOM_CHANNEL_NAME_2");
// And then, to get a channel using a custom Channel ID
$channel = $discord->Channels("CHANNEL_ID");