DaGeyHat.mp3
DaGeyHat.mp3

Reputation: 25

How do display a message when a command is missing a required argument in discord.py

I am trying to make an 8ball command with a random answer generator, but I wanted to add an event where if you only say "&8ball" it will say something like "the 8ball requires a question"

Here is the code that I used:

@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)

And here is what I'm trying to add:

    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)

I tried using if and else statements aswell

if question in message.content:

Something like that, tried looking all over Google and YouTube and I either couldn't find anything or they where all in python rewrite so I (once again) resorted to stack overflow. Thanks for your help if you helped me in anyway, thanks.

Upvotes: 1

Views: 154

Answers (2)

Just for fun
Just for fun

Reputation: 4225

Use error handler checking is error isinstance of commands.MissingRequiredArgument and fields requires a name and value not only name.

You probably want a footer

@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)

Upvotes: 2

Cohen
Cohen

Reputation: 2415

You can simply add None if the requirement cannot be passed, it can be used like below:

@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)

Upvotes: 2

Related Questions