user15609798
user15609798

Reputation:

Discord Python await message.channel.send can't be done in code?

I have been coding discord bot and I got some stuff to work, but now I am stuck on using await message.channel.send, here is error:

Traceback (most recent call last):
  File "C:\Users\*****\PycharmProjects\discordAPI\venv\lib\site-packages\discord\client.py", line 343, in _run_event
    await coro(*args, **kwargs)
  File "C:/Users/*****/PycharmProjects/discordAPI/main.py", line 37, in on_raw_reaction_add
    await message.channel.send('ADD Reaction To Be **immortal**  Rank')
NameError: name 'message' is not defined

and this is code:


role = None

client = discord.Client()


@client.event
async def on_message(self, message):
    # don't respond to ourselves
    if message.author == self.user:
        return

    if message.content == 'Hi':
        await message.channel.send('Hii, how are you')

    if message.content == '>message set':
        await message.channel.send('ADD Reaction To Be **immortal**  Rank')
        await message.channel.send('ADD Reaction To Be **coolguy**  Rank')
        await message.channel.send('ADD Reaction To Be **mortal**  Rank')


@client.event
async def on_raw_reaction_add(payload):
    message_id = payload.message_id
    if message_id == 833768364215762974:
        guild_id = payload.guild_id
        guild = discord.utils.find(lambda g: g.id == guild_id, client.guilds)
        print(payload.emoji.name)
        global role

        if payload.emoji.name == '✅':
            role = discord.utils.get(guild.roles, name='immortal')
        if role is not None:
            print(role.name)
            await message.channel.send('anything')
            # cant do await message here


@client.event
async def on_raw_reaction_add_remove(payload):
    pass


client.run('*******')

So why can't I use await there? Thanks for all the help

Upvotes: 0

Views: 1209

Answers (1)

Ali Hakan Kurt
Ali Hakan Kurt

Reputation: 1037

Because you need the get message object first.

message = await guild.get_channel(payload.channel_id).fetch_message(payload.message_id)

Now you can send message with

await message.channel.send()

Also if you will use message just for sending message, you can do it with getting channel.

Upvotes: 1

Related Questions