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

如何向discord.ui.Modal子类传入自定义参数?

问题描述

我原本有一个运行正常的discord.ui.Modal子类:

class Report_SurveyModal_NoRace(discord.ui.Modal, title='KRF1 Report'):
    was = discord.ui.TextInput(label='Describe what happened', style=discord.TextStyle.paragraph, max_length=1000)
    media = discord.ui.TextInput(label='Media', style=discord.TextStyle.paragraph, max_length=500, placeholder="blabalblablab", required=False)

    async def on_submit(self, interaction: discord.Interaction):
        runde = ""
        await report_modal_submit_button(interaction, runde, self.was, self.media)

现在需要给这个类传入一个标志变量steward_flag(值为1或0),于是我重写了__init__方法:

class Report_SurveyModal_NoRace(discord.ui.Modal, title='KRF1 Report'):
    def __init__(self, steward_flag):
        self.steward_flag = steward_flag
    was = discord.ui.TextInput(label='Describe what happened', style=discord.TextStyle.paragraph, max_length=1000)
    media = discord.ui.TextInput(label='Media', style=discord.TextStyle.paragraph, max_length=500, placeholder="blabalblablab", required=False)

    async def on_submit(self, interaction: discord.Interaction):
        runde = ""
        await report_modal_submit_button(interaction, runde, self.was, self.media, self.steward_flag)

修改后出现了以下错误:

Traceback (most recent call last):
  File "C:\Users\Marcel\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\ui\view.py", line 427, in _scheduled_task
    await item.callback(interaction)
  File "d:\Daten\Programming\PYTHON\00_bots\07_krfbot\Bot\cogs\servicecenter.py", line 792, in callback
    await report_check_for_part_two(interaction, self.steward_flag)
  File "d:\Daten\Programming\PYTHON\00_bots\07_krfbot\Bot\cogs\servicecenter.py", line 658, in report_check_for_part_two
    await interaction.response.send_modal(Report_SurveyModal_NoRace(steward_flag))
  File "C:\Users\Marcel\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\interactions.py", line 971, in send_modal
    params = interaction_response_params(InteractionResponseType.modal.value, modal.to_dict())
  File "C:\Users\Marcel\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\ui\modal.py", line 203, in to_dict
    'custom_id': self.custom_id,
AttributeError: 'Report_SurveyModal_NoRace' object has no attribute 'custom_id'

我确定这个错误来自discord.ui.Modal内部,因为我的类里没定义custom_id,推测是重写__init__方法导致的问题。我刚接触面向对象编程,想知道解决办法。

解决方法

你重写__init__方法时,没有调用父类discord.ui.Modal的构造方法,导致父类负责初始化的custom_id等关键属性没有被创建,所以才会抛出这个错误。

正确的做法是在自定义的__init__方法里,先调用父类的__init__,再初始化自己的变量。同时调整模态框标题的传递方式,保持代码规范:

class Report_SurveyModal_NoRace(discord.ui.Modal):
    def __init__(self, steward_flag):
        # 调用父类构造方法,传入标题参数
        super().__init__(title='KRF1 Report')
        self.steward_flag = steward_flag

    was = discord.ui.TextInput(label='Describe what happened', style=discord.TextStyle.paragraph, max_length=1000)
    media = discord.ui.TextInput(label='Media', style=discord.TextStyle.paragraph, max_length=500, placeholder="blabalblablab", required=False)

    async def on_submit(self, interaction: discord.Interaction):
        runde = ""
        await report_modal_submit_button(interaction, runde, self.was, self.media, self.steward_flag)

关键说明

  • super().__init__(title='KRF1 Report'):这行代码会触发父类discord.ui.Modal的构造逻辑,自动完成custom_id等内部属性的初始化,同时设置模态框标题。
  • 不要直接在类继承时写title='KRF1 Report',这种旧版快捷方式在重写__init__后会失效,必须通过父类初始化来传递标题参数。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.04 13:21:36