почему ошибка взаимодействия disnake кнопки?

Рейтинг: 0Ответов: 1Опубликовано: 25.02.2023
class PayConfirm(disnake.ui.View):
    def __init__(self):
        super().__init__()
        self.value: Optional[bool] = None
        
    @disnake.ui.button(label = "Перевести", style = disnake.ButtonStyle.primary)
    async def confirm(self, button: disnake.ui.Button, inter: disnake.CommandInteraction):
        print("+")
        self.value = True
            
    @disnake.ui.button(label = "Отмена", style = disnake.ButtonStyle.red)
    async def cancel(self, button: disnake.ui.Button, inter: disnake.CommandInteraction):
        print("-")
        self.value = False

минус/плюс выводит в консоль потом "Ошибка взаимодействия". если код нужен дискорд heck1r#0508

Ответы

▲ 0Принят

Выводит "Ошибка взаимодействия" т.к в вашем коде нет завершающего действия для интеграции. Достаточно любого отправленного сообщения. Вот как это реализовать:

class PayConfirm(disnake.ui.View): #кнопки для сообщения
  def __init__(self):
    super().__init__()
    self.value = Optional[bool]
    
  @disnake.ui.button(label='Перевести', style=disnake.ButtonStyle.green) #1-я кнопка; label='название кнопки'; style=disnake.ButtonStyle.цвет
  async def confirm(self, button: disnake.ui.Button, inter: disnake.CommandInteraction):
    await inter.response.defer()
    print('+')
    await inter.edit_original_response()
    self.value = True

  @disnake.ui.button(label='Отмена', style=disnake.ButtonStyle.red) #1-я кнопка; label='название кнопки'; style=disnake.ButtonStyle.цвет
  async def cancel(self, button: disnake.ui.Button, inter: disnake.CommandInteraction):
    await inter.response.defer()
    print('-')
    await inter.edit_original_response()
    self.value = False

Также заранее могу дать некоторые советы:

  • Если нужно чтобы бот что-то выводил в чат, то уберите defer и edit_original_response, и добавьте await inter.response.send_message('ваш текст'), а чтобы сделать этот текст видимый только одному человеку, внесите изменения send_message('ваш текст', ephemeral=True)
  • Чтобы кнопки работали всего 20 секунд, измените super().__init__(timeout=20)
  • Чтобы кнопки работали всего 1 раз, то для каждой кнопки после self.value = поставьте self.stop()

Вот доработанный код:

class PayConfirm(disnake.ui.View): #кнопки для сообщения
  def __init__(self):
    super().__init__(timeout=20)
    self.value = Optional[bool]
    
  @disnake.ui.button(label='Перевести', style=disnake.ButtonStyle.primary) #1-я кнопка; label='название кнопки'; style=disnake.ButtonStyle.цвет
  async def confirm(self, button: disnake.ui.Button, inter: disnake.CommandInteraction):
    print('+')
    await inter.response.send_message('Успешно', ephemeral=True)
    self.value = True
    self.stop()

  @disnake.ui.button(label='Отмена', style=disnake.ButtonStyle.red) #1-я кнопка; label='название кнопки'; style=disnake.ButtonStyle.цвет
  async def cancel(self, button: disnake.ui.Button, inter: disnake.CommandInteraction):
    print('-')
    await inter.response.send_message('Успешно', ephemeral=True)
    self.value = False
    self.stop()

Если вам помог мой ответ поставьте галочку :)