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

Disnake按钮交互问题:点击后禁用原消息按钮而非发送新消息

Disnake机器人举报功能优化实现方案

核心优化思路

  • 直接编辑原举报消息,禁用被点击的对应按钮,替代发送新消息的逻辑
  • 按钮操作后发送仅操作人可见的提示消息,避免频道冗余内容
  • 存储举报者信息,操作完成后私信通知结果,同时处理无法发送私信的异常情况

修改后的完整代码

举报按钮视图类

import disnake
from disnake.ext import commands

class ReportView(disnake.ui.View):
    def __init__(self, reporter: disnake.User):
        super().__init__(timeout=None)  # 设置永久超时,保持按钮长期有效
        self.reporter = reporter  # 保存举报者对象,用于后续私信通知

    @disnake.ui.button(label="Accept", style=disnake.ButtonStyle.green)
    async def accept_button(self, button: disnake.ui.Button, interaction: disnake.Interaction):
        # 1. 禁用当前点击的按钮
        button.disabled = True
        # 2. 更新原举报消息的组件状态
        await interaction.response.edit_message(view=self)
        # 3. 向操作管理员发送操作提示(仅本人可见)
        await interaction.followup.send("举报已通过审核", ephemeral=True)
        # 4. 私信通知举报者结果
        try:
            await self.reporter.send("你的举报已被管理员通过,感谢你的反馈!")
        except disnake.Forbidden:
            # 处理用户关闭私信权限的情况
            await interaction.followup.send("无法向举报者发送私信,请提醒其开启私信权限", ephemeral=True)

    @disnake.ui.button(label="Reject", style=disnake.ButtonStyle.red)
    async def reject_button(self, button: disnake.ui.Button, interaction: disnake.Interaction):
        # 1. 禁用当前点击的按钮
        button.disabled = True
        # 2. 更新原举报消息的组件状态
        await interaction.response.edit_message(view=self)
        # 3. 向操作管理员发送操作提示
        await interaction.followup.send("举报已被驳回", ephemeral=True)
        # 4. 私信通知举报者结果
        try:
            await self.reporter.send("你的举报已被管理员驳回,如有疑问可联系服务器管理员。")
        except disnake.Forbidden:
            await interaction.followup.send("无法向举报者发送私信,请提醒其开启私信权限", ephemeral=True)

举报提交命令

@bot.slash_command(name="report", description="提交用户举报")
async def submit_report(interaction: disnake.Interaction, target_user: disnake.User, report_reason: str):
    # 替换为你的审核频道ID
    moderation_channel = bot.get_channel(123456789012345678)
    
    # 构建举报消息嵌入
    report_embed = disnake.Embed(
        title="新举报工单",
        color=disnake.Color.orange(),
        description=f"""
**举报者**: {interaction.user.mention}
**被举报者**: {target_user.mention}
**举报理由**: {report_reason}
"""
    )
    
    # 创建视图并传入举报者对象
    report_view = ReportView(reporter=interaction.user)
    # 发送举报消息到审核频道
    await moderation_channel.send(embed=report_embed, view=report_view)
    # 回复举报提交者
    await interaction.response.send_message("举报已成功提交,管理员会尽快处理你的反馈", ephemeral=True)

关键细节说明

  • timeout=None:确保按钮不会因超时失效,长期保留在举报消息上
  • interaction.response.edit_message:直接修改原消息的组件,实现禁用对应按钮的效果,无需发送新消息
  • ephemeral=True:操作提示仅对点击按钮的管理员可见,避免频道消息杂乱
  • 异常处理:针对用户关闭私信权限的情况,给出明确提示,避免机器人报错

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.26 05:41:28