通过命令生成模型:
@discord.app_commands.command(name="host_game_test", description="Host a new game")
async def host_game_test(self, interaction: discord.Interaction):
modal = HostGameModal(title="Host New Game", era=GameAge.EA)
await interaction.response.send_modal(modal)
这工作得很好,创建了模式窗口。然而,填写完毕并单击“提交”后,一段延迟后出现红色错误消息 - “出现问题。请重试。”控制台中根本没有报告任何错误。
从不和谐导入交互,文本样式 从discord.ui导入TextInput,Modal
from utils import printlog
HostGameModal 类(模态): def init(self,era:str,*args,**kwargs): super().init(*args, **kwargs) self.era =era # 存储时代
self.add_item(TextInput(label="Game Name", custom_id="game_name", placeholder="Enter your game's name"))
self.add_item(TextInput(label="Max Players", custom_id="max_players", placeholder="Enter max number of players",
style=TextStyle.short))
self.add_item(TextInput(label="Password", custom_id="password", placeholder="Enter a game password",
style=TextStyle.short))
async def callback(self, interaction: Interaction):
printlog("HostGameModal callback called.")
try:
await interaction.response.defer(ephemeral=True)
game_name = self.children[0].value
max_players = int(self.children[1].value)
password = self.children[2].value
# Use self.era here for the game's era
await interaction.followup.send(f"Hosting a '{self.era}' game named '{game_name}' with max {max_players} "
f"players and password '{password}'.")
except Exception as e:
printlog(f"Error in HostGameModal callback: {e}")
await interaction.followup.send("An error occurred. Please try again later.", ephemeral=True)
这是 Modal 类。回调函数开始处的 printlog 从未被调用,因此似乎由于某种原因回调函数从未被调用。
已经被这个问题困扰了一段时间,而且我能找到的关于模态内容的文档似乎并不多。谁能看出问题出在哪里吗?