1
votes

I am making a discord bot that queries the Spotify API and returns the result as a Discord embed. Instead of sending multiple results in multiple messages, It sends one message that shows one result in an embed, which the user can navigate through by reacting with the ⬅️ emoji or the ➡️ emoji.
When the bot detects an on_reaction_add event, it detects whether the reaction is the ⬅️ or the ➡️ emoji, and edits the message that has that reaction to update the embed.
However with my approach, whenever somebody adds a ⬅️ or ➡️ reaction to the bot's other messages, the bot will edit it, and I cannot figure out how to get the bot to remember the query given by the user, and then fetch the Spotify API again using the same query, but with a different offset.
My question is, how do I make the bot detect ⬅️ and ➡️ reactions only on the messages with the Spotify embed, and how do I make the bot remember what the query was in the message?
Here is the code I am using:

@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)
1
You can get the embed of a message via: embed = reaction.message.embeds[0], and from that embed, you can get the contents, such as embed.title, embed.description. One that might be particularly useful is embed.url.Diggy.
Sorry for the late reply, but I got it to work similarly with a field containing the API link, since I wanted to reserve the title's URL to link to the search result itself, thank you! @Diggyhopital

1 Answers

1
votes

Use reaction.message. It detects the message that was reacted to.