来自 Discord 角色的在线用户数
Online User count from a Discord Role
我希望我的 Discord Bot 将某个角色的在线用户数显示为 Activity。
我似乎无法弄清楚,我在网上找不到任何东西。
谁能给我示例代码或向我解释一下?
您可以使用 Guild.members.forEach()
to loop through every member of the guild, then if they have that role (you can use GuildMember.roles.has
(Role.id)
to check that) increase a counter. When you have finished your loop through the members, use the counter in your Client.user.setActivity()
.
这就是你为了得到你想要的东西所需要的。
尝试这些东西,如果您仍然遇到问题 post MCVE 我们会帮助您,但首先您需要自己尝试。
您也可以使用 filter
:
// Discord.js v12/v13 (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('...')
要通过 ID 获取公会,请使用:
// v12/v13
const guild = client.guilds.cache.get('id')
// v11
const guild = client.guilds.get('id')
我希望我的 Discord Bot 将某个角色的在线用户数显示为 Activity。
我似乎无法弄清楚,我在网上找不到任何东西。
谁能给我示例代码或向我解释一下?
您可以使用 Guild.members.forEach()
to loop through every member of the guild, then if they have that role (you can use GuildMember.roles.has
(Role.id)
to check that) increase a counter. When you have finished your loop through the members, use the counter in your Client.user.setActivity()
.
这就是你为了得到你想要的东西所需要的。
尝试这些东西,如果您仍然遇到问题 post MCVE 我们会帮助您,但首先您需要自己尝试。
您也可以使用 filter
:
// Discord.js v12/v13 (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('...')
要通过 ID 获取公会,请使用:
// v12/v13
const guild = client.guilds.cache.get('id')
// v11
const guild = client.guilds.get('id')