python - 我对“分配前引用的局部变量'公会'”有一个大问题
问题描述
几个月前,我制作了自己的 Discord 机器人。在我决定将我的机器人移动到我伙伴的服务器之前,这一切都很有趣和游戏。所以我不得不改变 .env 文件中的 DISCORD_GUILD 。我做到了,但是,在 shell 中,我只得到“分配前引用的局部变量 'guild'”。请帮帮我,它越来越没有希望了。我在这个机器人的代码上花了大约 2 周的时间......
感谢您的回答!
编辑:经过一些更改,它说 'unindent does not match any external indentation level' 。我不明白。
@client.event
async def on_ready():
for guild in client.guilds:
if guild.name == GUILD:
break
print(
f'{client.user} is connected to the following guild:\n'
f'{guild.name}(id: {guild.id})'
)
Edit2:我将发布整个代码(没有 .env ofc)这里是:
import os
import discord
from dotenv import load_dotenv
load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
GUILD = os.getenv('DISCORD_GUILD')
client = discord.Client()
@client.event
async def on_ready():
for guild in client.guild:
if guild.name == GUILD:
break
print(f'{client.user} is connected to the following guild:\m')
print(f'{guild.name}(id: {guild.id})')