Search code examples
pythondiscorddiscord.py

Interaction failed on discord.py after some time


I have an error on discord.py Interaction failed

only dont works after some time

But when i try do /add_button works in the first 5 minutes after that dont work anymore the button

Idk why any help on my code?

I have this problem and idk how to solve that

class SuggestionModal(discord.ui.Modal, title="Envie sua Sugestão"):
    input_field = discord.ui.TextInput(
        label="Sua sugestão:",
        placeholder="Escreva aqui sua sugestão...",
        style=discord.TextStyle.paragraph,
        min_length=20,
        max_length=4000
    )

    async def on_submit(self, interaction: discord.Interaction):
        try:
            embed = discord.Embed(
                title="Nova Sugestão",
                description=self.input_field.value,
                color=discord.Color.green()
            )
            embed.set_author(name=interaction.user.display_name, icon_url=interaction.user.avatar.url)
            channel = interaction.guild.get_channel(1167254208299421760)
            await channel.send(embed=embed)
            await interaction.response.send_message("Sua sugestão foi enviada com sucesso!", ephemeral=True)
        except Exception as e:
            print(f"Erro ao enviar sugestão: {e}")
            await interaction.response.send_message("Erro ao enviar sua sugestão. Por favor, tente novamente mais tarde.", ephemeral=True)

@bot.tree.command(name='sugerir', description='Envie uma sugestão para o servidor.')
async def suggest(interaction: discord.Interaction):
    modal = SuggestionModal()
    await interaction.response.send_modal(modal)

class SuggestionButtonView(discord.ui.View):
    @discord.ui.button(label="Enviar Sugestão", style=discord.ButtonStyle.primary, custom_id="send_suggestion")
    async def button_click(self, interaction: discord.Interaction, button: discord.ui.Button):
        modal = SuggestionModal()
        await interaction.response.send_modal(modal)

async def check_roles(interaction: discord.Interaction):
    allowed_roles = ['DONO', 'ROLETA']  # Adicione os nomes dos cargos permitidos aqui
    user_roles = [role.name for role in interaction.user.roles]
    if any(role in allowed_roles for role in user_roles):
        return True
    else:
        await interaction.response.send_message("Você não tem permissão para usar este comando.", ephemeral=True)
        return False

@bot.tree.command(name='add_button', description='Adicione um botão para enviar sugestões.')
@app_commands.checks.check(check_roles)
async def add_button(interaction: discord.Interaction):
    try:
        embed = discord.Embed(
            title="Envie sua Sugestão",
            description="Clique no botão abaixo para enviar sua sugestão!\n\n Com seu feedback conseguimos melhorar em vários aspectos!",
            color=discord.Color.gold()
        )
        embed.set_author(name="Sistema de Sugestões")
        embed.set_image(url="https://cdn.discordapp.com/attachments/1142250322933334106/1168019173272715264/Monochrome_Wolf_Mascot_eSports_Gaming_Logo_6.gif?ex=662db99f&is=661b449f&hm=b4a6e15e3d1eb77e813d61abd86dfd620945af978dfc69b355de6154b6dba42e&")  # Opcional: Adicione uma imagem de thumbnail
        embed.set_footer(text="BOT Powered by Rodopoulos")

        # Crie a view com o botão
        view = SuggestionButtonView()

        # Envie a mensagem com o embed e o botão
        await interaction.response.send_message(embed=embed, view=view)
    except Exception as e:
        print(f"Erro ao adicionar botão de sugestões: {e}")
        await interaction.response.send_message("Erro ao adicionar botão de sugestões. Por favor, tente novamente mais tarde.", ephemeral=True)

To work the code but dont work


Solution

  • discord.ui.View has a timeout parameter, it's by default 180 seconds. You can pass None to it if you never want it to stop working.

    class MyView(discord.ui.View, timeout=None):
        # your buttons
    

    The buttons will however stop working after the bot restarts, if you need it to keep working after a restart, look into persistent views.