Conqueror
Conqueror

Reputation: 43

Python discord bot message defined problem

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"

Upvotes: 1

Views: 228

Answers (1)

itzFlubby
itzFlubby

Reputation: 2289

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("✔️")

Upvotes: 2

Related Questions