mirror of
https://github.com/Rapptz/discord.py.git
synced 2025-06-08 04:38:42 +00:00
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
|
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
|
# Define a simple View that persists between bot restarts
|
||||||
# In order a view to persist between restarts it needs to meet the following conditions:
|
# 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
|
# 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)
|
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
|
async def on_ready(self):
|
||||||
# Note that this does not send the view to any message.
|
if not self.persistent_views_added:
|
||||||
# In order to do this you need to first send a message with the View, which is shown below.
|
# Register the persistent view for listening here.
|
||||||
# If you have the message_id you can also pass it as a keyword argument, but for this example
|
# Note that this does not send the view to any message.
|
||||||
# we don't have one.
|
# In order to do this you need to first send a message with the View, which is shown below.
|
||||||
bot.add_view(PersistentView())
|
# 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()
|
@bot.command()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user