【问题标题】:How To Make The Bot Say The Amount Of Online And Offline People In Discord.py如何让机器人说出 Discord.py 中的在线和离线人数
【发布时间】:2021-08-01 05:45:46
【问题描述】:

所以,我想做的是让机器人发送线下和线上的人数。

@bot.command()
async def members(message):
  id = bot.get_guild(server token here)
  await message.channel.send(f'Total-Members: {id.member_count}')

这只让机器人说出总成员的数量,而没有任何进一步的说明。你能帮我么?我想让机器人说出总成员、离线成员、在线成员和机器人。

【问题讨论】:

标签: python discord.py


【解决方案1】:

您可以根据成员的状态过滤成员。

@bot.command()
async def members(ctx):
    total = len(ctx.guild.members)
    online = len(list(filter(lambda m: str(m.status) == "online", ctx.guild.members)))
    idle = len(list(filter(lambda m: str(m.status) == "idle", ctx.guild.members)))
    dnd = len(list(filter(lambda m: str(m.status) == "dnd", ctx.guild.members)))
    offline = len(list(filter(lambda m: str(m.status) == "offline", ctx.guild.members)))
    humans = len(list(filter(lambda m: not m.bot, ctx.guild.members)))
    bots = len(list(filter(lambda m: m.bot, ctx.guild.members)))
    await ctx.send(f"Total: {total}\nHumans: {humans}\nBots: {bots}\nOnline: {online}\nIdle: {idle}\nDo not Distrub: {dnd}\nOffline: {offline}")


命令对象也接受commands.Context 而不是message

【讨论】:

  • 当我尝试这个时,它可以工作,但它说 Total: 1 Humans: 0 Bots: 1 Online: 0 Idle: 0 Do not Distrub: 0​​ Offline: 1 这不是正确的人数.我做了你做的事
  • 您是否启用了意图?如果不需要,则需要在开发人员门户中启用它。然后在你的代码中定义这样的机器人:bot = commands.Bot(command_prefix="!", intents=discord.Intents.all())
  • 也不要多次提问,也不要像last time someone has said那样提及我。
  • 对不起,我会确保不再提及这样的人
  • 现在它说 raise PriveligedIntentsRequired(exc.shard_id) from None 是一个错误
猜你喜欢
  • 2021-04-04
  • 1970-01-01
  • 1970-01-01
  • 2020-09-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-17
  • 2020-07-13
  • 2021-12-21
相关资源
最近更新 更多