Get total number of members in Discord using PHP

2019-08-23 17:38发布

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?

标签: php discord
4条回答
你好瞎i
2楼-- · 2019-08-23 18:36

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 header Authorization: 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 to https://discordapp.com/api/auth/login and set the body to {"email": "EMAIL_HERE", "password": "PASSWORD_HERE"}. You will get a response with the parameter token. Save this token, you will need it later. BUT:

NEVER, UNDER ANY CIRCUMSTANCES show anyone this token, as it is equivalent to your login credentials!

With this token, you can now send a POST request to the same address: https://discordapp.com/api/auth/login, but now add the header Authorization: YOUR_BOT_TOKEN_HERE. Note the missing "Bot" at the beginning.

Also, what you mustn't forget:

If you don't add the parameter ?limit=MAX_USERS, you will only get the first guild member. Take a look here to see details.

查看更多
冷血范
3楼-- · 2019-08-23 18:14

You need a bot on your discord server to get all members. Use the Discord js library for example.

查看更多
Luminary・发光体
4楼-- · 2019-08-23 18:16

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:

<?php

$json_options = [
  "http" => [
    "method" => "GET",
    "header" => "Authorization: Bot enter-bot-token-here"
  ]
];

$json_context = stream_context_create($json_options);

$json_get     = file_get_contents('https://discordapp.com/api/guilds/enter-guild-id-here/members?limit=1000', false, $json_context);

$json_decode  = json_decode($json_get, true);

echo '<h2>Member Count</h2>';
echo count($json_decode);

echo '<h2>JSON Output</h2>';
echo '<pre>';
print_r($json_decode);
echo '</pre>';

?>
查看更多
我欲成王,谁敢阻挡
5楼-- · 2019-08-23 18:25

You have to count the number of online member here is the working code

<?php
$members = json_decode(file_get_contents('https://discordapp.com/api/guilds/356230556738125824/widget.json'), true)['members'];
$membersCount = 1;
foreach ($members as $member) {
    if ($member['status'] == 'online') {
        $membersCount++;
    }
}
echo "Number of members: " . $membersCount;
?>
查看更多
登录 后发表回答