【发布时间】:2021-06-07 23:54:38
【问题描述】:
我不断收到此错误,想知道如何解决它:
@bot.event
async def on_message(message):
if isinstance(message.channel, discord.channel.DMChannel) and message.author != bot.user:
async with message.channel.typing():
responses = ["word", "car"]
await asyncio.sleep(1)
await message.channel.send(f'{random.choice(responses)}')
我得到的错误是: 忽略 on_message 中的异常
Traceback (most recent call last):
File "C:\Python38\lib\site-packages\discord\client.py", line 343, in _run_event
await coro(*args, **kwargs)
File "C:\Users\endle\Documents\MyDiscordBot\bot.py", line 112, in on_message
await message.channel.send(f'{random.choice(responses)}')
UnboundLocalError: local variable 'responses' referenced before assignment
有人可以帮忙吗?
【问题讨论】:
-
如果
if isinstance(message.channel, discord.channel.DMChannel) . . .检查为假,responses会是什么? -
responses 是我用来随机选择一个字符串并发送它的变量
-
responses仅在第一个if语句为真时作为变量存在。如果为 false,则不会创建变量并且您会收到错误消息。 -
John Gordon,我在哪里使我的 if 语句为真,对不起,我不是最好的 python
标签: python python-3.x discord.py