【问题标题】:Extra Spaces after custom prefix [Discord.py]自定义前缀后的额外空格 [Discord.py]
【发布时间】:2020-12-31 18:07:16
【问题描述】:

我尝试制作自定义前缀命令,但为了提高效率,如果前缀包含字母字符(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}`.")```

【问题讨论】:

    标签: sqlite discord discord.py discord.py-rewrite


    【解决方案1】:

    那是因为你在 if 语句中定义了 new_prefix,所以 new_prefix 只有在 if 语句为 True(有时可能为 False)时才有意义,所以它说在定义之前使用。

    如果您在命令或 else 语句的开头执行new_prefix = '',它应该可以解决错误

    【讨论】:

    • 实际上,我必须将new_prefix设置为new_prefix = prefix,所以如果没有触发空格,它会添加正常的前缀,如果你用@做它会是''或None 987654326@ 但无论如何谢谢!
    猜你喜欢
    • 2021-03-31
    • 2021-04-04
    • 2021-11-01
    • 1970-01-01
    • 2021-03-30
    • 1970-01-01
    • 2021-07-19
    • 1970-01-01
    • 2015-08-02
    相关资源
    最近更新 更多