[英]Discord.py ctx commands not recognised
我對一般編碼相當陌生,最近開始嘗試編寫我自己的機器人。 我見過的幾乎所有教程都使用 ctx 命令,但是,每當我使用它時,都會出現此錯誤:
"NameError: name 'ctx' is not defined"
這是我使用 ctx 命令的代碼的一部分。 目的是讓它刪除最后發送的 3 條消息。
@client.event
async def purge(ctx):
"""clear 3 messages from current channel"""
channel = ctx.message.channel
await ctx.message.delete()
await channel.purge(limit=3, check=None, before=None)
return True
@client.event
async def on_message(message):
if message.content.startswith("purge"):
await purge(ctx)
client.run(os.environ['TOKEN'])
完整錯誤:
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)
File "main.py", line 31, in on_message
await purge(ctx)
NameError: name 'ctx' is not defined
如果這有什么不同,我將在 repl.it 上托管服務器,正如我所說,我對編碼很陌生,所以我可能錯過了一些非常明顯的東西,感謝任何幫助。 ^_^
對此的解決方法是:
async def purge(message):
await message.delete()
channel = message.channel
await channel.purge(limit=3)
@client.event
async def on_message(message):
if message.content.startswith("purge"):
await purge(message)
問題是on_message
function 使用message
而不是ctx
。 用ctx
替換消息不起作用,因為我很確定on_message
不能使用ctx
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.