I have a Discord servern with 1361 members and on my website I want to display a total number of joined users.
I have figured out how to get all online Members on the server using:
<?php
$jsonIn = file_get_contents('https://discordapp.com/api/guilds/356230556738125824/widget.json');
$JSON = json_decode($jsonIn, true);
$membersCount = count($JSON['members']);
echo "Number of members: " . $membersCount;
?>
What should I do differently to get a total number of ALL users that have joined the server, and not just display the online members?
Now, I realize I am reviving a pretty old thread here, but I figure some might still use an answer. As jrenk pointed out, you should instead access
https://discordapp.com/api/guilds/356230556738125824/members
.Your
404: Unauthorized
comes from the fact that you are -you guessed it- not authorized. If you have created a bot, it is fairly easy: just add a request headerAuthorization: Bot YOUR_BOT_TOKEN_HERE
. If you use a normal Discord account, the whole problem is a bit more tricky:You will first have to send a
POST
request tohttps://discordapp.com/api/auth/login
and set the body to{"email": "EMAIL_HERE", "password": "PASSWORD_HERE"}
. You will get a response with the parametertoken
. Save this token, you will need it later. BUT:With this token, you can now send a
POST
request to the same address:https://discordapp.com/api/auth/login
, but now add the headerAuthorization: YOUR_BOT_TOKEN_HERE
. Note the missing "Bot" at the beginning.Also, what you mustn't forget:
You need a bot on your discord server to get all members. Use the Discord js library for example.
First create a discord bot and get a token, see the following url:
https://github.com/reactiflux/discord-irc/wiki/Creating-a-discord-bot-&-getting-a-token
As @2Kreeper noted, do not reveal your token publicly.
Then use the following code, replacing "enter-bot-token-here" and "enter-guild-id-here" with your own information:
You have to count the number of online member here is the working code