from typing import Optional import discord from discord.ext.commands import Bot class BasicView(discord.ui.View): def __init__(self, timeout: Optional[float] = 60.0, clear: Optional[bool] = False): """Basic View with author checks and timeout actions Parameters ----------- timeout: :class:`float` Time after view performs tiemout checks clear: :class:`bool` Whether to delete items in view after timeout Usage --------- set the following attributes to View for checks: View.author : discord.User | discord.Member View.mesaage : discord.Message | discord.InteractionMessage """ super().__init__(timeout=timeout) self.clear = clear self.message: Optional[discord.Message | discord.InteractionMessage] = None self.author: Optional[discord.Member | discord.User] = None self.bot: Optional[Bot] = None async def interaction_check(self, interaction: discord.Interaction) -> bool: if not self.author: return True # anyone can interact with View if self.author.id != interaction.user.id: await interaction.response.send_message( "only command invoker can do this", ephemeral=True ) return self.author.id == interaction.user.id async def on_timeout(self) -> None: if not self.children: return if self.clear: self.clear_items() else: for item in self.children: item.disabled = True if self.message is not None: try: await self.message.edit(view=self) except discord.HTTPException: ...