PHP code example of thediscordlist / integration-discordphp
1. Go to this page and download the library: Download thediscordlist/integration-discordphp library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
thediscordlist / integration-discordphp example snippets
use Discord\Discord;
use Discord\WebSockets\Intents;
use TheDiscordList\DiscordPHP\Autoposter;
$discord = new Discord([
'token' => getenv('DISCORD_TOKEN'),
'intents' => Intents::getDefaultIntents(),
]);
Autoposter::attach($discord, getenv('TDL_TOKEN') ?: null);
$discord->run();
Autoposter::attach($discord, getenv('TDL_TOKEN') ?: null, [
// Runs on the bot's event loop: anything that does I/O returns a promise of the int.
'serverCount' => fn () => $redis->get('bot:guilds')->then(fn ($guilds) => (int) $guilds),
]);
use TheDiscordList\DiscordPHP\Options;
Autoposter::attach($discord, $token, new Options(
botId: '161660517914509312', // default: the client's application id
baseUrl: 'https://thediscordlist.com/api',
stats: true,
statsInterval: 300.0,
statsDebounce: 20.0,
statsMinGap: 60.0,
heartbeat: true,
heartbeatInterval: 30.0, // starting cadence, 15–60; retuned from the API response
commands: true,
postEmptyCommands: false,
cluster: ['id' => 0, 'count' => 1],
serverCount: fn () => 1234,
));
use TheDiscordList\DiscordPHP\PublishException;
use TheDiscordList\DiscordPHP\PostResult;
$poster = Autoposter::attach($discord, getenv('TDL_TOKEN') ?: null);
$poster->on('posted', function (string $type, int $status) {});
$poster->on('error', function (PublishException $error) {}); // ->type, ->status, ->retryable
$poster->on('disabled', function (string $type, ?int $status, string $reason) {});
$poster->postStats()->then(fn (PostResult $result) => $result->ok); // ->status, ->skipped
$poster->postCommands(); // after re-registering commands in-process
$poster->postHeartbeat();
$poster->stop();
use TheDiscordList\DiscordPHP\Webhooks\Verifier;
use TheDiscordList\DiscordPHP\Webhooks\VerificationException;
try {
$event = Verifier::verify($rawBody, $signatureHeader, $secret); // tolerance: 300s
} catch (VerificationException $error) {
// $error->reason is an ErrorCode: MissingSignature, MalformedSignature,
// StaleTimestamp, InvalidSignature or MalformedBody. Answer 401 for a
// missing or invalid signature, 400 otherwise.
}
use Illuminate\Http\Request;
use TheDiscordList\DiscordPHP\Webhooks\Verifier;
use TheDiscordList\DiscordPHP\Webhooks\VerificationException;
Route::post('/webhooks/tdl', function (Request $request) {
try {
$event = Verifier::verify($request->getContent(), $request->header('X-TDL-Signature'), config('services.tdl.webhook_secret'));
} catch (VerificationException) {
return response()->noContent(400);
}
if (! Cache::add('tdl:'.$request->header('X-TDL-Delivery'), true, now()->addDay())) {
return response()->noContent(); // a retry we already handled
}
ProcessTdlEvent::dispatch($event);
return response()->noContent();
});
use GuzzleHttp\Psr7\HttpFactory;
use TheDiscordList\DiscordPHP\Webhooks\WebhookHandler;
$handler = new WebhookHandler(
(string) getenv('TDL_WEBHOOK_SECRET'),
function (array $event, array $context): void {
// $context['delivery'] is X-TDL-Delivery, $context['attempt'] is 1-based.
},
new HttpFactory(),
);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.