【发布时间】:2020-12-09 09:31:21
【问题描述】:
我的 discord.py 脚本给了我一个意外错误。它基本上将我在async def on_message(): 之外定义但在其下使用的所有变量标记为“未定义”。
好吧,您也可以通过在on_message() 下定义它们来轻松修复它。我试过了。
但是,问题来了:每次发送消息时,变量都会被设置回您定义的值,即使您确实添加了,就像一行说如果发生这种情况,请将其更改为另一个值。 (例如前缀,正如您在下面的代码中看到的那样,我想使用名为“changeprefix”的命令更改机器人的前缀并且它可以工作,但是当使用该前缀发送另一条消息时,它不会识别它;因为在开头定义变量的行再次运行,一切都被重置了。
除了删除on_message() 并使用discord.ext.commands 或其他方法之外,还有其他方法吗?我对on-message()系统比较熟悉,而且如果我切换到它,我会更改整个脚本。
import discord
client = discord.Client()
token= "some nice token"
#defining here results in "UnboundLocalError: local variable 'prefix' referenced before assignment", in the first line using the variable defined here
@client.event
async def on_message(message):
#defining here results variables being refreshed each time a message is sent
【问题讨论】:
标签: python discord undefined discord.py