首页 > 解决方案 > 当命令在 discord.py 中缺少必需参数时如何显示消息

问题描述

我正在尝试使用随机答案生成器制作 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)

我也尝试使用ifandelse语句

if question in message.content:

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

标签: pythonpython-3.xdiscorddiscord.py

解决方案


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)


推荐阅读