Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Getting error while making a dropdown menu for discord bot

Below is the code I am writing The main work is to create a dropdown menu which proceed’s with an option to show all the available commands in the bot.

#MODULES--------------------------------------------------------------------------------------------------------------------------------------------|
import os
import time
import json
import discord
from discord.ui import Select, View
from discord.ext import commands

#GETTING DATA FROM CONFIG FILE----------------------------------------------------------------------------------------------------------------------|
def load_data(file_path):
    with open(file_path, 'r') as file:
        return json.load(file)
    
Config_Path = os.path.join(os.path.dirname(__file__), '..', 'Config', 'config.json') # File path.

Config_Data = load_data(Config_Path)

# Loading prefix.
Prefix = Config_Data['PREFIX']

#MAIN-----------------------------------------------------------------------------------------------------------------------------------------------|
class HelpMenu(View):
    @discord.ui.select(
        placeholder = "Please choose what you need help with",
        min_values = 1,
        max_values = 1,
        options = [
            discord.SelectOption(label = 'Command Help', description = 'Show info on all the available commands.', emoji = '🤖', value = 1)
        ]
    )
    async def select_callback(self, select, interaction):
        select.disabled = True

        if select.value[0] == '1':
            all_commands = self.bot.commands

            embed = discord.Embed(
                title = 'Bot Commands',
                color = discord.Color.blue()
            )
            for command in all_commands:
                command_name = [f'{Prefix}{command.name}'] + [f'!{alias}' for alias in command.aliases]
                aliases_str = ', '.join(command_name[1:]) if len(command_name) > 1 else 'None'

                embed.add_field(
                    name = command_name[0],
                    value = f'Description: {command.description}\n Aliases: {aliases_str}',
                    inline = False
                )
                await interaction.response.edit_message(embed = embed)

class Commands(commands.Cog):
    def __init__(self, bot):
        self.bot = bot

    #(1) Custom help command.
    @commands.command(
        name = 'help',
        aliases = ['Help', 'HELP'],
        description = 'Help you to know more about the bot.'
    )
    async def help(self, ctx):
        menu = HelpMenu()
        await ctx.reply(view = menu)

#ADDING THE CLASS TO COG'S--------------------------------------------------------------------------------------------------------------------------|
async def setup(bot):
    await bot.add_cog(Commands(bot))

The main issue is in the HelpMenu class and in this section

async def select_callback(self, select, interaction):
        select.disabled = True

This is the error i am geting

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

2024-03-02 18:19:54 ERROR    discord.ui.view Ignoring exception in view <HelpMenu timeout=180.0 children=1> for item <Select placeholder='Please choose what you need help with' min_values=1 max_values=1 disabled=False options=[<SelectOption label='Command Help' value=1 description='Show info on all the available commands.' emoji=<PartialEmoji animated=False name='🤖' id=None> default=False>]>
Traceback (most recent call last):
  File "C:\Users\xesch\AppData\Local\Programs\Python\Python312\Lib\site-packages\discord\ui\view.py", line 427, in _scheduled_task
    await item.callback(interaction)
  File "c:\Users\xesch\OneDrive\Desktop\Spooks\Cogs\commands.py", line 32, in select_callback
    select.disabled = True
    ^^^^^^^^^^^^^^^
AttributeError: 'Interaction' object has no attribute 'disabled'

I tried finding the solution on chatgpt or youtube but nothing is working.
So I appreciate the help. Thank you.

>Solution :

The arguments in the callback should be the other way around:

async def select_callback(self, interaction, select):
    select.disabled = True
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading