Search code examples
pythondiscord.pybotsroles

Bot don't add emoji to message


I'm newbie in Python and tried to add reaction-roles for my discord bot, but my bot succesfully send message, but don't add emoji to message.

@client.event
async def on_message(message):

    if message.content.lower().startswith("!lol"):
        embed1 = discord.Embed(
            title="Escolha seu Elo!",
            color=COR,
            description="- Ferro = 👎\n"
                        "- Bronze = 🐤\n"
                        "- Prata  =  📘 \n"
                        "- Ouro  = 📙\n"
                        "- Platina = 👍\n"
                        "- Diamante = ✋\n"
                        "- Mestre = 👊\n"
                        "- Grã-Mestre = ✊\n"
                        "- Desafiante = 🙏\n",)

    botmsg = await message.channel.send(embed=embed1)

    await ctx.message.add_reaction(botmsg, "👎")
    await ctx.message.add_reaction(botmsg, "🐤")
    await ctx.message.add_reaction(botmsg, "📘")
    await ctx.message.add_reaction(botmsg, "📙")
    await ctx.message.add_reaction(botmsg, "👍")
    await ctx.message.add_reaction(botmsg, "✋")
    await ctx.message.add_reaction(botmsg, "👊")
    await ctx.message.add_reaction(botmsg, "✊")
    await ctx.message.add_reaction(botmsg, "🙏")

    global msg_id
    msg_id = botmsg.id

    global msg_user
    msg_user = message.author
 

This is the error i get I have tried many work arounds with this I can't find anything that will fix this issue

Ignoring exception in on_message
Traceback (most recent call last):
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/client.py", line 343, in _run_event
    await coro(*args, **kwargs)
  File "main.py", line 55, in on_message
    botmsg = await message.channel.send(embed=embed1)
UnboundLocalError: local variable 'embed1' referenced before assignment
Ignoring exception in on_message
Traceback (most recent call last):
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/client.py", line 343, in _run_event
    await coro(*args, **kwargs)
  File "main.py", line 57, in on_message
    await ctx.message.add_reaction(botmsg, "👎")
NameError: name 'ctx' is not defined

Also I tried use

emoji = client.get_emoji(310177266011340803)
await message.add_reaction(emoji)

But this don't work too

upd: These emoji is example, I wanna use both unicode emoji and custom emoji


Solution

  • Try this:

    @client.event
    async def on_message(message):
    
        if message.content.lower().startswith("!lol"):
            embed1 = discord.Embed(
                title="Escolha seu Elo!",
                color=COR,
                description="- Ferro = 👎\n"
                            "- Bronze = 🐤\n"
                            "- Prata  =  📘 \n"
                            "- Ouro  = 📙\n"
                            "- Platina = 👍\n"
                            "- Diamante = ✋\n"
                            "- Mestre = 👊\n"
                            "- Grã-Mestre = ✊\n"
                            "- Desafiante = 🙏\n",)
    
        botmsg = await message.channel.send(embed=embed1)
    
        await botmsg.add_reaction("👎")
        await botmsg.add_reaction("🐤")
        await botmsg.add_reaction("📘")
        await botmsg.add_reaction("📙")
        await botmsg.add_reaction("👍")
        await botmsg.add_reaction("✋")
        await botmsg.add_reaction("👊")
        await botmsg.add_reaction("✊")
        await botmsg.add_reaction("🙏")
    
        global msg_id
        msg_id = botmsg.id
    
        global msg_user
        msg_user = message.author
    

    The context arg is not passed when on_message is called. Instead of ctx.message.add_reaction() you can use message.add_reaction().
    EDIT: Oh I understand your full problem, now the script should react to your bot's message.