Skip to content

Instantly share code, notes, and snippets.

@TriForceX
Last active June 14, 2024 20:11
Show Gist options
  • Save TriForceX/a8332709718851ea1bf0cad15f3a2600 to your computer and use it in GitHub Desktop.
Save TriForceX/a8332709718851ea1bf0cad15f3a2600 to your computer and use it in GitHub Desktop.
Discord badge workaround
<?php
// Discord badge workaround
if (isset($_GET['id']) && ($_GET['id'] == 'ALLOWED_ID_1_HERE' || $_GET['id'] == 'ALLOWED_ID_2_HERE'))
{
// Fetch data
$members_json = json_decode(file_get_contents('https://discord.com/api/guilds/'.$_GET['id'].'/widget.json'), true);
// Check count
if ($members_json) {
$members = $members_json['presence_count'];
} else {
$members = 0;
}
// The URL of the image
$imageUrl = 'https://img.shields.io/badge/Discord-'.$members.'%20online-5865F2.svg?logo=discord&style=flat&logoColor=white';
// Initialize a cURL session
$ch = curl_init($imageUrl);
// Set cURL options
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Return the transfer as a string
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // Follow redirects
// Execute the cURL session
$imageData = curl_exec($ch);
// Get the content type of the image
$contentType = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
// Close the cURL session
curl_close($ch);
// Set the appropriate content type header
header('Content-Type: '.$contentType);
// Output the image data
echo $imageData;
}
else
{
// Throw error on invalid ID
die('Not allowed.');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment