1

I'm trying to build a bot that detects a specific emoji. When bot gets this, it's moving message to specific channel. For example, if message reaction has "like_emoji", it has to move this message.content to "xxx" channel. I couldn't find any solution on stackoverflow (or maybe I didn't search deeper).

Here is my code:

import os
import sys
import discord
from discord.ext import commands

my_secret = os.environ['locksecret']

intents = discord.Intents.default()
intents = discord.Intents(messages=True,guilds=True,reactions=True,members=True,presences=True)

Bot = commands.Bot(command_prefix = "!",intents=intents)
@Bot.event
async def on_reaction_add(reaction,user):
  channel = reaction.message.channel
  await channel.send("emoji added")
  if reaction.emoji == '👍':
    channel = Bot.get_channel("channel_id")
    await channel.send("{} written by:\n {}".format(message.author.mention,message.content))
    await message.channel.send("{} your message move to this channel --> {}".format(message.author.mention,channel.mention))
    await message.add_reaction("✔️")
Bot.run(my_secret)

When I do this, I get this error:

***Ignoring exception in on_reaction_add
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_reaction_add
    await channel.send("{} written by:\n {}".format(message.author.mention,message.content))
NameError: name 'message' is not defined***

How can I solve this problem? By the way, in code spaces, there is some code that I tried to bot command, because of this, console says "line 57"

1
  • The error tells you what the problem is? You are using message.author.mention, message.content and message.channel.send but message is not defined in any way. Commented May 24, 2021 at 11:09

1 Answer 1

2

Your problem is that message is no where defined, just like your error states. The on_reaction_add event doesn't provide the message that got the reaction added. You should use the on_raw_reaction_add event instead.


@Bot.event
async def on_raw_reaction_add(payload):
    guild = Bot.get_guild(<your-guild-id>)
    channel = guild.get_channel(payload.channel_id)
    message = await channel.fetch_message(payload.message_id)
    await channel.send("emoji added")
    if payload.emoji.name == '👍':
      await channel.send("{} written by:\n {}".format(message.author.mention,message.content))
      await message.channel.send("{} your message move to this channel --> {}".format(message.author.mention,channel.mention))
      await message.add_reaction("✔️")
Sign up to request clarification or add additional context in comments.

11 Comments

guild = Bot.get_guild(<your-guild-id>) that part you mean bot user id or mine?
The guild id. If you don't want it to be static use payload.guild_id like so: guild = Bot.get_guild(payload.guild_id)
oh thx. it had been fixed. but there is another problem became. it sending message same channel. how can i choose the channel?
Then send it in the desired channel with await guild.get_channel(<channel-id>).send("...") where <channel-id> has to be replaced with the actual channel-id. If it doesn't work right away, maybe ask another question regarding this.
oh really thank you sir. last question. how can i get count of "emoji"? for example if n times get "emoji" then do bla bla. do i have to make with len() function?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.