python - 如果成员名称在消息中,则检查 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'
有人可以告诉我我的代码有什么问题吗?
解决方案
您的 message 变量是其内容的字符串,因此您不能为公会成员调用它。您正在寻找的是上下文(ctx)。
@client.command()
async def test(ctx, *, message):
for member in ctx.guild.members:
if member.name in message:
print(member.name)
推荐阅读
- laravel - laravel livewire 异常:不支持具有多个模型连接的队列集合
- javascript - 如何添加可自定义的支付按钮?
- java - 测试类未与其他类(形状、正方形、矩形、圆形)集成
- if-statement - 在单个 if 语句中使用 AND 评估组合的多个条件
- python-3.x - 使用带 vargs 的类型
- docker - docker-compose 从主机网络上的容器解析桥上的容器名称
- javascript - 有人知道为什么这个 XHR 请求没有发布吗?错误 500
- python - Pandas:强制从 Excel 文件中的多行合并为单行到单行
- cors - Access-Control-Allow-Methods 是否应该包括 OPTIONS
- nuxmv - NuXmv 程序在启动时无法识别名称