Search code examples
python-3.xdiscorddiscord.py

discord.py SelectMenu in slash_command with SelectOption's


I want to create a tree.command that is a slash_command, in the slash_comamnd the user can choose a SelectOption in a SelectMenu and if the SelectOption with the variable'hack' is choosen it shall send a response. The bot wont even start now because i get the TypeError: "'member_descriptor' object is not callable" This is the script:

@tree.command(name='test', description='test', guild=discord.Object(12345678))
async def slash_command(interaction: discord.Interaction, reporttype: discord.SelectMenu(discord.SelectMenu.options(
    discord.SelectOption(label='Hacking', value='hack', description='did you experience someone hacking?'), 
    discord.SelectOption(label='Harassment', value='harass', description='did someone harass you?'),
    discord.SelectOption(label='Discord ToS', value='tos', description='did you experience someone break the discord ToS in the server?'),
    discord.SelectOption(label='Support', value='sup', description='do you need support?'))),
    name: discord.Member):
    if reporttype is discord.SelectOption(value='hack'):
        interaction.response('successfull')

im using this imports:

import discord
import discord.app_commands
import discord.ext
from datetime import timedelta, datetime

this is the variable for tree:

tree = discord.app_commands.CommandTree(client)

i already tried this:

@tree.command(name='test', description='test', guild=discord.Object(12345678))
async def slash_command(interaction: discord.Interaction, reporttype: discord.SelectMenu.options(
    discord.SelectOption(label='Hacking', value='hack', description='did you experience someone hacking?'), 
    discord.SelectOption(label='Harassment', value='harass', description='did someone harass you?'),
    discord.SelectOption(label='Discord ToS', value='tos', description='did you experience someone break the discord ToS in the server?'),
    discord.SelectOption(label='Support', value='sup', description='do you need support?'))),
    name: discord.Member):
    if reporttype is discord.SelectOption(value='hack'):
        interaction.response('successfull')

but it doesnt works too

btw im kinda new in discord.py, i already asked a friend for help but he codes in hikari and lighblub and he couldnt help me


Solution

  • To send a response you must use:

    await interaction.response.send_message('successfull')
    

    I believe this is the error in your code. Additionally, I recommend that you use Views to handle user interactions. I'll show you an example:

    HACK_OPTIONS = [
       discord.SelectOption(label='Hacking', value='hack', description='did you experience someone hacking?'),
       discord.SelectOption(label='Harassment', value='harass', description='did someone harass you?'),
       discord.SelectOption(label='Discord ToS', value='tos', description='did you experience someone break the discord ToS in the server?'),
       discord.SelectOption(label='Support', value='sup', description='do you need support?')
    ]
    
    class HackView(discord.ui.View)
       def __init__(self):
          super().__init__(timeout=100)
       
       @discord.ui.select(placeholder="Please select an option", options=HACK_OPTIONS, max_values=1)
       async def reply_select(interaction: discord.Interaction, select: discord.ui.Select):
          value = select.values[0]
          if value == 'hack':
             await interaction.responde.send_message("You selected hack!")
          else:
             await interaction.responde.send_message("You didn't select hack.")
    

    With the view prepared, all you need to do is send it as a response to your command:

    @tree.command(name='test', description='test', guild=discord.Object(12345678))
    async def test(interaction: discord.Interaction):
       await interaction.response.send_message("I'm here to help you", view=HackView())
    

    Note: i haven't tested this code, but I hope it's working as expected.