add persistent view in on_ready to avoid loop issues
This commit is contained in:
parent
7601d6cec3
commit
7d0bd7ed20
@ -2,15 +2,6 @@ from discord.ext import commands
|
||||
import discord
|
||||
|
||||
|
||||
class PersistentViewBot(commands.Bot):
|
||||
def __init__(self):
|
||||
super().__init__(command_prefix=commands.when_mentioned_or('$'))
|
||||
|
||||
async def on_ready(self):
|
||||
print(f'Logged in as {self.user} (ID: {self.user.id})')
|
||||
print('------')
|
||||
|
||||
|
||||
# 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
|
||||
@ -36,14 +27,26 @@ class PersistentView(discord.ui.View):
|
||||
await interaction.response.send_message('This is grey.', ephemeral=True)
|
||||
|
||||
|
||||
bot = PersistentViewBot()
|
||||
class PersistentViewBot(commands.Bot):
|
||||
def __init__(self):
|
||||
super().__init__(command_prefix=commands.when_mentioned_or('$'))
|
||||
self.persistent_views_added = False
|
||||
|
||||
# Register the persistent view for listening
|
||||
# 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.
|
||||
bot.add_view(PersistentView())
|
||||
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()
|
||||
|
Loading…
x
Reference in New Issue
Block a user