You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Discord.py:如何让View保留斜杠命令传入的参数?

解决discord.py中按钮View保留用户输入参数的问题

要让按钮对应的View保留entry参数,只需将参数传入View实例并保存为属性即可,具体修改如下:

修改enemyButtons类

让构造函数接收entry参数并保存为实例属性,按钮回调就能直接访问该参数:

class enemyButtons(discord.ui.View):
    def __init__(self, entry: int, *, timeout=180):
        super().__init__(timeout=timeout)
        self.entry = entry  # 将entry保存为实例属性

    @discord.ui.button(label="Sprites", style=discord.ButtonStyle.gray)
    async def gray_button(self, button: discord.ui.Button, interaction: discord.Interaction):
        # 通过self.entry访问保存的敌人ID
        sprite_key = enemy_configuration_table[self.entry]['Overworld Sprite']
        file = discord.File(f"SpriteGroups/{sprite_key}.png", filename="overworld_sprite.png")
        
        embed = discord.Embed(title=enemy_configuration_table[self.entry]['Name'], color=0xdb0000)
        embed.set_author(name=f"Enemy {self.entry}")
        embed.set_image(url="attachment://overworld_sprite.png")  # 修正拼写错误:attatchment → attachment
        
        # 编辑消息时传入当前View实例,保持按钮可用
        await interaction.response.edit_message(file=file, embed=embed, view=self)

修改enemy命令中的View初始化

创建enemyButtons实例时传入用户输入的entry参数:

@client.tree.command()
@app_commands.describe(
    entry="The enemy ID (0 - 230)"
)
async def enemy(interaction: discord.Interaction, entry: app_commands.Range[int, 0, 230]):
    """Look up enemy data from enemy_configuration_table.yml"""

    file = discord.File(f"BattleSprites/{entry}.png", filename="battle_sprite.png")
    
    embed = discord.Embed(title=enemy_configuration_table[entry]['Name'], color=0xdb0000)
    embed.set_author(name=f"Enemy {entry}")
    embed.set_thumbnail(url="attachment://battle_sprite.png")
    embed.add_field(name="HP", value=enemy_configuration_table[entry]['HP'], inline=True)
    embed.add_field(name="PP", value=enemy_configuration_table[entry]['PP'], inline=True)
    embed.add_field(name="Offense", value=enemy_configuration_table[entry]['Offense'], inline=True)
    embed.add_field(name="Defense", value=enemy_configuration_table[entry]['Defense'], inline=True)
    embed.add_field(name="Luck", value=enemy_configuration_table[entry]['Luck'], inline=True)
    embed.add_field(name="Guts", value=enemy_configuration_table[entry]['Guts'], inline=True)
    embed.add_field(name="Speed", value=enemy_configuration_table[entry]['Speed'], inline=True)
    embed.add_field(name="Exp.", value=enemy_configuration_table[entry]['Experience points'], inline=True)
    embed.add_field(name="Money", value=f"${enemy_configuration_table[entry]['Money']}", inline=True)
    embed.add_field(name="Level", value=enemy_configuration_table[entry]['Level'], inline=True)
    
    # 传入entry参数初始化View
    await interaction.response.send_message(file=file, embed=embed, view=enemyButtons(entry))

额外修正

原代码中attatchment为拼写错误,已改为正确的attachment,否则图片无法正常加载。

内容的提问来源于stack exchange,提问作者supremekirb

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 08:10:22