【问题标题】:How do display a message when a command is missing a required argument in discord.py当命令在 discord.py 中缺少必需参数时如何显示消息
【发布时间】:2021-05-05 04:02:37
【问题描述】:

我正在尝试使用随机答案生成器制作一个 8ball 命令,但我想添加一个事件,如果您只说“&8ball”,它会说“8ball 需要一个问题”

这是我使用的代码:

@client.command(aliases=['8b','8B','8Ball','b','B','8ball','Ball'])
@cooldown(1, 7, BucketType.user)
async def ball(ctx, *, question):
    embed = discord.Embed(title="????shaking the magic 8ball", colour=ctx.author.colour)
    responses = [line.strip() for line in open('8ball.txt')]
    choices = random.choice(responses)
    embed2 = discord.Embed(title="????the magic 8ball says " + choices, colour=ctx.author.colour)
    message = await ctx.send(embed=embed)
    await asyncio.sleep(5)
    await message.edit(embed=embed2)

这是我要添加的内容:

    embed3 = discord.Embed(title="????The magic 8ball requires a question", colour=ctx.author.colour)
    embed.add_field(text="Brody Foxx")
    await ctx.send(embed=embed3)

我也尝试使用 ifelse 语句

if question in message.content:

类似的东西,试着在谷歌和 YouTube 上找遍了,我要么找不到任何东西,要么它们都在 python 中重写,所以我(再一次)求助于堆栈溢出。谢谢你的帮助,如果你帮助了我,谢谢。

【问题讨论】:

    标签: python python-3.x discord discord.py


    【解决方案1】:

    使用错误处理程序检查是commands.MissingRequiredArgument 的错误实例,并且字段需要名称和值而不仅仅是名称。

    你可能想要一个页脚

    @client.command(aliases=['8b','8B','8Ball','b','B','8ball','Ball'])
    @cooldown(1, 7, BucketType.user)
    async def ball(ctx, *, question):
        embed = discord.Embed(title="?shaking the magic 8ball", colour=ctx.author.colour)
        responses = [line.strip() for line in open('8ball.txt')]
        choices = random.choice(responses)
        embed2 = discord.Embed(title="?the magic 8ball says " + choices, colour=ctx.author.colour)
        message = await ctx.send(embed=embed)
        await asyncio.sleep(5)
        await message.edit(embed=embed2)
    @ball.error
    async def ball_error(ctx, error):
        if isinstance(error, commands.MissingRequiredArgument):
            embed3 = discord.Embed(title="?The magic 8ball requires a question", colour=ctx.author.colour)
            embed3.set_footer(text="Brody Foxx")
            await ctx.send(embed=embed3)
        else: raise(error)
    

    【讨论】:

      【解决方案2】:

      如果要求不能通过,可以简单的添加None,可以像下面这样使用:

      @client.command(aliases=['8b','8B','8Ball','b','B','8ball','Ball'])
      @cooldown(1, 7, BucketType.user)
      async def ball(ctx, *, question=None):
          
          if question == None:
              embed3 = discord.Embed(title="?The magic 8ball requires a question", colour=ctx.author.colour)
              embed.add_field(text="Brody Foxx")
              await ctx.send(embed=embed3)
              return
      
          embed = discord.Embed(title="?shaking the magic 8ball", colour=ctx.author.colour)
          responses = [line.strip() for line in open('8ball.txt')]
          choices = random.choice(responses)
          embed2 = discord.Embed(title="?the magic 8ball says " + choices, colour=ctx.author.colour)
          message = await ctx.send(embed=embed)
          await asyncio.sleep(5)
          await message.edit(embed=embed2)
      
      

      【讨论】:

      • 我试过了,当我执行命令时它没有发送任何内容,它不会响应我用命令尝试的任何内容:(
      • 你定义了嵌入吗?
      猜你喜欢
      • 2020-04-26
      • 1970-01-01
      • 2020-12-16
      • 2021-01-24
      • 1970-01-01
      • 1970-01-01
      • 2021-08-24
      • 1970-01-01
      • 2022-01-10
      相关资源
      最近更新 更多