Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get total member count of any Discord server?

I'm trying to build a scraping script to get a bunch of Discord server's total members. I actually did that with Puppeteer like below but I think my IP address has been banned because I'm getting "Invite Invalid" error from Discord even though invite links are working.

My question is that does Discord have APIs to get any server's total member count? Or is there any 3rd party library for that purpose? Or any other method?

const puppeteer = require('puppeteer')

const discordMembers = async ({ server, browser }) => {
  if (!server) return

  let totalMembers

  const page = await browser.newPage()

  try {
    await page.goto(`https://discord.com/invite/${server}`, {
      timeout: 3000
    })

    const selector = '.pill-qMtBTq'
    await page.waitForSelector(selector, {
      timeout: 3000
    })
    const totalMembersContent = await page.evaluate(selector => {
      return document.querySelectorAll(selector)[1].textContent
    }, selector)

    if (totalMembersContent) {
      totalMembers = totalMembersContent
        .replace(/ Members/, '')
        .replace(/,/g, '')
      totalMembers = parseInt(totalMembers)
    }
  } catch (err) {
    console.log(err.message)
  }

  await page.close()

  if (totalMembers) return totalMembers
}

const asyncForEach = async (array, callback) => {
  for (let i = 0; i < array.length; i++) {
    await callback(array[i], i, array)
  }
}

const run = async () => {
  const browser = await puppeteer.launch({
    headless: true,
    args: ['--no-sandbox']
  })

  const servers = ['tQp4pSE', '3P5K3dzgdB']

  await asyncForEach(servers, async server => {
    const members = await discordMembers({ server, browser })
    console.log({ server, members })

    // result
    // { server: 'tQp4pSE', members: 57600 }
    // { server: '3P5K3dzgdB', members: 159106 }
  })

  await browser.close()
}

run()

Update: Mar 22, 2022

Thanks for @Vaviloff's answer we can actually access Discord's private APIs but the problem is it's only accessible over browser. I'm getting Request failed with status code 400 issue from Axios. Is it a CORS issue? How do we get the results in a Node.js app?

const axios = require('axios')

const discordMembers = async ({ server }) => {
  try {
    const apiResult = await axios({
      data: {},
      method: 'get',
      url: `https://discord.com/api/v9/invites/${server}?with_counts=true&with_expiration=true`
    })
    console.log(apiResult)
  } catch (err) {
    console.log(err)
  }
}

discordMembers({ server: 'tQp4pSE' })
like image 498
ozgrozer Avatar asked Oct 22 '25 19:10

ozgrozer


1 Answers

A lot of modern web applications have their own internal APIs. Oftentimes you can spot frontend making requests to it, by using Networking tab in Devtools (filter by Fetch/XHR type):

internal API requests image

Such API endpoints can change any time of course, but usually the last for a long time and is a rather convenient way of scraping

Currently Discord uses this URL for basic instance description:

https://discord.com/api/v9/invites/tQp4pSE?with_counts=true&with_expiration=true

By accessing it you get the desired data:
Discord instance stats from internal API


Update
To make your code work don't send any data in the request:

const apiResult = await axios({
  method: 'get',
  url: `https://discord.com/api/v9/invites/${server}?with_counts=true&with_expiration=true`
})
like image 64
Vaviloff Avatar answered Oct 25 '25 01:10

Vaviloff



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!