0

I am currently looking for a way to auto reset the embed in a Discord python bot. This is my code. It works fine the very first time a user clicks on a category. The second time it won't refresh anymore. How can I fix that?

class AutoResetView(discord.ui.View): def __init__(self, initial_embed: discord.Embed, reset_time: int = 300): super().__init__(timeout=None) self.message = None self.initial_embed = initial_embed self.reset_time = reset_time self._reset_task = None async def on_timeout(self): await self.reset_to_initial_embed() async def reset_to_initial_embed(self): if self.message: await self.message.edit(embed=self.initial_embed, view=self) async def start_auto_reset(self): await asyncio.sleep(self.reset_time) await self.reset_to_initial_embed() def set_message(self, message): self.message = message async def on_select(self, interaction: discord.Interaction): print("Auswahl getroffen!") await self.reset_to_initial_embed() await self.start_auto_reset() class SummonView(AutoResetView): def __init__(self): initial_embed = discord.Embed(title="TITLE", description="DESCRIPTION", color=COLOR) super().__init__(initial_embed) self.add_item(SummonDropdown()) async def send_view(self, channel: discord.TextChannel): self.message = await channel.send(embed=self.initial_embed, view=self) self.set_message(self.message) await self.start_auto_reset() @bot.command() async def send_summon(ctx): summon_view = SummonView() await summon_view.send_view(ctx.channel) 

1 Answer 1

0

try creating a SummonDropdown class that inherits from discord.ui.Select and implement the callback method to call reset_to_initial_embed and start_auto_reset. This ensures the embed is properly updated when interacting with the dropdown :)

import discord from discord.ext import commands import asyncio intents = discord.Intents.default() intents.message_content = True bot = commands.Bot(command_prefix="!", intents=intents) class SummonDropdown(discord.ui.Select): def __init__(self): options = [ discord.SelectOption(label="Option 1", value="1"), discord.SelectOption(label="Option 2", value="2"), discord.SelectOption(label="Option 3", value="3"), ] super().__init__(placeholder="Choose an option...", options=options) async def callback(self, interaction: discord.Interaction): await self.view.reset_to_initial_embed() await self.view.start_auto_reset() class AutoResetView(discord.ui.View): def __init__(self, initial_embed: discord.Embed, reset_time: int = 300): super().__init__(timeout=None) self.message = None self.initial_embed = initial_embed self.reset_time = reset_time self._reset_task = None async def on_timeout(self): await self.reset_to_initial_embed() async def reset_to_initial_embed(self): if self.message: await self.message.edit(embed=self.initial_embed, view=self) async def start_auto_reset(self): await asyncio.sleep(self.reset_time) await self.reset_to_initial_embed() def set_message(self, message): self.message = message class SummonView(AutoResetView): def __init__(self): initial_embed = discord.Embed(title="TITLE", description="DESCRIPTION", color=0x00ff00) super().__init__(initial_embed) self.add_item(SummonDropdown()) async def send_view(self, channel: discord.TextChannel): self.message = await channel.send(embed=self.initial_embed, view=self) self.set_message(self.message) await self.start_auto_reset() @bot.command() async def send_summon(ctx): summon_view = SummonView() await summon_view.send_view(ctx.channel) bot.run('TOKEn') 
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your help! So I tried this version but it said interaction failed when clicking on the dropdown. Did I miss something?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.