2

Tried to get numbers of discord members using discord's API endpoint GET/guilds/{guild.id}/members (see here).

I am using Postman to make a call to it. I set the Authorization header:

Authorization: "Bot ${BOT_TOKEN}"

The response I get is

{ "message": "Missing Access", "code": 50001 }

I also tried with Authorization: DISCORD_CLIENT_ID. The response is { "message": "401: Unauthorized", "code": 0 }.

Am I missing something? Please help me out with this.

enter image description here

Chris
  • 18,724
  • 6
  • 46
  • 80
  • You didn't provide proper authentication. Double-check the docs and make sure you are providing the right token. – code Feb 19 '22 at 05:54
  • Now, I tried login with my email and password than got token and used it with the endpoint https://discord.com/api/v9/guilds/{{guild_id}}/members. Now getting the response of "Missing access" with code "50001". So few questions arising is that. 1. Which exact token I need? Bot, App, or access token which I can get after login. 2. With working token will I be able to get any guild_id's details? (I am trying to get number of member of that guild_id) If you can point me out with discord doc link it will be very helpful. Thank you –  Feb 21 '22 at 06:03
  • I don't know. Maybe you should try out all the tokens you can access, since it already failed many times. Here's the guild docs, although I doubt it'd be much of a help because it only talks about the API and no authentication: https://discord.com/developers/docs/resources/guild – code Feb 21 '22 at 18:15
  • 1
    Are you sure you're interpolating the guild ID correctly? It should be `https://discord.com/api/v9/guilds/YOUR_GUILD_ID/members`. – code Feb 21 '22 at 18:18

1 Answers1

5

First, you need to make sure you are using the correct guild id and not a channel id. You can enable Developer Mode on Discord through User settings -> Advanced, which allows you to obtain the guild id by right-clicking on a guild (server), and then clicking on Copy ID.

Next, go to your application through Developer Portal, select your application and navigate to the Bot tab on the navigation bar to the left. From there, obtain your bot's authentication token that you need to pass in the headers of your request. On the same tab - since you need to get the list of Guild Members, and since that "endpoint is restricted according to whether the GUILD_MEMBERS Privileged Intent is enabled for your application" - scroll down to the Privileged Gateway Intents section and enable SERVER MEMBERS INTENT.

Below is a working example using the Node.js standard modules. Please note that, as described here, the default limit of maximum number of members to return is 1. Thus, you can adjust the limit in the query parameters, as below, to receive more results per request. The maximum number of results you can get per request is 1000. Therefore, if a guild contains more members than that, you can keep track of the highest user id present in each previous request, and pass it to the after parameter of the next request, as described in the documentation. In this way, you can obtain every member in a guild. Also, make sure you set the properties in options in the proper way, as shown below; otherwise, you might come accross getaddrinfo ENOTFOUND error, as shown here.

Update

If you haven't already, you should add your bot to the server you wish, by generating an invite link for your bot through URL Generator under OAuth2 in your application settings (select bot from scopes). Now, you can access that URL from your browser and add the bot to any of your servers. If you need to, you can share the same invite link with others, so that they can add your bot to their servers.

Example in Node.js

const https = require('https')
const url = require('url');

GUILD_ID = "YOUR_GUILD_ID"
BOT_TOKEN = 'YOUR_BOT_TOKEN'
LIMIT = 10

const requestUrl = url.parse(url.format({
    protocol: 'https',
    hostname: 'discord.com',
    pathname: `/api/guilds/${GUILD_ID}/members`,
    query: {
        'limit': LIMIT
    }
}));

const options = {
    hostname: requestUrl.hostname,
    path: requestUrl.path,
    method: 'GET',
    headers: {
        'Authorization': `Bot ${BOT_TOKEN}`,
    }
}
const req = https.request(options, res => {
    res.on('data', d => {
        process.stdout.write(d)
    })
})

req.on('error', error => {
    console.error(error)
})

req.end()

Example in Python

import requests
import json

GUILD_ID = "YOUR_GUILD_ID"
BOT_TOKEN = 'YOUR_BOT_TOKEN'
LIMIT = 10
headers = {'Authorization' : 'Bot {}'.format(BOT_TOKEN)}
base_URL = 'https://discord.com/api/guilds/{}/members'.format(GUILD_ID)
params = {"limit": LIMIT}
r = requests.get(base_URL, headers=headers, params=params)
print(r.status_code)
print(r.text,'\n')
#print(r.raise_for_status())
for obj in r.json():
    print(obj,'\n')
Chris
  • 18,724
  • 6
  • 46
  • 80
  • I followed your instructions but when I try to run I am getting this error: { "message": "Missing Access", "code": 50001 } –  Feb 28 '22 at 09:32
  • This doesn't sound right. I have also added a Python version for you to test. Both the examples above should work as expected. The error you mention is thrown only if you haven't provided the correct `Guild ID`, or the bot has not been added to a server, or you haven't toggle ON `Guild_Members` (Server Members Intent) for your bot, as described above. – Chris Feb 28 '22 at 10:00
  • Oh, so do I need to add my bot in the server??? So that means I cannot fetch members of any other server other than mine? right? –  Feb 28 '22 at 10:57
  • 1
    You could fetch members of other servers, as long as the `bot` is added to those servers. I have updated the answer with how you could add the `bot` to your server. The same URL could be shared with others to add the bot to their servers. – Chris Feb 28 '22 at 11:07
  • 1
    _"Bots cannot accept invites or join servers/guilds any other way than being manually invited. "_ (see [here](https://stackoverflow.com/a/55554780)). Otherwise, this would allow bots to "randomly" join servers. – Chris Feb 28 '22 at 11:31