我如何使具有memberCount的机器人状态?

时间:2020-02-14 11:24:01

标签: node.js bots discord discord.js

discord.js的新功能。我制作了一个简单的Discord验证机器人,但我想使该机器人状态为观看#个人进行验证(#是我服务器中的人数)。我看到有些机器人拥有它,但我不知道该怎么做。这是我当前的机器人状态代码

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);
    }

4 个答案:

答案 0 :(得分:1)

首先,您需要使用set interval命令来更新成员。

此操作不需要使用this.user。上一个答案方法将仅显示cached个用户,所以这是错误的方式,因为在bot启动时,该集合中将没有用户。

如果您需要在自己的服务器上使用显示成员,则可以这样:

  1. 通过ID获取公会
  2. 获取属性guild.memberCount
  3. 每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);

});

机器人启动后,此信息将在5分钟后更新。

为了进行测试,您可以将}, 1000 * 60 * 5) 更改为}, 1000);

答案 1 :(得分:0)

欢迎使用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文档中找到它。

答案 2 :(得分:0)

它很简单:

bot.user.setPresence({ activity: { name: `${bot.users.cache.size} members` , type: 'WATCHING'}, status: 'online' })

答案 3 :(得分:0)

使用Discord.js v12 API

代替client.guilds.get('GUILD ID HERE')

您应该使用client.guilds.cache.get('GUILD ID HERE')