Home > Back-end >  How do you make sure that user types full command in discord.py?
How do you make sure that user types full command in discord.py?

Time:04-07

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?

CodePudding user response:

You can give a default value to the parameters.

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