“此交互失败”带有discord.py stoper的Discord机器人

问题描述 投票:0回答:1

机器人将发送一个包含 1 个绿色按钮的徽章 点击此按钮后,用户在Discord服务器上的昵称将显示在徽章上,并且旁边会有一个计时器。 像这样in photo

import discord
from discord.ext import commands, tasks
from discord.ui import Button, View

bot = commands.Bot(command_prefix='?', intents=discord.Intents.all())
bot.remove_command('help')


@bot.event
async def on_ready():
  print(f'Logged in as {bot.user.name} ({bot.user.id})')
  print('------')


@bot.command()
async def run_bot(ctx):
  embed = discord.Embed(title="◣💻**Tablet Ochrony**💻◢",
                        description="Click Start to begin the timer",
                        color=0b000000)
  embed.add_field(name="🧢Imię i Nazwisko |⏳Na służbie:",
                  value="None",
                  inline=False)
  embed.add_field(name="Time Elapsed:", value="0 seconds", inline=False)
  start_button = Button(style=discord.ButtonStyle.green,
                        label="Status 1",
                        custom_id="Status 1")
  stop_button = Button(style=discord.ButtonStyle.red,
                       label="Status 3",
                       custom_id="Status 3")

  view = View()
  view.add_item(start_button)
  view.add_item(stop_button)

  message = await ctx.send(embed=embed, view=view)

  interaction = await bot.wait_for(
      "button_click",
      check=lambda i: i.user.id == ctx.author.id and i.message.id == message.id
  )

  if interaction.custom_id == "start_button":
    nickname = ctx.author.display_name
    embed.set_field_at(0, name="Nickname:", value=nickname, inline=False)
    await interaction.response.edit_message(embed=embed)
  elif interaction.custom_id == "stop_button":
    await interaction.response.edit_message(content="Timer stopped.")


bot.run('token')

after i click any button discord failed popping out.

python discord discord.py discord-buttons
1个回答
0
投票

执行交互检查的方式非常低效,您应该使用

discord.ui
模块来执行相同的操作(docs),这里有一个相同的示例:

from discord.ext import commands

import discord


class Bot(commands.Bot):
    def __init__(self):
        intents = discord.Intents.default()
        intents.message_content = True

        super().__init__(command_prefix=commands.when_mentioned_or('$'), intents=intents)

    async def on_ready(self):
        print(f'Logged in as {self.user} (ID: {self.user.id})')
        print('------')


# Define a simple View that gives us a confirmation menu
class Confirm(discord.ui.View):
    def __init__(self):
        super().__init__()
        self.value = None

    # When the confirm button is pressed, set the inner value to `True` and
    # stop the View from listening to more input.
    # We also send the user an ephemeral message that we're confirming their choice.
    @discord.ui.button(label='Confirm', style=discord.ButtonStyle.green)
    async def confirm(self, interaction: discord.Interaction, button: discord.ui.Button):
        await interaction.response.send_message('Confirming', ephemeral=True)
        self.value = True
        self.stop()

    # This one is similar to the confirmation button except sets the inner value to `False`
    @discord.ui.button(label='Cancel', style=discord.ButtonStyle.grey)
    async def cancel(self, interaction: discord.Interaction, button: discord.ui.Button):
        await interaction.response.send_message('Cancelling', ephemeral=True)
        self.value = False
        self.stop()


bot = Bot()


@bot.command()
async def ask(ctx: commands.Context):
    """Asks the user a question to confirm something."""
    # We create the view and assign it to a variable so we can wait for it later.
    view = Confirm()
    await ctx.send('Do you want to continue?', view=view)
    # Wait for the View to stop listening for input...
    await view.wait()
    if view.value is None:
        print('Timed out...')
    elif view.value:
        print('Confirmed...')
    else:
        print('Cancelled...')


bot.run('token')
© www.soinside.com 2019 - 2024. All rights reserved.