有没有办法查看一个不和谐服务器中的所有在线成员是否都在玩游戏 -- discord.py

Is there a way to see if all online members in a discord server are playing a game -- discord.py

我正在尝试使用 discord.py 为我的 discord 机器人发出命令,其中 return 是玩某个游戏的每个人的列表。

这是我目前的代码:

@client.command()
async def members_info(ctx):
    server_members = ctx.guild.members
    data = [member.name for member in server_members if "ActivityType.playing" in member.activity]
    embed = discord.Embed(title=f'Server Members', description=f'{data}', color=discord.Color.dark_gray())
    await ctx.send(embed=embed)

代码return错误:

discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: argument of type 'Game' is not iterable

我是 Python 的新手,也是使用 discord.py 库的初学者。这也是我的第一个堆栈溢出 post,因此非常感谢任何帮助。

member.activity has an ActivityType 让您弄清楚 activity 是什么。然而,有时这个属性不存在,所以你可以 return False.

def is_game(activity):
    try:
        return activity.type == discord.ActivityType.playing
    except AttributeError:  # sometimes it could be a `BaseActivity` or `Spotify`
        return False

@client.command()
async def members_info(ctx):
    server_members = ctx.guild.members
    data = [member.name for member in server_members if is_game(member.activity)]
    embed = discord.Embed(title=f'Server Members', description='\n'.join(data), color=discord.Color.dark_gray())
    await ctx.send(embed=embed)