python 如何在www.example.com中单击Discord Interactive按钮后禁用它discord.py?

c0vxltue  于 2023-06-20  发布在  Python
关注(0)|答案(1)|浏览(127)

我正在为我的机器人制作一个应用程序自定义命令。成员可以通过运行斜杠命令申请版主。我做了一个斜杠命令,成员可以输入他们的名字,成为mod的原因,以及经验。一旦成员在输入所需属性后运行斜杠命令,它就会自动将其发送到版主提交通道进行审查,以嵌入的形式,带有2个可点击的交互式按钮,用于接受和拒绝。然而,一旦任何一个按钮被点击,我希望整个按钮功能被禁用,而非点击一个是灰色的,就像这样:

^单击按钮之前。

^点击“接受”按钮后。“Accept”按钮保持绿色,但“reject”按钮的颜色从红色变为灰色(类似地,单击“Reject”时,“reject”保持红色,“accept”为灰色)。但是,如果单击任何一个按钮,则这两个按钮都将不可用。
提供的图像是我想要的一个例子。下面是我的源代码:

import os
import discord
from discord import app_commands

TOKEN = ' YOUR BOT TOKEN HERE '
GUILD = ' SERVER NAME HERE '
intents = discord.Intents.default()
intents.message_content = True
client = discord.Client(intents=intents)
tree = app_commands.CommandTree(client=client)

@client.event
async def on_ready():
    for guild in client.guilds:
        if guild.name == GUILD:
            break
    print(f'{client.user} is connected to {guild.name}(id: {guild.id})')

@tree.command(name='apply', description='Apply for Moderator', guild=discord.Object(id=000000000))
async def firstcommand(interaction:discord.Interaction, name:str, reason:str, experience:str):

    submission_channel = await client.fetch_channel(00000000000000)
    emb = discord.Embed(title='Moderator App', description=f'Name: {name} \n Reason to be mod: {reason} \n Moderator Experience: {experience}')
    emb.set_author(name=f'Submitted by {interaction.user}')
    accept_button = discord.ui.Button(label="Accept", style=discord.ButtonStyle.success)
    reject_button = discord.ui.Button(label='Reject', style=discord.ButtonStyle.danger)
    view = discord.ui.View()
    view.add_item(accept_button)
    view.add_item(reject_button)

    async def accept_callback(interaction:discord.Interaction):
        await interaction.response.send_message('Application Accepted.')

    async def reject_callback(interaction:discord.Interaction):
        await interaction.response.send_message('Application Rejected.')

    accept_button.callback = accept_callback
    reject_button.callback = reject_callback

    await submission_channel.send(embed=emb, view=view)
    await interaction.response.send_message('Your Application has been submitted to the Head Admin for review. Thank you.', ephemeral=True)

client.run(TOKEN)

请告诉我怎么做。谢谢你

  • Discord.py(最新版本,v2.3.0)
  • app_commands.Commandtree()
  • discord.Client()
  • Python 3.11
5gfr0r5j

5gfr0r5j1#

设置按钮的属性、样式和禁用,然后通过编辑更新视图。

@tree.command(name='test')
async def test(interaction):
  view = discord.ui.View()
  button = discord.ui.Button(label='Press me', style=discord.ButtonStyle.red)
  async def button_callback(interaction):
    button.style = discord.ButtonStyle.grey
    button.disabled = True
    await interaction.response.edit_message(content='Pressed', view=view)
  button.callback = button_callback
  view.add_item(button)
  
  await interaction.response.send_message(content='Not pressed', view=view)

按钮通常是通过类。下面是它在类中的样子:

class MyButtonView(discord.ui.View):
  def __init__(self):
    super().__init__()
  @discord.ui.button(label='Press me', style=discord.ButtonStyle.red)
  async def button_callback(self, interaction: discord.Interaction, button: discord.ui.Button):
    self.style = discord.ButtonStyle.grey
    self.disabled = True
    await interaction.response.edit_message(content='Pressed', view=self)

@tree.command(name='test')
async def test(interaction):
  await interaction.response.send_message(content='Not pressed', view=MyButtonView())

文件:discord.ui.Button

相关问题