Jump to content
 Share

Roy

[PHP] Code To Get The Member Count Via Discord API

Recommended Posts

Hey everyone,

 

I just wanted to post some code I used to make our Discord Tracker in PHP. Unfortunately, I was having issues trying to get the Discord member count of the server. The member_count field located in the API here would always return NULL regardless of the bot having proper privileges. I even setup a test bot along with a test Discord server and had issues as well. After doing research, I found others were having this issue as well. Therefore, I decided to make my own code to get the member count. The code basically loops through all members using this request and object. It was a little bit more complicated since there was a limit of 1000 members and you'd have to specify a GET parameter ("after") to continue the query from where we left off on the last request.

 

Anyways, here's the PHP code. Obviously, you need to change $guildID and $botToken to suit your needs.

 

// Please configure.
$guildID = '';
$botToken = '';

// Variables.
$going = true;
$after = 0;
$count = 0;

// Da loop.
while ($going)
{
	// Perform a GET request to the Discord API using the $guildID.
	$ch = curl_init('https://discordapp.com/api/guilds/' . $guildID . '/members?limit=1000&after=' . $after);
	
	// Set authorization token.
	curl_setopt($ch, CURLOPT_HTTPHEADER, Array('Authorization: Bot ' . $botToken));
	
	// We don't wanna return the headers ;)
	curl_setopt($ch, CURLOPT_HEADER, 0);
	
	// Yes..
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
	
	// Give $result the output.
	$result = curl_exec($ch);
	
	// Close the cURL handle.
	curl_close($ch);
	
	// Decode the results using JSON (since that's what it returns).
	$users = json_decode($result);
	
	// Check if it's valid.
	if ($users && is_array($users))
	{
		// Increase the count per request.
		$count += count($users);
		
		// If the amount is under 1000, we're done here.
		if (count($users) < 1000)
		{
			$going = false;
			
			// Not really needed since we set $going to false anyways, but just figured we'd put it there just in-case?
			break;
		}
		
		// Leave off with the last user ID from the results so it keeps going.
		$after = $users[999]->user->id;
	}
	else
	{
		// BREAKKKKKKKKKKKK!
		break;
	}
}

 

I know the code may be a bit sloppy, but it works well for my needs. Obviously, the higher the member count is on the Discord server, the longer the loop will last. So it may take a few seconds to execute it. If you start getting over 10K+ members in your Discord server, I'd ensure that you have a long timeout set for this script.

 

I hope this helps anyone having the same issue as I was :) 

 

Thank you!

Share this post


Link to post
Share on other sites


Hidden

Thanks for sharing, @Roy!

 

Just a note: If you're trying to query a larger server, then you might get rate limited. The status code for rate limiting is 429 TOO MANY REQUESTS, so keep an eye out for that. Here's a link to Discord's documentation on their rate limiting.

 

You'd often choose to use a framework to avoid manually handling rate limits, but it'd be overkill here in my opinion.


Wanna know what I am up to? Take a look at my personal Trello board or my cards on the Development Trello board!

Share this post


Link to post

Hidden

Simple method, but interesting bug -- I myself run my discord bot via lua (discordia) and the api for returning member_count returns correctly -- no need for iteration. I would look into the bug more and maybe attempt to look at how other discord API interfaces do it internally and reproduce it via PHP.

Share this post


Link to post



×
×
  • Create New...