discord php adalah perpustakaan PHP yang dikembangkan untuk menyertakan dan menyederhanakan pembuatan dan koneksi antara situs web, aplikasi online, dan Discord.
Hanya diperlukan PHP 5.6+. Mari kita mulai mendownload library discord-php, lalu ekstrak ke your_server_path/
.
Cara cepatnya adalah dengan mengimpor Autoload.php
:
require_once ( " /your/custom/path/to/discord-php/Autoload.php " );
Selain itu, Anda hanya dapat menyertakan kelas yang Anda perlukan:
// Required
require_once ( " ./Discord.php " );
require_once ( " ./Client.php " );
// Optional
include_once ( " ./Discord/Guilds.php " );
include_once ( " ./Discord/Channels.php " );
Sebelum menggunakan lib discord php , Anda harus membuat Aplikasi Discord Anda, lalu membuat "Objek Konfigurasi Baru". sebagai berikut:
$ 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 );
Di mana:
client_id
= @me
- Diperlukan untuk permintaan oAuth2 / Token Exchange ;;client_secret
= Diperlukan untuk permintaan oAuth2 / Token Exchange ;token
= Diperlukan untuk Permintaan API apa pun seperti New DiscordClientUsers("USER_ID");
; Anda juga dapat dengan mudah menambahkan semua info Aplikasi Discord Anda dengan menggunakan file ./Configs.php
, lalu menyertakan sebagai berikut:
$ discord_configs = New Discord Configs ( include ( " ./Configs.php " ));
Buat Klien Discord berdasarkan Konfigurasi Discord Anda, untuk mengirim permintaan API Discord sebagai berikut:
$discord = New DiscordClient($discord_configs);
Anda sekarang siap menggunakan lib discord php sesuai keinginan! Segala sesuatu yang perlu Anda ketahui tentang perpustakaan, seperti Kelas dan "Cara Menggunakan", dijelaskan di sini di Wiki. Saya harap perpustakaan ini akan membantu Anda mengkode Discord di PHP. Setiap saran atau perbaikan selalu diterima.
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
Sebelum memulai, Anda harus mempelajari lebih lanjut tentang objek "Stash": Pertama-tama, simpanan adalah array opsional untuk disiapkan selama pembuatan Konfigurasi Discord baru. Dengan menghormati standar yang diusulkan dalam contoh, Anda dapat dengan mudah menggunakan "Klien Discord" untuk membuat respons API, sebagai berikut:
$ 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 " );