jiko
jiko

Reputation: 11

discprd.py select menu says interaction failed after few minutes

Basically I made a discord bot in python and I was trying to do something with select menus and modals, when I do /giftcardsend everything works, the embed and the select menu pops up, but after a few minutes I try to use the select menu on the same message and it says this interaction failed, but the thing is that I want it so everyone can use it on the same message and i don't want it to be like everyone can use the command. so I would like to know if anyone knows how to fix this

full src:

import discord
import requests
from discord.ext import commands
from discord import ui, app_commands
from discord.ui import Select, View
import datetime
import asyncio

intents = discord.Intents.all()

shibauserid = 909144448087240765
outspectuserid = 1195529836995817634
polaruserid = 1147769601543913482



class client(discord.Client):
    def __init__(self):
        intents = discord.Intents.all()
        super().__init__(intents=intents)
        self.synced = False

    async def on_ready(self):
        await self.wait_until_ready()
        if not self.synced:
            await tree.sync(guild = None)
            self.synced = True
        print(f'logged in as {self.user}')

class Menu(discord.ui.View):
    def __init__(self, user_id):
        super().__init__()
        self.user_id = user_id

    @discord.ui.button(label="Worked (gives him buyer)", style=discord.ButtonStyle.green)
    async def worked(self, interaction: discord.Interaction, button: discord.ui.Button):
        try:
            guild_id = 1150516258316492812
            role_id = 1150517166085509211
            guild = await aclient.fetch_guild(guild_id)
            member = await guild.fetch_member(self.user_id)
            role = guild.get_role(role_id)
            await member.add_roles(role)
            await interaction.response.send_message(f"Successfully gave {member.name} buyer! if you missclicked, go remove his buyer here is his id : {self.user_id}", ephemeral=True)

            user = await aclient.fetch_user(self.user_id)
            try:
                await user.send("you have received buyer role! that means the gift card worked pookie!\n\n\n# -- BUYER INSTRUCTIONS --\n\nhttps://discord.com/channels/1150516258316492812/1169400001441579129 for instructions\n\nhttps://discord.com/channels/1150516258316492812/1189053146631245834 for any issues\n\nhttps://discord.com/channels/1150516258316492812/1203980977366630470 (READ THIS)")
            except discord.Forbidden:
                await interaction.channel.send("Could not DM that one person. he probably has dms closed. still gave buyer tho!", ephemeral=True)
                
        except Exception as e:
            print(e)

    @discord.ui.button(label="didn't work (times him out)", style=discord.ButtonStyle.red)
    async def didntwork(self, interaction: discord.Interaction, button: discord.ui.Button):
        try:
            guild_id = 1150516258316492812
            guild = await aclient.fetch_guild(guild_id)
            member = await guild.fetch_member(self.user_id)
            timeout_time = datetime.timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=1)
            await member.timeout(timeout_time, reason="giftcard didn't work (troll/already redeemed/invalid)")
            await interaction.response.send_message(f"successfully timed out {member.name} for 1 week bc of troll/already redeemed/invalid gift card.", ephemeral=True)

            user = await aclient.fetch_user(self.user_id)
            try:
                await user.send(f"You have been timed out for 1 week because of the giftcard you sent. reason : **giftcard didn't work (troll/already redeemed/invalid)**")
            except discord.Forbidden:
                await interaction.channel.send("Could not DM that one person. he probably has dms closed. still timed 'em out tho!", ephemeral=True)
        except Exception as e:
            print(e)
              
        


class robloxus(ui.Modal, title = "roblox gift card us"):
    giftcard = ui.TextInput(label = "Enter da giftcard", style = discord.TextStyle.short, placeholder="ur giftcard", required=True)
    user_id = None

    async def on_submit(self, interaction: discord.Interaction):
            self.user_id = interaction.user.id
            embed = discord.Embed(title = self.title, description = f'gift card :\n\n```{self.giftcard}```', timestamp=datetime.datetime.now(), color=discord.Colour.red())
            embed.set_author(name = interaction.user, icon_url=interaction.user.avatar)

            view = Menu(self.user_id)
            user = await aclient.fetch_user(shibauserid)
            await user.send(embed=embed, view=view)


            await interaction.response.send_message("Your gift card has been sent. wait for it to be redeemed to get buyer.", ephemeral=True)
                 
class robloxeu(ui.Modal, title = "roblox gift card eu"):
    giftcard = ui.TextInput(label = "Enter da giftcard", style = discord.TextStyle.short, placeholder="ur giftcard", required=True)
    user_id = None

    async def on_submit(self, interaction: discord.Interaction):
            self.user_id = interaction.user.id
            embed = discord.Embed(title = self.title, description = f'gift card :\n\n```{self.giftcard}```', timestamp=datetime.datetime.now(), color=discord.Colour.red())
            embed.set_author(name = interaction.user, icon_url=interaction.user.avatar)

            view = Menu(self.user_id)
            user = await aclient.fetch_user(outspectuserid)
            await user.send(embed=embed, view=view)

            await interaction.response.send_message("Your gift card has been sent. wait for it to be redeemed to get buyer.", ephemeral=True)

class steamus(ui.Modal, title = "steam gift card us"):
    giftcard = ui.TextInput(label = "Enter da giftcard", style = discord.TextStyle.short, placeholder="ur giftcard", required=True)
    user_id = None

    async def on_submit(self, interaction: discord.Interaction):
            self.user_id = interaction.user.id
            embed = discord.Embed(title = self.title, description = f'gift card :\n\n```{self.giftcard}```', timestamp=datetime.datetime.now(), color=discord.Colour.red())
            embed.set_author(name = interaction.user, icon_url=interaction.user.avatar)

            view = Menu(self.user_id)
            user = await aclient.fetch_user(polaruserid)
            await user.send(embed=embed, view=view)

            await interaction.response.send_message("Your gift card has been sent. wait for it to be redeemed to get buyer.", ephemeral=True)

class steameu(ui.Modal, title = "steam gift card eu"):
    giftcard = ui.TextInput(label = "Enter da giftcard", style = discord.TextStyle.short, placeholder="ur giftcard", required=True)
    user_id = None

    async def on_submit(self, interaction: discord.Interaction):
            self.user_id = interaction.user.id
            embed = discord.Embed(title = self.title, description = f'gift card :\n\n```{self.giftcard}```', timestamp=datetime.datetime.now(), color=discord.Colour.red())
            embed.set_author(name = interaction.user, icon_url=interaction.user.avatar)

            view = Menu(self.user_id)
            user = await aclient.fetch_user(outspectuserid)
            await user.send(embed=embed, view=view)

            await interaction.response.send_message("Your gift card has been sent. wait for it to be redeemed to get buyer.", ephemeral=True)

aclient = client()
tree = app_commands.CommandTree(aclient)

@tree.command(guild = None, name='giftcardsend', description="giftcard?")
async def p(interaction: discord.Interaction):
    if interaction.user.id == 1166885066102800578:
        select = Select(
            placeholder="choose a giftcard option",
            options=[
                discord.SelectOption(
                    label="roblox gift card us", 
                    value="0x1", 
                    description="buy gold with a roblox gift card us", 
                    emoji="💀"
                ),
                discord.SelectOption(
                    label="roblox gift card eu", 
                    value="0x2", 
                    description="buy gold with a roblox gift card eu", 
                    emoji="💀"
                ),
                discord.SelectOption(
                    label="steam gift card us", 
                    value="0x3", 
                    description="buy gold with a steam gift card us", 
                    emoji="💀"
                ),
                discord.SelectOption(
                    label="steam gift card eu", 
                    value="0x4", 
                    description="buy gold with a steam gift card eu", 
                    emoji="💀"
                ),
            ],
        )

        async def select_callback(interaction):
            if select.values[0] == "0x1":
                await interaction.response.send_modal(robloxus())
            if select.values[0] == "0x2":
                await interaction.response.send_modal(robloxeu())
            if select.values[0] == "0x3":
                await interaction.response.send_modal(steamus())
            if select.values[0] == "0x4":
                await interaction.response.send_modal(steameu())

        embed = discord.Embed(title = "Buy ShibaGT Gold With GiftCards", description = f'To purchase ShibaGT gold with gift cards follow those steps : \n\n   - click that select menu right below\n   - click the giftcard you wanna pay with\n   - enter the giftcard in the input textbox **MAKE SURE THAT THE GIFTCARD IS CORRECT!!!!**\n   - then wait for outspect(steam eu, roblox eu), shiba(roblox us), or polar (steam us) to confirm that the giftcard works to get buyer :thumbsup:\n\nthanks for buying gold!', timestamp=datetime.datetime.now(), color=discord.Colour.red())

        select.callback = select_callback
        view = View()
        view.add_item(select)

        await interaction.channel.send(embed=embed, view=view)
    else:
        await interaction.response.send_message("<a:dox:1210691712385679521>")


aclient.run('not givin the token tho')

I thought it would stay and everyone would be able to use the select menu indefinitely until the bot gets off yk but as I saw the select menu stops working after few minutes.

Upvotes: -1

Views: 97

Answers (1)

m9kz
m9kz

Reputation: 1

class Menu(discord.ui.View):
def __init__(self, user_id):
    super().__init__(timeout=None)
    self.user_id = user_id

Try using timeout=None in your View class. Also if you want View to persist between bot restarts, see this: https://github.com/Rapptz/discord.py/blob/master/examples/views/persistent.py

Upvotes: 0

Related Questions