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')