![](/img/trans.png)
[英]Discord.py: wait_for('reaction_add') not working as intended
[英]discord bot (python) "wait_for" "reaction_add" duplicating embed
我一直在嘗試制作嵌入以顯示戲劇擁有的角色列表時遇到問題。 我基本上想要:left_arrow: 和:right_arrow: emojis 作為對用戶擁有的長字符數據庫之間左右平移的反應。
我確實設法做到了,但是我注意到,如果我在一個超時之前使用兩次顯示所有字符的嵌入的命令,然后單擊向左箭頭查看下一頁,它將轉到下一頁兩種嵌入,而不僅僅是我反應的那個。
這是我的源代碼。
@commands.command(aliases=["collection", "c"])
@commands.cooldown(1, 5, commands.BucketType.user)
async def characters(self, ctx):
if not await bot.check_channel_permissions(ctx.author, ctx.channel): # Checks if the bot can send messages in the channel.
return
playerData = await bot.get_player_data(ctx.author, ctx.channel) # Returns the user's data.
if playerData:
if len(playerData["Characters"]) > 0: # Checks amount of characters the user owns.
if len(playerData["Characters"]) > 20: # Player owns 20 so it will create an embed with reactions.
global pageNumber
global maxPages
pageNumber = 1
maxPages = math.ceil(len(playerData["Characters"]) / 20)
embed = await characters.setupEmbed(self, playerData, ctx.author, pageNumber)
currentEmbed = await ctx.channel.send(ctx.author.mention, embed=embed)
await currentEmbed.add_reaction("⬅")
await currentEmbed.add_reaction("➡")
async def listenForRection(self, mainUser): # Waits until the user reacts with the left or right arrow in order to edit the current embed.
global pageNumber # the current page (20 characters are shown a page)
global maxPages # the max amount of pages the user can pan between.
def check(reaction, reactionuser):
if reactionuser != mainUser:
return False
elif str(reaction.emoji) == '⬅' and pageNumber > 1:
return True
elif str(reaction.emoji) == '➡' and pageNumber < maxPages:
return True
else:
return False
try:
reaction, reactionuser = await self.client.wait_for(
"reaction_add",
timeout=60,
check=check
)
except:
asyncio.TimeoutError
await currentEmbed.edit(content="Timed out.")
else:
if str(reaction.emoji) == '⬅':
pageNumber += -1
elif str(reaction.emoji) == "➡":
pageNumber += 1
newEmbed = await characters.setupEmbed(self, playerData, mainUser, pageNumber) # Returns an embeded list given the page number and info on the user.
await currentEmbed.edit(embed=newEmbed) # re-edits the current embed to show the next or previous page.
await listenForRection(self, mainUser) # re-runs the function again to listen for the next reaction.
await listenForRection(self, ctx.author) # runs the listener once so that it can begin detecting and re-running on its own.
else: # player will get an embeded list with no reactions since they only have 1 page of characters.
embed = await characters.setupEmbed(self, playerData, ctx.author, 1)
await ctx.channel.send(ctx.author.mention, embed=embed)
else:
await ctx.reply("You don't have any characters.")
會發生什么是我運行一次命令,它工作得很好。 然后,當我在超時之前再次運行它並單擊一個反應時,它要么不起作用,要么在新嵌入和舊嵌入上都運行。 我花了很多時間嘗試多種事情並進行搜索,但除了在修復當前錯誤后立即創建一個新錯誤之外,我並沒有做太多事情。
實際上有一個用於制作分頁菜單的 ext 庫,稱為discord-ext-menus 。 它為您完成大部分工作,因此您不必創建自己的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.