首页 > 解决方案 > 每次具有特定角色的用户在任何频道发言时,如何让机器人发送消息 | 不和谐.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'

标签: pythonpython-3.xdiscorddiscord.py

解决方案


正如你的错误所说,on_message只接受message作为位置参数,你提供了两个,即messagectx

这里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")

推荐阅读