Mr Wing
Mr Wing

Reputation: 13

Can't change timeout disnake.py

I write code in disnake.py. And I got a problem. The default timeout is 180, but I can't change it in any way. How do I get the select menu to work even after a bot restart? I will be very grateful

Code:

import disnake
from disnake.ext import commands
from disnake import TextInputStyle, Button, ButtonStyle, ActionRow
 

class Modal(disnake.ui.Modal):
    def __init__(self):
        components = [
            disnake.ui.TextInput(
                label="ПОЧЕМУ ВЫ ХОТИТЕ ВСТУПИТЬ?",
                placeholder="Кратко опишите",
                custom_id="Почему вы хотите вступить?",
                style=TextInputStyle.short,
                min_length=3,
                max_length=50,
            ),
            disnake.ui.TextInput(
                label="Что умеете?",
                placeholder="Был ли опыт, и т.д.",
                custom_id="Описание",
                style=TextInputStyle.short,
                max_length=2000,
            ),
            disnake.ui.TextInput(
                label="Почему именно вы?",
                placeholder="Расскажите о себе. пол/возраст/город",
                custom_id="Почему именно вы?",
                style=TextInputStyle.short,
                max_length=2000,
            ),            
        ]
        super().__init__(
            title="Подача заявки: Модератор",
            custom_id="create_tag",
            components=components,
        )

    async def callback(self, inter: disnake.ModalInteraction):
        embed = disnake.Embed(title="`Благодарим за подачу заявки на модератора! вскоре ее рассмотрят, и ответят вам в ЛС.`", color =0x00FF9A )
        embed.set_author(name = f"{inter.author.name}" ,icon_url= f"{inter.author.avatar}")
        for key, value in inter.text_values.items():
            embed.add_field(
                name=key.capitalize(),
                value=value[:1024],
                inline=False,
            )
        channel = inter.client.get_channel(938413397597372437)
        await inter.response.send_message(embed=embed, ephemeral=True)
        await channel.send(embed=embed)

Here I set the modal window that will pop up when the button is clicked.

class Modal2(disnake.ui.Modal):
    def __init__(self):
        components = [
            disnake.ui.TextInput(
                label="ПОЧЕМУ ВЫ ХОТИТЕ ВСТУПИТЬ?",
                placeholder="Кратко опишите",
                custom_id="Почему вы хотите вступить?",
                style=TextInputStyle.short,
                min_length=3,
                max_length=50,
            ),
            disnake.ui.TextInput(
                label="Что умеете?",
                placeholder="Был ли опыт, и т.д.",
                custom_id="Описание",
                style=TextInputStyle.short,
                max_length=2000,
            ),
            disnake.ui.TextInput(
                label="Почему именно вы?",
                placeholder="Расскажите о себе. пол/возраст/город",
                custom_id="Почему именно вы?",
                style=TextInputStyle.short,
                max_length=2000,
            ),            
        ]
        super().__init__(
            title="Подача заявки: Саппорт",
            custom_id="create_tag",
            components=components,
        )

    async def callback(self, inter: disnake.ModalInteraction):
        embed = disnake.Embed(title="`Благодарим за подачу заявки на саппорт! вскоре ее рассмотрят, и ответят вам в ЛС.`", color =0x00FF9A )
        embed.set_author(name = f"{inter.author.name}" ,icon_url= f"{inter.author.avatar}")
        for key, value in inter.text_values.items():
            embed.add_field(
                name=key.capitalize(),
                value=value[:1024],
                inline=False,
            )
        channel = inter.client.get_channel(938413397597372437)
        await inter.response.send_message(embed=embed, ephemeral=True)
        await channel.send(embed=embed)

I set buttons

class MyView(disnake.ui.View):
    @disnake.ui.button(label="Подать заявку", style=disnake.ButtonStyle.gray, emoji="😎") 
    async def button_callback(self,button,inter: disnake.ApplicationCommandInteraction):
        await inter.response.send_modal(modal=Modal())
        view = disnake.ui.View(timeout=None)
        view.add_item(MyView)

class MyView2(disnake.ui.View):
    @disnake.ui.button(label="Подать заявку", style=disnake.ButtonStyle.gray, emoji="😎") 
    async def button_callback(self,button,inter: disnake.ApplicationCommandInteraction):
        await inter.response.send_modal(modal=Modal2())
        view = disnake.ui.View(timeout=None)
        view.add_item(MyView2)        

I set SelectMenu

class Dropdown(disnake.ui.Select):
    def __init__(self):
        options = [
            disnake.SelectOption(
                label="Саппорт", description="Заявка на саппорт", emoji="🟥"
            ),
            disnake.SelectOption(
                label="Модератор", description="Заявка на модератора", emoji="🟩"
            )
        ]
        super().__init__(placeholder="Выберете должность",max_values=1,min_values=1,options=options)
    async def callback(self, interaction: disnake.Interaction):
        if interaction.values[0] == "Саппорт":
            await interaction.response.send_message(embed = disnake.Embed(title='Саппорт',description=f"Саппорт: обязанности, помогать жить.", color=0x0000), view=MyView2(), ephemeral=True)
        if interaction.values[0] == "Модератор":
          await interaction.response.send_message(embed = disnake.Embed(title='Модерация',description=f"Модератор: обязанности, жить.", color=0x0000), view=MyView(), ephemeral=True)


class DropdownView(disnake.ui.View):
    def __init__(self):
        super().__init__()
        view = disnake.ui.View(timeout=None)
        self.add_item(Dropdown())
        view.add_item(Dropdown())

class ModalCog(commands.Cog):
  def __init__(self, bot):
    self.bot = bot

The menu itself, which I want to loop forever.

  @commands.slash_command()
  async def send_menu(self, interaction: disnake.ApplicationCommandInteraction):
      '''send modal'''
      await interaction.response.defer()
      z = await interaction.original_message()
      await z.delete()
      await interaction.channel.send(embed = disnake.Embed(title='Заявки в персонал', description=f"Чтобы подать заявку, и стать частью нашей семьи, выбери подходящий вариант в списке.", color=0x0000), view=DropdownView())

def setup(bot):
    bot.add_cog(ModalCog(bot))
    print(f"{__name__} готов к работе")

It took me all day to write this code. I'm not good with disnake.interaction and those things.

Upvotes: 0

Views: 531

Answers (1)

user21208507
user21208507

Reputation: 1

To add a timeout for a modal, you should add the timeout keyword argument to the super.__init__() part. You need to pass a whole number. That is the amount of seconds it will timeout. If you set it as None, it will default to 180. So the code for the modal is:

import disnake
from disnake.ext import commands
from disnake import TextInputStyle, Button, ButtonStyle, ActionRow
 

class Modal(disnake.ui.Modal):
    def __init__(self):
        components = [
            disnake.ui.TextInput(
                label="ПОЧЕМУ ВЫ ХОТИТЕ ВСТУПИТЬ?",
                placeholder="Кратко опишите",
                custom_id="Почему вы хотите вступить?",
                style=TextInputStyle.short,
                min_length=3,
                max_length=50,
            ),
            disnake.ui.TextInput(
                label="Что умеете?",
                placeholder="Был ли опыт, и т.д.",
                custom_id="Описание",
                style=TextInputStyle.short,
                max_length=2000,
            ),
            disnake.ui.TextInput(
                label="Почему именно вы?",
                placeholder="Расскажите о себе. пол/возраст/город",
                custom_id="Почему именно вы?",
                style=TextInputStyle.short,
                max_length=2000,
            ),            
        ]
        super().__init__(
            title="Подача заявки: Модератор",
            custom_id="create_tag",
            components=components,
        )

    async def callback(self, inter: disnake.ModalInteraction):
        embed = disnake.Embed(title="`Благодарим за подачу заявки на модератора! вскоре ее рассмотрят, и ответят вам в ЛС.`", color =0x00FF9A )
        embed.set_author(name = f"{inter.author.name}" ,icon_url= f"{inter.author.avatar}")
        for key, value in inter.text_values.items():
            embed.add_field(
                name=key.capitalize(),
                value=value[:1024],
                inline=False,
            )
        channel = inter.client.get_channel(938413397597372437)
        await inter.response.send_message(embed=embed, ephemeral=True)
        await channel.send(embed=embed)

Upvotes: 0

Related Questions