【发布时间】:2020-09-08 02:10:21
【问题描述】:
我正在制作一个 Discord 机器人,它查询 Spotify API 并将结果作为 Discord 嵌入返回。它不是在多条消息中发送多个结果,而是发送一条消息,在嵌入中显示一个结果,用户可以通过与⬅️表情符号或➡️表情符号做出反应来导航。
当机器人检测到on_reaction_add event 时,它会检测反应是⬅️ 还是➡️ 表情符号,并编辑具有该反应的消息以更新嵌入。
但是使用我的方法,每当有人向机器人的其他消息添加⬅️或➡️反应时,机器人都会对其进行编辑,我无法弄清楚如何让机器人记住用户给出的查询,然后获取 Spotify API再次使用相同的查询,但偏移量不同。
我的问题是,如何让机器人仅检测 ⬅️ 和 ➡️ 对嵌入了 Spotify 的消息的反应,以及如何让机器人记住消息中的查询内容?
这是我正在使用的代码:
@client.event
async def on_message(message):
#if the spotify command is triggered
#fetch from the API
spotifyEmbed = discord.Embed(title=resultName, ...)
spotifyEmbed.set_image(url=spotifyImgUrl)
spotifyMessage = await message.channel.send(embed=spotifyEmbed)
await spotifyMessage.add_reaction("⬅️")
await spotifyMessage.add_reaction("➡️")
@client.event
async def on_reaction_add(reaction, user):
if user != client.user:
if str(reaction.emoji) == "➡️":
#fetch new results from the Spotify API
newSearchResult = discord.Embed(...)
await reaction.message.edit(embed=newSearchResult)
if str(reaction.emoji) == "⬅️":
#fetch new results from the Spotify API
newSearchResult = discord.Embed(...)
await reaction.message.edit(embed=newSearchResult)
【问题讨论】:
-
您可以通过:
embed = reaction.message.embeds[0]获取消息的嵌入,并从该嵌入中获取内容,例如embed.title、embed.description。一个可能特别有用的是embed.url。 -
抱歉回复晚了,但我让它与包含 API 链接的字段类似,因为我想保留标题的 URL 以链接到搜索结果本身,谢谢! @Diggy
标签: python discord python-3.7 discord.py