Skip to content
Advertisement

Discord: Get User by Id

I’m trying to create a web application to manage the users of my Discord server. In my database, I have stored only the users’ ids.

I tried to use the discord.js API, but from what I’ve understood it requires a discord bot to do that. That’s not what I want. I would like to retrieve the user’s information from my frontend, even by calling a backend function, but without having a discord bot which is always online. In other words, I need something simpler.

I would like to request users’ information by using only the id. Which is the best way to do that in JavaScript?

Advertisement

Answer

You can use the Discord API.

First, create a Discord application here. Once you’ve done that, click ‘Bot’ on the sidebar and create a bot for that application. There, you’ll see a section called ‘Token’ under the bot username. Copy this and store it somewhere secure. It is important to never share your token. If you do, you should regenerate it to prevent abuse.

You can then use the Get User endpoint (/users/{user.id}/) to retrieve the user for an ID. This should be done by the backend as it involves authenticating with the bot token.


Using the API directly

Here is a minimal example of how you would fetch a user by their ID using the Discord API using Node.js:

const fetch = require('node-fetch')

// You might want to store this in an environment variable or something
const token = 'YOUR_TOKEN'

const fetchUser = async id => {
  const response = await fetch(`https://discord.com/api/v9/users/${id}`, {
    headers: {
      Authorization: `Bot ${token}`
    }
  })
  if (!response.ok) throw new Error(`Error status code: ${response.status}`)
  return JSON.parse(await response.json())
}

The response would be something like this:

{
  "id": "123456789012345678",
  "username": "some username",
  "avatar": null,
  "discriminator": "1234",
  "public_flags": 0,
  "banner": null,
  "banner_color": null,
  "accent_color": null
}

Using a library

Alternatively, you may be able to use a Discord library to do this instead. The following examples also handle rate limits.

@discordjs/rest + discord-api-types

const {REST} = require('@discordjs/rest')
const {Routes} = require('discord-api-types/v9')

const token = 'YOUR_TOKEN'

const rest = new REST().setToken(token)

const fetchUser = async id => rest.get(Routes.user(id))

The result would be the same JSON as described above.

For TypeScript users:

import type {RESTGetAPIUserResult, Snowflake} from 'discord-api-types/v9'

const fetchUser = async (id: Snowflake): Promise<RESTGetAPIUserResult> =>
  rest.get(Routes.user(id)) as Promise<RESTGetAPIUserResult>

discord.js

When I first posted this answer, @discordjs/rest didn’t exist yet.

const {Client} = require('discord.js')

const token = 'YOUR_TOKEN'

const client = new Client({intents: []})
client.token = token

const fetchUser = async id => client.users.fetch(id)

The result of fetchUser would be a discord.js User object.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement