How can I read the message someone has reacted to? - python

I want to read the message content of the message that got reacted to. I believe that the payload doesn't have a message object but does have the message_id. How can I even get the message? There is nothing I can use to get the contents of the message?
Specifically, if its am embeded message, I want to read the footer
async def on_raw_reaction_add(self, payload):
message_id = payload.message_id

Sorry for the late answer!
When using the payload from the RawReactionEvent, you're able to get the channel_id as well. Using this, it means you can do:
async def on_raw_reaction_add(self, payload):
channel = self.bot.get_channel(payload.channel_id)
message = await channel.fetch_message(payload.message_id)
print(message.content) # now you can view the message's content!
References:
Client.get_channel()
TextChannel.fetch_message()

Related

How to get the text of the message to which the person has put a reaction. Discord.py

How to get the text of the message to which the person has put a reaction? Is it possible?
#client.event
async def on_raw_reaction_add(payload):
emoji = payload.emoji.name
if emoji == "🎦":
print(msg)
Yes, payload allows you to get the message ID.
You can then convert this to the message, by getting the channel and then fetching the message. If you don't want to do this, you should just use the normal reaction add event, which provides the actual message class (but only if the message cached - see here).
#client.event
async def on_raw_reaction_add(payload):
emoji = payload.emoji.name
if emoji == "🎦":
message = await client.get_channel(payload.channel_id).fetch_message(payload.message_id)
print(message.content)
# prints message content, or do whatever you need

Get message from RawReactionActionEvent (discord.py)

How can I get the message a reaction is used on? I know you can get the message with context, but how can I use the attributes
channel_id guild_id and message_id
to get the original message? This seems like enough information
I have tried using client.http.edit_message and client.http.delete_message which seems to work until using an embed. Whenever used, TypeError: Object of type Embed is not JSON serializable is raised
There are multiple ways of doing this:
Independent of the bot's cache, requires more API calls:
async def fetch_message(payload):
channel = await bot.fetch_channel(payload.channel_id)
message = await channel.fetch_message(payload.message_id)
return message
Depends on bot's cache, requires less API calls:
async def get_message(payload):
channel = bot.get_channel(payload.channel_id)
message = await bot.fetch_message(payload.message_id)
return message

is there a way to send a message after a message has been deleted?

So, when a user sends a message I want my bot to send a message to that channel with an emoji
so far I've been able to send it to a specific channel, but after that I haven't been able to send a message to any channel that a message was deleted in.
maybe I just don't know the attributes.
thanks for all the help in advance!
#bot.event
async def on_message_delete(ctx):
if ctx.author.id != 835676293625282601:
author = ctx.message.author
del_msg = await channel.send(":eyes:")
await author.send(del_msg)
await asynciaito.sleep(10)
await del_msg.delete()
Events never take ctx as an argument, if you take a look at the docs you can see that on_message_delete takes message as the only argument
#bot.event
async def on_message_delete(message):
if message.author.id != 835676293625282601:
author = message.author
del_msg = await message.channel.send(":eyes:")
await author.send(del_msg)
await asynciaito.sleep(10)
await del_msg.delete()
Also you don't have to learn all the attributes from all the discord objects, just read the documentation

Retrieving a message from on_raw_reaction_add discord.py

I've been trying to make it so the bot removes the reaction using discord.Message.remove_reaction(), but I can't find a way to actually retrieve and store the message in a variable. Does anybody know how to do this?
Here is the code I have so far:
#client.event
async def on_raw_reaction_add(self):
if self.message_id == 805179023641542726:
channel = client.get_channel(self.channel_id)
message = ???
user = client.get_user(self.user_id)
await message.remove_reaction(self.emoji,user)
on_raw_reaction_add returns a RawReactionActionEvent which has a message_id attribute. You can pass it to discord.abc.Messageable.fetch_message to retreive the message :
#client.event
async def on_raw_reaction_add(payload):
channel = client.get_channel(payload.channel_id)
message = await channel.fetch_message(payload.message_id)
user = client.get_user(payload.user_id)
await message.remove_reaction(payload.emoji,user)
Reference : discord.py documentation

How can I get the id from message a bot sent using discord

I am working with Python discord. And I need to get an id from message. I have a function:
async def send_message(user_id, text):
user = self.client.get_user(user_id)
await user.send(text)
And I want to return the id of new message.
add this at the end. It will send the message.id at the channel.
await message.channel.send(message.id)
Message Docs:
https://discordpy.readthedocs.io/en/latest/api.html#message
User.send returns an object of Message type which has an id field.
You can simply use
message.id or message.webhook_id if message comes from webhook.
async def send_message(user_id, text):
user = self.client.get_user(user_id)
message = await user.send(text)
return message.id
Sending any message to a Messageable object returns the Message object that was sent.
This means you can simply get the id attribute in your code, like this:
async def send_message(user_id, text):
user = self.client.get_user(user_id)
message = await user.send(text)
return message.id

Categories

Resources