'Discord.py non-required options being required
I've been implementing slash commands to my discord bot with mostly no problems until I introduced options with required=False, and I have no idea how to make them work. I've tried changing the arrangements, define the args before, then check them and even rewritten the entire command over again with no success. How can I make the command execute the first cmd7 if only trackname is given, and execute the second if both trackname and artist args are given?
@slash.slash(name='song', description='search for spotify songs', options=[discord_slash.manage_commands.create_option(name='trackname', description='name of the track you\'re searching', option_type=3, required=True), discord_slash.manage_commands.create_option(name='artist', description='track artist\'s name', option_type=3, required=False)])
async def cmd7(ctx:discord_slash.SlashContext, trackname):
resp_json = sp.search(q='track: ' + trackname, type = 'track', limit=1)
for track in resp_json['tracks']['items']:
if str(track['name']).lower() == trackname.lower():
track_id = track['external_urls']['spotify']
print(track_id)
await ctx.send(track_id)
else:
await ctx.send('song not found')
async def cmd7(ctx:discord_slash.SlashContext, trackname, artist):
resp_json=sp.search(q='track: ' + trackname + ', artist: ' + artist, type='track,artist', limit=1)
for track in resp_json['tracks']['items']:
if str(track['name']).lower() == trackname.lower():
track_id = track['external_urls']['spotify']
await ctx.send(track_id)
else:
await ctx.send('song not found')```
Solution 1:[1]
If you are using PyCord:
from discord.commands import Option
async def cmd7(ctx:discord_slash.SlashContext, trackname: Option(str, "The name of the track you would like to search", required=False)):
The first argument: str being the type of variable that the user has to input
The second argument being the description of the Option
The final argument required: whether it's required or not
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 | Apolymoxic |
