当我创建一个按钮并处理回调或发送消息,等待与我的python不一致的反应,这似乎是有限的时间。有时在1小时后,机器人就不再登记反应了。可以肯定的是,一旦我重新启动机器人,连接就会丢失,并且它将不再注册交互。
然而,我已经看到机器人在不和谐的情况下,总是对一个按钮做出反应,不管这个按钮是在多久以前创建的。有办法做到这一点吗?我是否必须定期“重新连接”机器人到它创建的按钮?
简单的例子:
class ButtonView(disnake.ui.View):
def __init__(self):
super().__init__(timeout=None)
@disnake.ui.button(label="Hi", style=ButtonStyle.red)
async def first_button(
self, button: disnake.ui.Button, interaction: disnake.MessageInteraction
):
await interaction.response.send_message("Button clicked.")
class Test(commands.Cog):
def __init__(self, bot: commands.Bot):
self.bot = bot
@commands.slash_command()
async def test(self, inter):
await inter.send("Button!", view=ButtonView())在本例中,->将不再对按钮单击作出反应,过了一段时间后,或者我重新启动了机器人。
发布于 2022-05-15 15:33:51
你可以这样做:
import disnake
from disnake.ext import commands
# 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
# 2) Every item in the View has to have a custom_id set
# It is recommended that the custom_id be sufficiently unique to
# prevent conflicts with other buttons the bot sends.
# For this example the custom_id is prefixed with the name of the bot.
# Note that custom_ids can only be up to 100 characters long.
class PersistentView(disnake.ui.View):
def __init__(self):
super().__init__(timeout=None)
@disnake.ui.button(
label="Green", style=disnake.ButtonStyle.green, custom_id="persistent_view:green"
)
async def green(self, button: disnake.ui.Button, interaction: disnake.MessageInteraction):
await interaction.response.send_message("This is green.", ephemeral=True)
@disnake.ui.button(label="Red", style=disnake.ButtonStyle.red, custom_id="persistent_view:red")
async def red(self, button: disnake.ui.Button, interaction: disnake.MessageInteraction):
await interaction.response.send_message("This is red.", ephemeral=True)
@disnake.ui.button(
label="Grey", style=disnake.ButtonStyle.grey, custom_id="persistent_view:grey"
)
async def grey(self, button: disnake.ui.Button, interaction: disnake.MessageInteraction):
await interaction.response.send_message("This is grey.", ephemeral=True)
class PersistentViewBot(commands.Bot):
def __init__(self):
super().__init__(command_prefix=commands.when_mentioned)
self.persistent_views_added = False
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()
@commands.is_owner()
async def prepare(ctx: commands.Context):
"""Starts a persistent view."""
# In order for a persistent view to be listened to, it needs to be sent to an actual message.
# Call this method once just to store it somewhere.
# In a more complicated program you might fetch the message_id from a database for use later.
# However this is outside of the scope of this simple example.
await ctx.send("What's your favourite colour?", view=PersistentView())
bot.run("token")这段代码来自disnake储存库
发布于 2022-05-16 17:47:54
我认为最有可能的是你的互联网有连接问题,而且你在没有注意到的情况下断开了连接。
这种情况发生在我身上,所以我添加了on_disconnect和on_resumed机器人事件,它们只是简单的打印语句,这样我就能够检查这是否是问题的根源。
bot: commands.Bot = commands.Bot(command_prefix='.', intents=intents)
bot.time = time.time()
@bot.event
async def on_ready():
print(f"Logged in as {bot.user} (ID: {bot.user.id})")
print('TesterBot is ready starting at ' + time.ctime(bot.time))
@bot.event
async def on_disconnect():
uptimedelta = time.time() - bot.time
print('TesterBot is disconnected at ' + time.ctime(bot.time) + '. Testerbot has been up for ' + str(datetime.timedelta(seconds=uptimedelta)))
@bot.event
async def on_resumed():
uptimedelta = time.time() - bot.time
print("TesterBot reconnected " + time.ctime(bot.time) + '. Testerbot has been up for ' + str(datetime.timedelta(seconds=uptimedelta)))
@bot.event
async def on_connect():
print('TesterBot is connected starting at ' + time.ctime(time.time()))就像这样的基本东西帮助我们发现问题在于我的机器断开了连接。这是我的互联网的一个物理问题,而不是编码错误或对api或库的误解。
发布于 2022-08-10 07:44:50
等待inter.response.defer(在这里输入图像描述)
https://stackoverflow.com/questions/72249395
复制相似问题