【问题标题】:How do I make a bot status that has memberCount on it?如何创建一个有 memberCount 的机器人状态?
【发布时间】:2020-05-30 04:42:54
【问题描述】:
我制作了一个简单的不和谐验证机器人,但我想让机器人状态观看 # 人 验证(# 是我的服务器中有多少人)。我看到一些机器人有它,但我不知道如何制作它。这是我当前的机器人状态代码:
if (Object.keys(this.config.presence).length !== 0) {
this.user.setPresence({
game: {
name: this.config.presence.name,
type: this.config.presence.type
},
status: "online"
}).catch(console.error);
}
【问题讨论】:
标签:
node.js
bots
discord
discord.js
【解决方案1】:
就这么简单:
bot.user.setPresence({ activity: { name: `${bot.users.cache.size} members` , type: 'WATCHING'}, status: 'online' })
【解决方案2】:
首先你需要设置间隔命令来更新成员。
您不需要使用this.user 进行此操作。以前的回答方法将只显示cached 用户,所以它是错误的方式,因为在机器人启动时,您将在此集合中没有用户。
如果您需要在自己的服务器上显示成员,您可以这样做:
- 通过 ID 获取您的公会
- 获取属性
guild.memberCount
- 每 5 分钟更新一次
client.on('ready', () => {
setInterval(() => {
targetGuild = client.guilds.get('GUILD ID HERE')
if(targetGuild) {
client.user.setPresence({ game: { name: targetGuild.memberCount + ' people verifying!', type: 'WATCHING' }, status: 'online' })
.then(console.log)
.catch(console.error);
}
}, 1000 * 60 * 5);
});
bot 启动后,5 分钟后更新。
对于测试,您可以将}, 1000 * 60 * 5) 更改为}, 1000);
【解决方案3】:
欢迎来到 StackOverflow!
client.users.size 应该返回该机器人所在的所有公会的所有用户!
if (Object.keys(this.config.presence).length !== 0) {
this.user.setPresence({
game: {
name: client.users.size + ' people verifying!',
type: this.config.presence.type
},
status: "online"
}).catch(console.error);
}
您也可以在 discord.js 文档中找到此内容。
【讨论】:
-
-
-
client.users.size 只显示chached 用户,所以一开始这是零收集