自定义前缀 [Discord.py] 后的额外空格
Extra Spaces after custom prefix [Discord.py]
我尝试制作自定义前缀命令,但为了提高效率,如果前缀包含字母字符 (a-z),我将在其中添加额外的空格,但是当我使用它时,它说 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}`.")```
那是因为你在 if 语句中定义了 new_prefix
,所以 new_prefix
只有在 if
语句为 True 时才有效,有时可能为 False,所以它表示已使用在定义之前。
如果您在命令或 else 语句的开头执行 new_prefix = ''
,它应该可以解决错误
我尝试制作自定义前缀命令,但为了提高效率,如果前缀包含字母字符 (a-z),我将在其中添加额外的空格,但是当我使用它时,它说 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}`.")```
那是因为你在 if 语句中定义了 new_prefix
,所以 new_prefix
只有在 if
语句为 True 时才有效,有时可能为 False,所以它表示已使用在定义之前。
如果您在命令或 else 语句的开头执行 new_prefix = ''
,它应该可以解决错误