I’m working on a Discord bot that uses a select menu to display help commands. The bot should show all available commands when users pick an option from the dropdown.
import os
import discord
from discord.ui import Select, View
from discord.ext import commands
import json
def get_config(path):
with open(path, 'r') as f:
return json.load(f)
config_file = os.path.join(os.path.dirname(__file__), '..', 'settings', 'bot_config.json')
bot_config = get_config(config_file)
bot_prefix = bot_config['BOT_PREFIX']
class CommandsMenu(View):
@discord.ui.select(
placeholder="Select what you need assistance with",
min_values=1,
max_values=1,
options=[
discord.SelectOption(
label='Available Commands',
description='Display all bot commands and their usage.',
emoji='📋',
value=1
)
]
)
async def menu_callback(self, selection, interaction_obj):
selection.disabled = True
if selection.value[0] == '1':
bot_commands = self.bot.commands
help_embed = discord.Embed(
title='Available Bot Commands',
color=discord.Color.green()
)
for cmd in bot_commands:
cmd_variants = [f'{bot_prefix}{cmd.name}'] + [f'!{alt}' for alt in cmd.aliases]
alias_list = ', '.join(cmd_variants[1:]) if len(cmd_variants) > 1 else 'No aliases'
help_embed.add_field(
name=cmd_variants[0],
value=f'Info: {cmd.description}\nAliases: {alias_list}',
inline=False
)
await interaction_obj.response.edit_message(embed=help_embed)
class BotCommands(commands.Cog):
def __init__(self, client):
self.bot = client
@commands.command(
name='help',
aliases=['assistance', 'commands'],
description='Shows information about bot functionality.'
)
async def show_help(self, context):
help_menu = CommandsMenu()
await context.reply(view=help_menu)
async def setup(client):
await client.add_cog(BotCommands(client))
I keep getting this error when someone clicks the dropdown:
AttributeError: 'Interaction' object has no attribute 'disabled'
Traceback (most recent call last):
File "discord/ui/view.py", line 427, in _scheduled_task
await item.callback(interaction)
File "bot_commands.py", line 28, in menu_callback
selection.disabled = True
The error happens in the callback function where I try to disable the select menu. I’ve looked through Discord.py docs but can’t figure out what I’m doing wrong. Any ideas on how to fix this?