Online User count from a Discord Role

前端 未结 2 1975
孤城傲影
孤城傲影 2021-01-28 18:37

I want my Discord Bot to display the Number of Online Users of a Role as an Activity.
I can\'t seem to figure it out and i can\'t find anything on the Web.
Can someone g

2条回答
  •  孤城傲影
    2021-01-28 19:12

    You can also use filter:

    // Discord.js v12 (latest version):
    const count = guild.members.cache.filter(m =>
      // If the member has the role
      m.roles.cache.has('role id') &&
      // and the member is online
      m.presence.status === 'online'
    ).size
    
    // Discord.js v11:
    const count = guild.members.filter(m =>
      // If the member has the role
      m.roles.has('role id') &&
      // and the member is online
      m.presence.status === 'online'
    ).size
    
    // Use count here:
    client.user.setActivity('...')
    

    To get a guild by its ID, use this:

    // v12
    const guild = client.guilds.cache.get('id')
    // v11
    const guild = client.guilds.get('id')
    

提交回复
热议问题