python - 每次具有特定角色的用户在任何频道发言时,如何让机器人发送消息 | 不和谐.py
问题描述
每当有欺负角色的人说话时,我都试图让机器人发送一条消息。我还想让它在该响应消息中提及用户,但这当然是可选的。我对 discord.py 有点陌生,但仍然知道很多事情。
这是我的代码:
@client.event
@commands.has_role('Bullied')
async def on_message(ctx, message):
if message.content != "":
if y == 0 or y2 == 0 or y3 == 0:
await ctx.send("hi")
这是每次有人在服务器上讲话时我收到的错误消息:
Ignoring exception in on_message
Traceback (most recent call last):
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/client.py", line 343, in _run_event
await coro(*args, **kwargs)
TypeError: on_message() missing 1 required positional argument: 'message'
解决方案
正如你的错误所说,on_message
只接受message
作为位置参数,你提供了两个,即message
和ctx
这里ctx
不是充当context
而是message
,然后您提供了一个message
它不知道的额外参数[因为ctx
充当discord.Message
对象]
@client.event
@commands.has_role('Bullied')
async def on_message(message):
if message.author.bot:
return
if message.content != "":
if y == 0 or y2 == 0 or y3 == 0: #what is this? I am not sure what y, y2, and y3 are (if not defined, would raise UnboundLocalError, local variable 'y' referenced before assignment)
await message.channel.send(f"{message.author.mention} hi")
推荐阅读
- c++ - 三元允许隐式调用显式复制构造函数?
- r - 文档的余弦相似度
- android - 从附加的 RecyclerView.Adapter 调用 Fragment 方法
- python - Django:刷新页面重新提交表单
- c# - c# - 不断尝试通过 Func 连接到服务器
- 但 SqlConnection 参数保持为空 - angularjs - 运行springboot应用程序时出现whitelabel错误页面
- java - Spring @profile 如何与继承一起工作?
- java - StringEscapeUtils.unescapeHtml4 不能为内部值工作(3.5g)
- java - 如何在rabbitMq消费者上消费多条消息
- angularjs - AngularJs 不使用 umbraco 插件调用控制器