在 discord.py 中显示集前缀的命令(重写)

Command to show set prefix in discord.py (rewrite)

我正在尝试制作一个帮助命令,发送嵌入机器人的当前服务器前缀。我在尝试弄清楚如何对此进行编码时遇到了一些麻烦,我希望得到一些帮助。我试过这样做:

@bot.command()
async def help(ctx):
    embed = discord.Embed(
        title='Help', description='', colour=discord.Colour.blue())
    embed.set_footer(text='Have fun!')

    prefix = command_prefix

    embed.add_field(
        name='Prefix',
        value=
        f'The current prefix for this server is {prefix}',
        inline=True)

    await ctx.send(embed=embed)

但是,当我这样做时,出现错误:

raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: NameError: name 'command_prefix' is not defined

我不确定自己做错了什么,如有任何帮助,我们将不胜感激。 (:

您应该创建一个前缀变量,然后您可以在帮助命令中显示该变量并稍后更改:

prefix="!!"
bot = commands.Bot(command_prefix=prefix)

@bot.command()
async def help(ctx):
    embed = discord.Embed(
        title='Help', description='', colour=discord.Colour.blue())
    embed.set_footer(text='Have fun!')

    embed.add_field(
        name='Prefix',
        value=
        f'The current prefix for this server is {prefix}',
        inline=True)

    await ctx.send(embed=embed)

如果还有人对这个问题感兴趣,你可以这样做:

client.commmand_prefix #if u use bot, just use bot.command_prefix

在这种情况下它将是:

@bot.command()
async def help(ctx):
    embed = discord.Embed(
        title='Help', description='', colour=discord.Colour.blue())
    embed.set_footer(text='Have fun!')

    prefix = command_prefix

    embed.add_field(
        name='Prefix',
        value=
        f'The current prefix for this server is {bot.command_prefix}',
        inline=True)

    await ctx.send(embed=embed)