Search code examples
pythondiscorddiscord.py

discord.py add reaction to own message


As the title says, I am trying to make the bot react to its own messages. Here is my example code so far:

from inspect import CO_NESTED
from typing import Literal
from discord.ext import commands
import discord
import random
import asyncio
import time

TOKEN = "xxx"

client = discord.Client()

@client.event
async def on_ready():
    print('We have logged in as {0.user}'.format(client))

@client.event
async def on_message(message):
    username = str(message.author).split('#')[0]
    user_message = str(message.content)
    channel = str(message.channel.name)
    print(f'{username}: {user_message} ({channel})')

    if message.author == client.user:
        return
    
    if "p!react" in user_message.lower():
        await message.channel.send("hello!")
        await message.add_reaction("📰")


client.run(TOKEN)

However, this reacts to the user's message, not the bot's. Any help you could give would be greatly appreciated!


Solution

  • .send() returns a new Message object of the message sent. You'll want to add the reaction to that message instead.

    new_msg = await message.channel.send("hello!")
    await new_msg.add_reaction("📰")