【问题标题】:How can I make a modal like in the picture - discord我怎样才能像图片中那样制作模态 - 不和谐
【发布时间】:2022-12-18 18:52:24
【问题描述】:

我想在按下按钮时创建一个如图所示的模态。如何使用 Discord.py 或 nextcord 制作此模型?

【问题讨论】:

  • Modal 目前在 Nextcord 中不可用。你可以尝试使用pycord
  • Modal 将在 nextcord v2.0.0a10 中可用

标签: python discord discord.py nextcord


【解决方案1】:

您可以在此处找到 Nextcord 中模态的示例:

https://github.com/nextcord/nextcord/blob/master/examples/modals/

class Pet(nextcord.ui.Modal):
    def __init__(self):
        super().__init__("Your pet")  # Modal title

        # Create a text input and add it to the modal
        self.name = nextcord.ui.TextInput(
            label="Your pet's name",
            min_length=2,
            max_length=50,
        )
        self.add_item(self.name)

        # Create a long text input and add it to the modal
        self.description = nextcord.ui.TextInput(
            label="Description",
            style=nextcord.TextInputStyle.paragraph,
            placeholder="Information that can help us recognise your pet",
            required=False,
            max_length=1800,
        )
        self.add_item(self.description)

    async def callback(self, interaction: nextcord.Interaction) -> None:
        """This is the function that gets called when the submit button is pressed"""
        response = f"{interaction.user.mention}'s favourite pet's name is {self.name.value}."
        if self.description.value != "":
            response += f"
Their pet can be recognized by this information:
{self.description.value}"
        await interaction.send(response)

@bot.slash_command(
    name="pet",
    description="Describe your favourite pet",
    guild_ids=[TESTING_GUILD_ID],
)
async def send(interaction: nextcord.Interaction):
    # sending the modal on an interaction (can be slash, buttons, or select menus)
    modal = Pet()
    await interaction.response.send_modal(modal)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-10
    • 2023-03-23
    • 1970-01-01
    • 2020-01-19
    • 1970-01-01
    • 2020-04-12
    • 1970-01-01
    相关资源
    最近更新 更多