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

Discord Python Bot:实现选项点击后手动输入自定义参数值

实现自定义投票选项的Discord机器人方案

要实现你说的类似Dyno的投票功能(点击选项后手动输入自定义值,而非硬编码选项),核心是用**按钮(Buttons)+ 模态框(Modals)**组合,替代预定义的choices参数。这种方式既允许用户自定义选项,又能避免私密数据在命令输入时暴露(模态框输入仅用户和机器人交互,公屏不会显示)。

实现步骤与代码示例

以下是基于discord.py v2.x的完整实现思路:

  1. 创建带按钮的命令响应
    用户输入投票命令后,机器人发送包含操作按钮的消息,引导用户点击按钮输入自定义选项。

  2. 按钮触发模态框
    每个按钮绑定回调函数,点击后弹出模态框,让用户输入对应选项的自定义内容。

  3. 收集选项并生成投票
    收集所有用户输入的自定义选项,最终生成投票消息。

import discord
from discord import app_commands
from discord.ui import Button, View, Modal, TextInput

class VoteView(View):
    def __init__(self):
        super().__init__(timeout=60)
        self.vote_options = []

    # 按钮1:输入选项1
    @discord.ui.button(label="添加选项1", style=discord.ButtonStyle.primary)
    async def option1_button(self, interaction: discord.Interaction, button: Button):
        # 创建模态框
        modal = Modal(title="输入选项1内容")
        # 添加输入框
        option_input = TextInput(label="选项内容", placeholder="输入你的自定义选项", style=discord.TextStyle.short)
        modal.add_item(option_input)
        
        # 发送模态框并等待用户提交
        await interaction.response.send_modal(modal)
        submitted = await modal.wait()
        if submitted:
            self.vote_options.append(modal.children[0].value)
            await interaction.followup.send(f"已添加选项:{modal.children[0].value}", ephemeral=True)

    # 按钮2:输入选项2
    @discord.ui.button(label="添加选项2", style=discord.ButtonStyle.primary)
    async def option2_button(self, interaction: discord.Interaction, button: Button):
        modal = Modal(title="输入选项2内容")
        option_input = TextInput(label="选项内容", placeholder="输入你的自定义选项", style=discord.TextStyle.short)
        modal.add_item(option_input)
        
        await interaction.response.send_modal(modal)
        submitted = await modal.wait()
        if submitted:
            self.vote_options.append(modal.children[0].value)
            await interaction.followup.send(f"已添加选项:{modal.children[0].value}", ephemeral=True)

    # 确认按钮:生成投票
    @discord.ui.button(label="生成投票", style=discord.ButtonStyle.success)
    async def confirm_button(self, interaction: discord.Interaction, button: Button):
        if not self.vote_options:
            await interaction.response.send_message("请先添加至少一个选项!", ephemeral=True)
            return
        
        # 构建投票消息
        vote_msg = "📊 新投票\n"
        for idx, option in enumerate(self.vote_options, 1):
            vote_msg += f"{idx}. {option}\n"
        
        # 发送投票并添加表情反应
        msg = await interaction.channel.send(vote_msg)
        for idx in range(len(self.vote_options)):
            await msg.add_reaction(chr(0x1F1E6 + idx))  # 添加A、B、C等表情
        
        await interaction.response.send_message("投票已生成!", ephemeral=True)
        self.stop()

# 注册命令
@app_commands.command(name="create_vote", description="创建自定义选项的投票")
async def create_vote(interaction: discord.Interaction):
    view = VoteView()
    await interaction.response.send_message("点击下方按钮添加投票选项,完成后点击生成投票:", view=view)

# 确保机器人启动时同步命令
async def setup(bot):
    bot.tree.add_command(create_vote)

关键说明

  • 私密数据保护:模态框的输入内容仅用户可见(ephemeral=True的回复也仅用户能看到),不会在公屏暴露敏感信息。
  • 灵活性:你可以扩展VoteView,增加更多按钮支持更多选项,或者调整模态框的输入类型(比如长文本、数字等)。
  • 替代choices的原因:app_commands.choices是预定义的静态选项,无法支持用户自定义输入;而按钮+模态框的组合完全满足“点击选项后输入自定义值”的需求。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.24 09:18:25