discord php
1.0.0
discord php是一個 PHP 函式庫,旨在包含並簡化網站、線上應用程式和 Discord 之間的建立和連結。
只需要 PHP 5.6+。讓我們開始下載 Discord-php 函式庫,然後將其解壓縮到your_server_path/
。
一個快速方法是導入Autoload.php
:
require_once ( " /your/custom/path/to/discord-php/Autoload.php " );
此外,您可以只包含您需要的課程:
// Required
require_once ( " ./Discord.php " );
require_once ( " ./Client.php " );
// Optional
include_once ( " ./Discord/Guilds.php " );
include_once ( " ./Discord/Channels.php " );
在使用discord php lib之前,您必須建立您的 Discord 應用程序,然後建立一個「新配置物件」。如下:
$ 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 Discord Configs ( $ configs );
在哪裡:
client_id
= @me
- oAuth2 /令牌交換請求所需;;client_secret
= oAuth2 /令牌交換請求所需;token
= 任何API 請求(例如New DiscordClientUsers("USER_ID");
;您還可以使用文件./Configs.php
輕鬆添加所有 Discord 應用程式信息,然後包括以下內容:
$ discord_configs = New Discord Configs ( include ( " ./Configs.php " ));
根據您的 Discord 配置建立一個 Discord 用戶端,以傳送 Discord API 請求,如下所示:
$discord = New DiscordClient($discord_configs);
現在您可以按照您的意願使用discord php庫了!您需要了解的有關該庫的所有信息,例如類和“如何使用”,都在 Wiki 中進行了描述。我希望這個函式庫能幫助您在 PHP 中進行 Discord 編碼。任何建議或改進總是受歡迎的。
require_once ( " ./Autoload.php " );
$ configs = include ( " ./Configs.php " );
$ discord = New Discord Client ( New Discord Configs ( $ configs ));
// Guilds
$ guild = New Discord Client Guilds ( $ 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 Discord Client Channels ( $ discord , " CHANNEL_ID " );
$ messages = $ channel -> messages ();
$ specific_message = $ channel -> message ( " MESSAGE_ID " ); // Will return a DiscordClientMessages Object
$discord->stash
在開始之前,您必須了解有關「Stash」物件的更多資訊:首先,stash 是在建立新的 Discord 配置期間要設定的可選數組。遵循範例中提出的標準,您可以輕鬆使用「Discord Client」進行API回應,如下所示:
$ 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 Discord Client ( New Discord Configs ( $ 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 " );