|discord.py |我正在为我的服务器制作一个 24/7 全天候吟唱机器人,但我收到此错误:未绑定本地错误:分配前'ss'引用局部变量



我是discord.py的初学者,不明白为什么我的bot不正确。我使用VS Code IDE通过命令提示符或Repl.it来编写和执行我的机器人程序。我正在制作一个机器人程序,在我的discord服务器中全天候吟唱一些台词。

当我试图运行我的机器人并写";ch!"开始";在我的服务器中,它给了我这个错误:

Traceback (most recent call last):
File "...AppDataRoamingPythonPython39site-packagesdiscordclient.py", line 343, in _run_event
await coro(*args, **kwargs)
File "...bot.py", line 36, in on_message
await chant()
File "...bot.py", line 17, in chant
if ss == 'not chanting':
UnboundLocalError: local variable 'ss' referenced before assignment

为什么会发生这种情况,我该如何解决?

这是我的代码:

# bot.py
#disabled from keep_alive import keep_alive
import discord
import os
import time
client = discord.Client()
ss = 'not chanting'
@client.event
async def on_ready():
print(f'{client.user} has connected to Discord!')
print('I am ready to chant.')
async def chant():
if ss == 'not chanting':
ss = chanting
print('changed not chanting to chanting')

elif ss == 'chanting':
ss = 'not chanting'
print('changed chanting to not chanting')
@client.event
async def on_message(message):
msg = message.content
sendmsg = message.channel.send
author = message.author
if msg == 'ch!help':
await sendmsg('**24/7 Chanter**nch!help - gives a list of commands I can donch!start - starts chanting in the specified channelnch!stop - stops chanting')

if msg == 'ch!start':
if ss == 'not chanting':
await chant()
await sendmsg('Ok. Chanting now.')
await sendmsg('(This message triggers the code to chant.) reference')

elif ss == 'chanting':
await sendmsg('I **am** chanting!')
if msg == 'ch!stop':
if ss == 'chanting':
await chant()
await sendmsg('Ok. Stopping chanting...')
await sendmsg('Please wait...')
elif ss == 'not chanting':
await sendmsg('I **am not** chanting!')
if msg.endswith('reference'):
await sendmsg('passed test 1')
if author == client.user:
await sendmsg('passed test 2')
if ss == 'chanting':
await sendmsg('passed test 3')
await asyncio.time(4)
await sendmsg('passed test 4')
await sendmsg('(MSG)')
await asyncio.time(1)
await sendmsg('(MSG)')
await asyncio.time(3)
await sendmsg('(MSG)')
await asyncio.time(7)
await sendmsg('(MSG)')
await asyncio.time(9)
await sendmsg('(MSG)')
await asyncio.time(5)
await sendmsg('(MSG) reference')

client.run('no token 4 u')

存在多个错误。一个是在async def chanting():中尝试ss = 'chanting',而不是ss = chanting。此外,ss不是全局变量。你需要把它变成一个。查看有关如何执行此操作的评论。

相关内容

最新更新