Disnake/discord.py indefinitely await reponse

Question:

When I create a button and handle the callback or send a message and await a reaction in discord with my python bot, this seems to be limited in time. Sometimes after ~ 1hour, the bot doesn’t register reactions anymore. For sure once I restart the bot, the connection is lost and it won’t register the interaction anymore.

However, I have seen bots in discord that always react to a button, no matter how long ago that button was created. Is there a way to achieve this? Do I have to periodically "reconnect" the bot to the buttons it created?

Simple example:

class ButtonView(disnake.ui.View):
    def __init__(self):
        super().__init__(timeout=None)

    @disnake.ui.button(label="Hi", style=ButtonStyle.red)
    async def first_button(
        self, button: disnake.ui.Button, interaction: disnake.MessageInteraction
    ):
        await interaction.response.send_message("Button clicked.")

class Test(commands.Cog):
    def __init__(self, bot: commands.Bot):
        self.bot = bot
       
    @commands.slash_command() 
    async def test(self, inter):
        await inter.send("Button!", view=ButtonView())

-> In this example the bot won’t react to the button click anymore after some time has passed or I restarted the bot.

Asked By: Nik

||

Answers:

You can do like this :

import disnake
from disnake.ext import commands


# Define a simple View that persists between bot restarts
# In order a view to persist between restarts it needs to meet the following conditions:
# 1) The timeout of the View has to be set to None
# 2) Every item in the View has to have a custom_id set
# It is recommended that the custom_id be sufficiently unique to
# prevent conflicts with other buttons the bot sends.
# For this example the custom_id is prefixed with the name of the bot.
# Note that custom_ids can only be up to 100 characters long.
class PersistentView(disnake.ui.View):
    def __init__(self):
        super().__init__(timeout=None)

    @disnake.ui.button(
        label="Green", style=disnake.ButtonStyle.green, custom_id="persistent_view:green"
    )
    async def green(self, button: disnake.ui.Button, interaction: disnake.MessageInteraction):
        await interaction.response.send_message("This is green.", ephemeral=True)

    @disnake.ui.button(label="Red", style=disnake.ButtonStyle.red, custom_id="persistent_view:red")
    async def red(self, button: disnake.ui.Button, interaction: disnake.MessageInteraction):
        await interaction.response.send_message("This is red.", ephemeral=True)

    @disnake.ui.button(
        label="Grey", style=disnake.ButtonStyle.grey, custom_id="persistent_view:grey"
    )
    async def grey(self, button: disnake.ui.Button, interaction: disnake.MessageInteraction):
        await interaction.response.send_message("This is grey.", ephemeral=True)


class PersistentViewBot(commands.Bot):
    def __init__(self):
        super().__init__(command_prefix=commands.when_mentioned)
        self.persistent_views_added = False

    async def on_ready(self):
        if not self.persistent_views_added:
            # Register the persistent view for listening here.
            # Note that this does not send the view to any message.
            # In order to do this you need to first send a message with the View, which is shown below.
            # If you have the message_id you can also pass it as a keyword argument, but for this example
            # we don't have one.
            self.add_view(PersistentView())
            self.persistent_views_added = True

        print(f"Logged in as {self.user} (ID: {self.user.id})")
        print("------")


bot = PersistentViewBot()


@bot.command()
@commands.is_owner()
async def prepare(ctx: commands.Context):
    """Starts a persistent view."""
    # In order for a persistent view to be listened to, it needs to be sent to an actual message.
    # Call this method once just to store it somewhere.
    # In a more complicated program you might fetch the message_id from a database for use later.
    # However this is outside of the scope of this simple example.
    await ctx.send("What's your favourite colour?", view=PersistentView())


bot.run("token")

This code comes from disnake repository

Answered By: crazycat256

I think it’s most likely that your internet has a connection issue, and that you’re disconnecting without noticing.

That was happening to me, so I added on_disconnect and on_resumed bot events that were just simple print statements so that I would be able to check on if that was the source of the issue.

bot: commands.Bot = commands.Bot(command_prefix='.', intents=intents)
bot.time = time.time()

@bot.event
async def on_ready():
    print(f"Logged in as {bot.user} (ID: {bot.user.id})")
    print('TesterBot is ready starting at ' + time.ctime(bot.time))

@bot.event
async def on_disconnect():
    uptimedelta = time.time() - bot.time
    print('TesterBot is disconnected at ' + time.ctime(bot.time) + '. Testerbot has been up for ' + str(datetime.timedelta(seconds=uptimedelta)))

@bot.event
async def on_resumed():
    uptimedelta = time.time() - bot.time
    print("TesterBot reconnected " + time.ctime(bot.time) + '. Testerbot has been up for ' + str(datetime.timedelta(seconds=uptimedelta)))

@bot.event
async def on_connect():
    print('TesterBot is connected starting at ' + time.ctime(time.time()))

Just basic stuff like that helped reveal that the problem was that my machine was dropping the connection. It was a physical issue with my internet, not a coding error or misunderstanding of the api or library.

Answered By: Jeffrey DeLucca

await inter.response.defer(enter image description here)

Answered By: SHENID
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.