'Multiple optional command parameters with discord.py
I was looking at asking for multiple optional parameters with discord.py, although I'm not sure how to do it. This is my example, although I'm pretty sure it's wrong.
def __init__(self, bot):
self.bot = bot
@commands.cooldown(rate = 1, per = .75, type = commands.BucketType.user)
#@bot.command.cooldown(1, 10, commands.BucketType.default)
@bot.command(name = "search", alises = ['find')
async def ecommand(ctx, limit, *tags, *args):
print('Testing API')```
I'd like to to receive input where it checks for args, but not all of them are needed to function.. example: Command can receive e!search 10 cat or e!search 10 fav_count cat and still work.
For further note, I'm taking these args and using them in a API search, so I need a way of getting the information from each arg, rather then just putting it all as one. Maybe there is another way to do this tho, I am just unsure.
Thank you for the help :)
Solution 1:[1]
Some tiny mistake you should fix before continuing :
-aliases was misspelled.
-missing the close bracket "}" for aliases.
-the first argument of the function "ecommand" should be "self" instead of "ctx" since you are doing it inside of a class.
Set a default value for the argument
@bot.command(name = "search", aliases = ['find'])
async def search_command(self,ctx,limit,*,args=None):
if args == None:
await ctx.send("Enter a something to search !")
#Turn it into a list
arg_list = args.split()
#Your Searching code
By doing this, you set the default value of the argument to None
. So
that it does not cause an error and break the whole script when the user enter nothing to search.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |