0
我试图创建一个简单的不和谐bot使用discord.py它的乐趣。 我很努力地完全理解asyncio的工作方式,并遇到覆盖/重新分配变量的问题。用async/await重新赋值变量?
log_channel = "8765327525217520521501"
@client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('!logchannel'):
if message.content == "!logchannel":
await client.send_message(message.channel, "```\n logchannel:
\r - Changes the channel this bot logs to.
\r - Takes the numerical channel ID as an argument
\r - E.g. !logchannel 123456789 ```")
else:
nc = message.content.split()[1]
try:
await client.send_message(client.get_channel(nc), "Testing new channel to be used for logs...")
except discord.NotFound:
await client.send_message(message.channel, "No channel was found with that ID.")
except discord.Forbidden:
await client.send_message(message.channel, "I Don't have permissions to use that channel!")
except discord.HTTPException:
await client.send_message(message.channel, "There was an error communciating with the server, please try again.")
except InvalidArgument:
await client.send_message(message.channel, "No channel was found with that ID.")
else:
await client.send_message(client.get_channel(log_channel), "Logging Channel Updated.")
await client.send_message(message.channel, "Logging Channel Updated.")
lmsg = 'Logging channel was updated to {} on {}'
log.write(lmsg.format(message.channel,logtime))
log_channel = nc
尝试覆盖log_channel变量会导致“引用前分配”异常。如果我不试图覆盖,那么它可以很好地得到变量的值。
我认为这是因为异步工作的方式,但我不完全理解这一点,我使用示例作为指导。