当命令在discord.py中缺少必需的参数时,如何显示消息

2024-09-20 17:28:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图使用随机答案生成器生成一个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重写的,所以我(再次)求助于堆栈溢出。谢谢你的帮助,如果你帮了我的话,谢谢


Tags: theinmessagetitlemagiclineembedresponses
2条回答

Use error handler checking是错误isinstance of 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)

您只需添加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)

相关问题 更多 >

    热门问题