Search code examples
pythondiscord.pycommand

Discord Bot in Python: Using parameters to make commands


So, while I was making my command script I was wondering how to get 2 quick answers like other bots. I saw that some bots when typing the command like /coinflip it was asking me for a parameter where it was asking which side, like heads or tails and it was showing 2 "quick answers", like showing the 2 options: heads or tails, and I wanted to make this same thing with my bot. I tried looking for a tutorial but I don't find anything helpful. Could you guys help?

My idea:

@bot.tree.command(name="createcoinflip", description=f"Creates a coinflip based on the arguments.")
async def describe(interaction: discord.Interaction, amount: int, side: commands.Option(choices=["Heads", "Tails", "Random"])): 
#make pop up these 3 answers when typing in discord the command and being in the parameter "side"
    await interaction.send(f"You chose {side} with an amount of {amount} gems.")

Well, I tried using commands.Option(choices=["Heads", "Tails", "Random"]) but that isn't a thing. I know I can make a condition if the parameter == "heads" but I want it to also pop up as "tip" to don't have to manually type it every time.

Also, I was trying to make optional arguments, like when doing /balance it asks for the player but if the person that does the command doesn't type something, it will be automatically chose the user of the person that sent that command.


Solution

  • For the first question, check the documentation for using autocomplete.

    About the second question, you can define parameters as optional by setting a default value for it (None for example). Here's an example command that has an optional player parameter:

    @bot.tree.command(name="profile")
    async def profile(interaction: discord.Interaction, player: discord.Member = None):
        """View profile
    
        Args:
            player: view another player's profile
        """
    
        # if the user do not enter a member, player will be None
        # so, if player is None, we will atribute interaction.user to player 
        player = player or interaction.user