Search code examples
pythondiscordnextcord

Add a reaction to a message in an interaction


I have a poll command in my nextcord bot. I want to add reactions, but when I try the command, It give me this: https://imgur.com/a/p6s3yy5 . How can i fix it?

btw, this is my command:

@nextcord.slash_command(name="yes-no-poll", description="Crée un sondage à deux options", guild_ids=[server])
async def yesno(self, ctx: nextcord.Interaction, poll):
    embed = nextcord.Embed(title=poll, color=nextcord.Color.blue())
    vote = await ctx.response.send_message(embed=embed)
    await vote.add_reaction("<:yes:914969003645091900>")
    await vote.add_reaction("<:no:914969105482809355>")

Solution

  • Please refrain from posting your errors as images. Please paste them directly into the post.

    Explanation

    As for the code at hand, InteractionResponse.send_message() always returns None, unlike abc.Messageable.send(), which returns a nextcord.Message.

    This of course raises an Exception when you try to call add_reaction on a NoneType.

    To get around this, you can search the Interaction.channel for a message containing the same Embed as the one you just sent, then add the reaction to that.

    Code

    @nextcord.slash_command(name="yes-no-poll", description="Crée un sondage à deux options", guild_ids=[703732969160048731])
    async def yesno(self, ctx: nextcord.Interaction, poll):
        embed = nextcord.Embed(title=poll, colour=nextcord.Colour.blue())
        await ctx.response.send_message(embed=embed)
    
        # Loop through channel history and pull the message that matches (should be first)
        message: nextcord.Message
        async for message in ctx.channel.history():
            if not message.embeds:
                continue
            if message.embeds[0].title == embed.title and message.embeds[0].colour == embed.colour:
                vote = message
                break
        else:
            # something broke
            return
    
        await vote.add_reaction("<:yes:914969003645091900>")
        await vote.add_reaction("<:no:914969105482809355>")