我试图制作一个自定义的前缀命令,但是为了使它高效,我将在其中添加额外的空格(如果该前缀包含字母字符(az)),但是当我使用它时,它表示new_prefix在赋值之前被引用,它有效,我只是想知道为什么吗?
@command(name='prefix')
@has_permissions(manage_guild=True)
@guild_only()
async def prefix_cmd(self, ctx, *, prefix=None):
if prefix is None:
async with connect('dumbo.db') as db:
prefix_db = await db.execute(f"SELECT Prefix FROM guilds WHERE GuildID = {ctx.guild.id}")
server_prefix = await prefix_db.fetchone()
await ctx.send(f"{emotrue} This server prefix is: `{server_prefix[0]}`.")
else:
for spaced in alphabet:
if spaced in prefix.lower():
new_prefix = f'{prefix} '
async with connect('dumbo.db') as db:
await db.execute(f"UPDATE guilds SET Prefix = ? WHERE GuildID = ?", (f'{new_prefix}', ctx.guild.id))
await db.commit()
await ctx.send(f"{emotrue} This server prefix is now updated to `{prefix}`.")```
答案 0 :(得分:1)
那是因为您在if语句中定义了new_prefix
,所以new_prefix
仅在该if
语句为True且有时可能为False的情况下才是问题,因此它说在定义之前
如果您在命令或else语句的开头执行new_prefix = ''
,它将解决错误