Python Disxord.py上的无前缀消息
当我说Python Disxord.py上的无前缀消息,python,discord.py,Python,Discord.py,当我说sa时,这个代码应该说as。它检测到单词,但没有响应。这就是我得到的错误: @client.event async def on_message(message,channel): if message.content.startswith("sa"): await channel.send(message.channel, "as") await client.process_commands(message) 我认为这可能是一个过时的代码,所以我试图
sa
时,这个代码应该说as
。它检测到单词,但没有响应。这就是我得到的错误:
@client.event
async def on_message(message,channel):
if message.content.startswith("sa"):
await channel.send(message.channel, "as")
await client.process_commands(message)
我认为这可能是一个过时的代码,所以我试图改变它尽可能新,但我得到了错误
Ignoring exception in on_message
Traceback (most recent call last):
File "C:\Users\---\PycharmProjects\discordmasterbot\venv\lib\site-packages\discord\client.py", line 312, in _run_event
await coro(*args, **kwargs)
TypeError: on_message() missing 1 required positional argument: 'channel'
我不知道代码是从哪里来的,但我在2018年做的一个老项目使用了这个函数签名:
@client.event
async def on_message(message):
if message.content.startswith('sa'):
await message.channel.send('as')
await client.process_commands(message)
然而,从那时起,它看起来像。以下是从以下方面进行操作的新方法:
因此,您需要的可能是最后几个部分:
@client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('$hello'):
await message.channel.send('Hello!')
编辑
您的代码似乎也错误地使用了
process\u命令
部分process_commands
是一种discord.ext.commands.Bot
方法,而不是client
。因此,它应该是bot.process_命令(message)
如果我开发了库,我会通过message或channel传入通道,但不是两者都传入…所以尝试删除参数列表中的通道?它工作于bot,在我放入它后,它不会检测到任何其他命令。连我都不知道!帮助我省略了wait client.process\u命令(消息)
只是为了给您展示一个简单的示例。尝试在底部添加它,如果OP使用client=discord.ext.commands.Bot(command\u prefix='!'),它应该仍然是wait client.process\u commands(message)
。
不,我没有使用它。你们能告诉我在哪里编辑吗
@client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('$hello'):
await message.channel.send('Hello!')
@client.event
async def on_message(message):
if message.content.startswith('sa'):
await message.channel.send('as')