這是一篇 Discord Bot 的進階教學,會使用 Python Cog 架構撰寫。
彈出對話框在許多 UI 程式開發中扮演了重要的角色,這些對話框提供一個全新的互動方式,它們不僅可以彈出提醒訊息或者確認對話框,還能彈出讓使用者輸入資料的對話框來收集資料。
在 Discord 中,開發者可以使用 Modal 工具來實現這樣的彈出對話框,為 Discord Bot 帶來更強大的互動性,以下將來學習如何在 Discord 使用 Modal 工具。
更多 Python Discord Bot 教學系列和程式範例
https://github.com/smallshawn95/Python-Discord-Bot-Course
Discord Modal 是 Discord 中用來製作彈出對話框的工具,讓使用者利用更直觀的介面,與能夠換行的性質,可以用於處理複雜的互動,來輸入需要較多文字的資料。
Discord Modal 是由「discord.ui.TextInput 文字輸入」這個 Item 元素組成,一個 Modal 中最多只能添加五個 Item 元素。
更多資訊可參考 Discord.py Modal 官方文檔。
# 繼承 discord.ui.Modal 類別,並傳入 title 參數
class ModalClass(discord.ui.Modal, title = "設定名稱"):
# 宣告一個 TextInput Item 元素
name = discord.ui.TextInput(label = "Name")
# Modal 提交後接著要執行的程式碼
async def on_submit(self, interaction: discord.Interaction):
await interaction.response.send_message(f"Hello, {self.name.value}!")
@app_commands.command(name = "modal_base", description = "Modal 基本範例")
async def modal_base(self, interaction: discord.Interaction):
# 回覆 Modal 給使用者
await interaction.response.send_modal(ModalClass())
discord.ui.TextInput(label: str, style: discord.TextStyle, placeholder: str, default: str, required: bool, min_length: int, max_length: int, custom_id: str, row: int)
send_modal()
。📢 歡迎加入我的 Discord 伺服器
https://discord.gg/Jtd3eVrFJs
Copyright © 2024 SmallShawn95. All rights reserved.