首页 > 解决方案 > 如果成员名称在消息中,则检查 Discord.py 中的 message.content

问题描述

我的 Discord.py 机器人中有一个函数,我试图检查命令中的参数之一是否是成员的名称。

这是代码:

@client.command()
async def test(ctx, *, message):
    x = message.guild.members
    for member in x:
        if member.name in message.content:
            print(member.name)

例如:$test John Tom Alice 应该在控制台中打印:
John
Tom
Alice

我试过这个但是这个错误不断弹出:

discord.ext.commands.errors.CommandInvokeError:命令引发异常:AttributeError:'str'对象没有属性'guild'

有人可以告诉我我的代码有什么问题吗?

标签: pythondiscord.py

解决方案


您的 message 变量是其内容的字符串,因此您不能为公会成员调用它。您正在寻找的是上下文(ctx)。

@client.command()
async def test(ctx, *, message):
for member in ctx.guild.members:
    if member.name in message:
        print(member.name)

推荐阅读