Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何让机器人说出Discord.py中在线和离线用户的数量_Python_Discord.py - Fatal编程技术网

Python 如何让机器人说出Discord.py中在线和离线用户的数量

Python 如何让机器人说出Discord.py中在线和离线用户的数量,python,discord.py,Python,Discord.py,所以,我想做的是让机器人发送离线和在线用户的数量 @bot.command() async def members(message): id = bot.get_guild(server token here) await message.channel.send(f'Total-Members: {id.member_count}') 这只允许bot说出成员总数,而无需进一步说明。你能帮帮我吗?我想让机器人说出成员总数、离线成员、在线成员和机器人。您可以根据成员的状态筛选成员 @bo

所以,我想做的是让机器人发送离线和在线用户的数量

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

这只允许bot说出成员总数,而无需进一步说明。你能帮帮我吗?我想让机器人说出成员总数、离线成员、在线成员和机器人。

您可以根据成员的状态筛选成员

@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}")



另外,command对象接受
命令。上下文
消息

相关:还有一个非常有用的文档链接。当我尝试此操作时,它工作正常,但它显示总计:1人:0机器人:1在线:0空闲:0不分发:0离线:1这不是正确的人数。我做的和你做的完全一样你做了什么?如果不需要,则需要在开发人员门户中启用它。然后在你的代码中这样定义bot:
bot=commands.bot(command_prefix=“!”,intents=discord.intents.all())
也不要多次问问题,也不要像上次有人说的那样提及我。对不起,我会确保不要提及这样的人,谢谢你(再次)帮助我