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

如何在Discord按钮被使用后禁用该按钮?

实现Discord.py中Claim按钮使用后禁用的方法

要实现按钮被点击后禁用,只需在按钮的callback方法中修改按钮状态并更新对应的视图即可,具体步骤如下:

核心修改点

在按钮的回调函数中添加两行关键代码:

  1. 将当前按钮的disabled属性设为True,标记为禁用状态
  2. 通过interaction.response.edit_message更新消息中的视图,让禁用状态生效

修改后的完整代码

import discord
from discord.ui import Button, View
from datetime import datetime
from load_config import load_config
from core.utils import Utilies

class claim(discord.ui.Button):
    def __init__(self, bot):
        super().__init__(label='Claim Ticket [FAST]', style=discord.ButtonStyle.grey, emoji="⚠", custom_id='claim-button')
        self.bot = bot

    async def callback(self, interaction: discord.Interaction):
        member = interaction.user
        channel = interaction.channel
        guild = interaction.guild

        if not member.guild_permissions.manage_messages:
            await interaction.followup.send('You are not allowed to claim tickets.', ephemeral=True)
            return

        # 1. 禁用当前按钮
        self.disabled = True
        # 2. 更新消息中的视图,使禁用状态生效
        await interaction.response.edit_message(view=self.view)

        embed = discord.Embed(
            title='Claimed Ticket Successful!',
            description=f"Ticket has been claimed by {member.mention}",
            color=discord.Color.green(),
            timestamp=datetime.now()
        )
        # 发送仅用户可见的成功提示
        await interaction.followup.send(embed=embed, ephemeral=True)

关键说明

  • self.disabled = True:直接修改当前按钮的状态,标记为不可点击
  • await interaction.response.edit_message(view=self.view):必须调用此方法来更新消息中的视图,否则按钮的禁用状态不会同步到客户端
  • 使用interaction.followup.send发送成功提示:因为已经通过edit_message响应了交互,后续需要用followup来发送额外消息,ephemeral=True可以让提示仅对点击按钮的用户可见

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.18 20:40:04