#view after bot offline
1 messages · Page 1 of 1 (latest)
Here's the persistent example.
^
I've tried to follow this example, but my buttons don't seem to be persistent - after restarting the bot, the buttons no longer work.
Code:
import os
import discord
from dotenv import load_dotenv
class PersistentView(discord.ui.View):
def __init__(self):
super().__init__(timeout=None)
@discord.ui.button(label="Green", style=discord.ButtonStyle.green, custom_id="persistent_view:green")
async def green(self, button: discord.ui.Button, interaction: discord.Interaction):
await interaction.response.send_message("This is green.", ephemeral=True)
@discord.ui.button(label="Red", style=discord.ButtonStyle.red, custom_id="persistent_view:red")
async def red(self, button: discord.ui.Button, interaction: discord.Interaction):
await interaction.response.send_message("This is red.", ephemeral=True)
@discord.ui.button(label="Grey", style=discord.ButtonStyle.grey, custom_id="persistent_view:grey")
async def grey(self, button: discord.ui.Button, interaction: discord.Interaction):
await interaction.response.send_message("This is grey.", ephemeral=True)
load_dotenv(".env")
intents = discord.Intents.default()
intents.message_content = True
bot = discord.Bot(intents=intents)
@bot.command()
async def prepare(ctx):
await ctx.respond("test", view=PersistentView())
bot.run(os.getenv("TOKEN"))
you need to add the view to the bot to be persistent. check the example and the guide
examples/views/persistent.py lines 39 to 50
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("------")```
I see - I somehow missed the add view part
Can I add the view inside a cog?
as long as it's in on_ready