代码之家  ›  专栏  ›  技术社区  ›  Xeschoz

为discord bot制作下拉菜单时出错

  •  0
  • Xeschoz  · 技术社区  · 1 年前

    以下是我正在编写的代码。主要工作是创建一个下拉菜单,该菜单带有一个选项,可以显示机器人中所有可用的命令。

    #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))
    

    主要问题在HelpMenu类和本节中

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

    这就是我犯的错误

    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'
    

    我试着在chaggpt或youtube上找到解决方案,但没有任何效果。 所以我很感激你的帮助。非常感谢。

    1 回复  |  直到 1 年前
        1
  •  1
  •   Łukasz Kwieciński    1 年前

    回调中的参数应该是相反的:

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