Discord.Py adding reactions to an embeded message - python

So I am trying to add three different reactions (emojis), to a message that the bot has sent in a text channel.
The user fills in a form in their DM, and the message then get sent into a text-channel called "admin-bug", the admins of the server can then react to three different emojis:
fixed
will not be fixed
not-a-bug
And then, depending on what emoji the admin press on, the message will be transferred to a text channel.
But! I can't seem to figure out how you actually add the reactions to the message itself, I have done a bunch of googling, but can't find the answer.
code:
import discord
from discord.ext import commands
TOKEN = '---'
bot = commands.Bot(command_prefix='!!')
reactions = [":white_check_mark:", ":stop_sign:", ":no_entry_sign:"]
#bot.event
async def on_ready():
print('Bot is ready.')
#bot.command()
async def bug(ctx, desc=None, rep=None):
user = ctx.author
await ctx.author.send('```Please explain the bug```')
responseDesc = await bot.wait_for('message', check=lambda message: message.author == ctx.author, timeout=300)
description = responseDesc.content
await ctx.author.send('````Please provide pictures/videos of this bug```')
responseRep = await bot.wait_for('message', check=lambda message: message.author == ctx.author, timeout=300)
replicate = responseRep.content
embed = discord.Embed(title='Bug Report', color=0x00ff00)
embed.add_field(name='Description', value=description, inline=False)
embed.add_field(name='Replicate', value=replicate, inline=True)
embed.add_field(name='Reported By', value=user, inline=True)
adminBug = bot.get_channel(733721953134837861)
await adminBug.send(embed=embed)
# Add 3 reaction (different emojis) here
bot.run(TOKEN)

Messagable.send returns the message it sends. So you can add reactions to it using that message object. Simply, you have to use a variable to define the message you sent by the bot.
embed = discord.Embed(title="Bug report")
embed.add_field(name="Name", value="value")
msg = await adminBug.send(embed=embed)
You can use msg to add reactions to that specific message
await msg.add_reaction("💖")
Read the discord.py documentation for detailed information.
Message.add_reaction

The discord.py docs have an FAQ post about adding reactions, it has multiple exampes and an indepth description, furthermore Messageable.send returns the message send so you can use Message.add_reaction on that. https://discordpy.readthedocs.io/en/neo-docs/faq.html#how-can-i-add-a-reaction-to-a-message

You need to save the embed as a variable, in this way you can add a reaction.
message = await adminBug.send(embed=embed) # save embed as "message"
await message.add_reaction('xxx') # add reaction to "message"

I'm not sure because I use nextcord (and it worked), but I think this can work :
#bot.command
async def testembed(ctx):
embed = discord.Embed(title='Test !', description='This is a test embed !')
msg = await ctx.send("", embed=embed)
msg = msg.fetch() # Notice this line ! It's important !
await msg.add_reaction('emoji_id')

Related

How do I DM a user when a reaction is added to a message?

Combining a user reacting with sending them a DM has had me stumped.
My project works by having an embed that has a reaction added at the bottom and when the user clicks the emoji then it will DM the user a set of instructions (for now I'm just using the word 'Test').
Here is what I have so far:
#client.event
async def on_message(message):
if message.content.startswith('.add'):
embed = discord.Embed(title="Embed Title", color=0x6610f2)
embed.add_field(name="__**Steps to get DM'd:**__", value="""
• Step 1
• Step 2
• Step 3
• Step 4
• Step 5
""")
msg = await message.channel.send(embed=embed)
await msg.add_reaction('\U0001F91D')
#client.event
async def on_raw_reaction_add(payload):
if payload.user_id == client.user.id:
return
if str(payload.emoji) == '\U0001F91D':
channel = await client.fetch_channel(payload.channel_id)
message = await channel.fetch_message(payload.message_id)
await message.author.send('Test')
I'm just not sure how to get the user's ID outside of the on_message() function because the only way I know how to make the on_reaction_add work is in its own client event. I'm not even sure if I'm handling the DM user aspect correctly.
Using the async version of Discord.py
In your payload, you should already get a discord.Member object you can work with. Try something like
#client.event
async def on_raw_reaction_add(payload):
if str(payload.emoji) == "\U0001F91D":
member = payload.member
await member.send('This is a DM to you!')
Because your code does not check for the message id, a reaction to ANY message (not just the one you sent) will lead to a response. To prevent this, you can check whether payload.message_id matches the id of your bot's message (which you could store somewhere when sending the message).
You also tried to prevent a response to the initial reaction of your bot. Instead of checking for payload.user_id, I would suggest
if member.bot:
return
which prevents a response to any bot reacting to your message, including your own.

how can i get the id of an emoji in reaction?

I'm trying to get the id of an emoji in the reaction of a message but I don't know how to do it.
#client.event
async def on_message(message):
channal = client.get_channel(825523364844142601)
embeds = message.embeds
for embed in embeds:
if embed.author.name in wishlist:
reaction = get(message.reactions)
print(reaction)
await channal.send("yes")
else:
await channal.send("false")
context : im playing a discord's game, where a bot send an embed with the name of a character and on this embed, the bot automatically add a reaction. What i want is get the id of the bot's reaction and with the id add the same
You don't need to get the emoji again to react. You can just add the reaction from the Emoji class.
I also suggest waiting a little bit and getting the message again because reactions won't be on the Message object immediately.
#client.event
async def on_message(message):
channal = client.get_channel(825523364844142601)
embeds = message.embeds
for embed in embeds:
if embed.author.name in wishlist:
await asyncio.sleep(1.0) # Give time for reaction to update on cache
message = await message.channel.fetch_message(message.id) # Update message object
reaction = message.reactions[0] # Get first reaction of a message
emoji = reaction.emoji
emoji_id = emoji.id
await message.add_reaction(emoji) # You can just add with emoji, you don't need to get emoji again
await channal.send("yes")
else:
await channal.send("false")
See the discord.Reaction object.

Issue with sending dm messages discord.py

I have a command in my bot that allows a user to dm an anonymous message to me. However, when I tested the command my bot only sends the first word of the message to me.
#commands.command(name='msgetika', aliases=['msgmax'])
async def msgetika(self, ctx, message1=None):
'''Send an annonymous message to discord'''
if message1 is None:
await ctx.send('Please specify a message')
maxid = await self.bot.fetch_user('643945264868098049')
await maxid.send('Anonymous message: ' + message1)
msg = await ctx.send('Sending message to max.')
await ctx.message.delete()
await asyncio.sleep(5)
await msg.delete()
You need to add a * if you want to catch the entire phrase. View the docs here about using this and have a look at the example working beneath the code.
#commands.command(name='msgetika', aliases=['msgmax'])
async def msgetika(self, ctx, *, message1=None):
Also, unless you are using an older version of discord.py, user ids are ints.
maxid = await self.bot.fetch_user(USER ID HERE)

discord.py - adding reaction as part of command

I'm writing a custom help command that sends a embed DM to a user, that all works fine. As part of the command, I'm trying to make the bot react to the command message but I cannot get it to work, I've read the documentation but I still can't seem to get it to react as part of a command. Below is the what I'm trying to achieve:
#client.command(pass_context=True)
async def help(ctx):
# React to the message
author = ctx.message.author
help_e = discord.Embed(
colour=discord.Colour.orange()
)
help_e.set_author(name='The bot\'s prefix is !')
help_e.add_field(name='!latency', value='Displayes the latency of the bot', inline=False)
help_e.add_field(name='!owo, !uwu, !rawr', value='Blursed commands', inline=False)
await author.send(embed=help_e)```
You can use message.add_reaction() and also you can use ctx.message to add reaction to the message. Here is what you can do:
#client.command(pass_context=True)
async def help(ctx):
# React to the message
await ctx.message.add_reaction('✅')
author = ctx.message.author
help_e = discord.Embed(
colour=discord.Colour.orange()
)
help_e.set_author(name='The bot\'s prefix is !')
help_e.add_field(name='!latency', value='Displayes the latency of the bot', inline=False)
help_e.add_field(name='!owo, !uwu, !rawr', value='Blursed commands', inline=False)
sent_embed = await author.send(embed=help_e)

Discord.py - Make a bot react to its own message(s)

I am trying to make my discord bot react to its own message, pretty much. The system works like this:
A person uses the command !!bug - And gets a message in DM', she/she is supposed to answer those questions. And then whatever he/she answered, it will be transferred an embedded message to an admin text-channel.
But I need to add 3 emojis, or react with three different emojis. And depending on what the admin chooses, it will transfer the message once more. So if an admin reacts to an emoji that equals to "fixed", it will be moved to a "fixed" text-channel (the entire message).
I have done a lot of research about this, but only found threads about the old discord.py, meaning
await bot.add_react(emoji) - But as I have understood it, that no longer works!
Here is my code:
import discord
from discord.ext import commands
import asyncio
TOKEN = '---'
bot = commands.Bot(command_prefix='!!')
reactions = [":white_check_mark:", ":stop_sign:", ":no_entry_sign:"]
#bot.event
async def on_ready():
print('Bot is ready.')
#bot.command()
async def bug(ctx, desc=None, rep=None):
user = ctx.author
await ctx.author.send('```Please explain the bug```')
responseDesc = await bot.wait_for('message', check=lambda message: message.author == ctx.author, timeout=300)
description = responseDesc.content
await ctx.author.send('````Please provide pictures/videos of this bug```')
responseRep = await bot.wait_for('message', check=lambda message: message.author == ctx.author, timeout=300)
replicate = responseRep.content
embed = discord.Embed(title='Bug Report', color=0x00ff00)
embed.add_field(name='Description', value=description, inline=False)
embed.add_field(name='Replicate', value=replicate, inline=True)
embed.add_field(name='Reported By', value=user, inline=True)
adminBug = bot.get_channel(733721953134837861)
await adminBug.send(embed=embed)
# Add 3 reaction (different emojis) here
bot.run(TOKEN)
In discord.py#rewrite, you have to use discord.Message.add_reaction:
emojis = ['emoji 1', 'emoji_2', 'emoji 3']
adminBug = bot.get_channel(733721953134837861)
message = await adminBug.send(embed=embed)
for emoji in emojis:
await message.add_reaction(emoji)
Then, to exploit reactions, you'll have to use the discord.on_reaction_add event. This event will be triggered when someone reacts to a message and will return a Reaction object and a User object:
#bot.event
async def on_reaction_add(reaction, user):
embed = reaction.embeds[0]
emoji = reaction.emoji
if user.bot:
return
if emoji == "emoji 1":
fixed_channel = bot.get_channel(channel_id)
await fixed_channel.send(embed=embed)
elif emoji == "emoji 2":
#do stuff
elif emoji == "emoji 3":
#do stuff
else:
return
NB: You'll have to replace "emoji 1", "emoji 2" and "emoji 3" with your emojis. add_reactions accepts:
Global emojis (eg. 😀) that you can copy on emojipedia
Raw unicode (as you tried)
Discord emojis: \N{EMOJI NAME}
Custom discord emojis (There might be some better ways, I've came up with this after a small amount a research)
async def get_emoji(guild: discord.Guild, arg):
return get(ctx.guild.emojis, name=arg)
When you want to add non custom emojis you need to add its id and name into a format that discord.py can read: <:emoji name:emoji id>
For example, the white and green check emoji that is a discord default emoji...
you would need to write <:white_check_mark:824906654385963018> for discord.py to be able to identify it.

Categories