【问题标题】:Command to show set prefix in discord.py (rewrite)在 discord.py 中显示设置前缀的命令(重写)
【发布时间】:2021-02-03 18:20:32
【问题描述】:

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

@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

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

【问题讨论】:

  • 你需要定义command_prefix来做prefix = command_prefix
  • 这样,async def help(ctx, command_prefix):?
  • 不,这不是您需要的,您需要将这些前缀自动保存并发送到一个文件中。
  • 那我可以用json数据来显示吗?
  • 是的,如果你为多个公会做这个机器人,或者你想打印出前缀,或者你想用命令更改前缀,你需要保存它。

标签: python command discord discord.py discord.py-rewrite


【解决方案1】:

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

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)

【讨论】:

    【解决方案2】:

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

    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)
    

    【讨论】:

      猜你喜欢
      • 2021-04-04
      • 1970-01-01
      • 2021-02-07
      • 2019-11-09
      • 2021-04-04
      • 2020-10-19
      • 2021-07-15
      • 2021-09-17
      • 2019-11-30
      相关资源
      最近更新 更多