Hyperba
Hyperba

Reputation: 119

How do you make sure that user types full command in discord.py?

Take a look at this example:

@client.command()
async def redeem(ctx, code):
    if code in available_codes:
        await ctx.reply("you redeemed the code!")
    else:
        await ctx.reply("It does not exist")

or if you use cogs and stuff:

@commands.command()
async def redeem(self, ctx: commands.Context, code: str)
    if code in available_codes:
        await ctx.reply("you redeemed the code!")
    else:
        await ctx.reply("It does not exist')

both the same thing...

If a person does not type a code, nothing happens...

How do you send a message if someone does not type a code? I tried:

if code == "":
    await ctx.reply("please specify a code")

but it does not work... If you do not type a code, nothing happens as if that line above did not even exist. So how do you carry out an action if there is no code specified? Or at the very least, how do you make sure that the user is at least told to type a code?

Upvotes: 2

Views: 68

Answers (1)

3nws
3nws

Reputation: 1439

You can give a default value to the parameters.

async def redeem(ctx, code=""):
    if code == "":
        await ctx.reply("please specify a code")

Upvotes: 3

Related Questions