#Get reactions by message ID

1 messages · Page 1 of 1 (latest)

loud finch
#

Hi, i have this code:

@bot.slash_command(description="Krijg alle reacties te zien!")
async def ad_poll_resultaat(inter, msg_id):
        channel = bot.get_channel(inter.channel.id)
        reactions = await channel.fetch_message(msg_id)
        print(reactions)

Now i want to get the reactions that are in that message. I also want the text inside that same message. Is that possible? I did find online the fetch_message() part.

tropic pulsar
#

Message has .reactions and .content attribute

loud finch
# tropic pulsar Message has `.reactions` and `.content` attribute

Got a error:
AttributeError: 'coroutine' object has no attribute 'reactions'

@bot.slash_command(description="Krijg alle reacties te zien!")
async def ad_poll_resultaat(inter, msg_id):
        channel = bot.get_channel(inter.channel.id)
        reaction = await channel.fetch_message(msg_id).reactions
        print(reaction)```
potent vine
#

channel.fetch_message returns a coroutine

#

i.e. you have to await it before you can do anything further with it

loud finch
#

I do have it awaited right?

reaction = await channel.fetch_message(msg_id).reactions

tropic pulsar
#

That's doing something like await (Coroutine().reactions)

#

Attribute access binds tighter than await

loud finch
#

So in my case:
await (channel.fetch_message("1043874475986395206").reactions) right?

#

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\k_der\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\disnake\ext\commands\interaction_bot_base.py", line 1353, in process_application_commands
    await app_command.invoke(interaction)
  File "C:\Users\k_der\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\disnake\ext\commands\slash_core.py", line 739, in invoke
    raise CommandInvokeError(exc) from exc
disnake.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'coroutine' object has no attribute 'reactions'
C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2288.0_x64__qbz5n2kfra8p0\lib\asyncio\events.py:80: RuntimeWarning: coroutine 'Messageable.fetch_message' was never awaited
  self._context.run(self._callback, *self._args)
RuntimeWarning: Enable tracemalloc to get the object allocation traceback```
potent vine
#

no, almost there

#

you want to specifically await the fetch_message call

#

think of it as two steps:

msg = await channel.fetch_message(...)
reaction = msg.reactions
```which, when combined into one line, gives 
```py
reaction = (await channel.fetch_message(...)).reactions
loud finch
#

Yes it works! Thank you so mutch

potent vine
loud finch
#

Same can i use for content right

tropic pulsar
#

If you need two attributes off the message, you should really just await the fetch and assign the result to a variable as the message

loud finch
# tropic pulsar If you need two attributes off the message, you should really just await the fet...

Bu tthen i got:
<Message id=1043874475986395206 channel=<TextChannel id=979395372738818112 name='adtg-gaming-night' position=4 nsfw=False news=False category_id=979395372738818111 default_auto_archive_duration=1440 flags=<ChannelFlags value=0>> type=<MessageType.application_command: 20> author=<Member id=979388071617372210 name='Adje-Dev' discriminator='6811' bot=True nick=None guild=<Guild id=979395371992240198 name='AllDayDev' shard_id=0 chunked=True member_count=6>> flags=<MessageFlags value=0>> and not the content

tropic pulsar
#

...you have a message, just grab the two attributes you need off it?

tropic pulsar
#

so message.content

#

and message.reactions

loud finch
tropic pulsar
#

That's a different issue

loud finch
#
@bot.slash_command(description="Krijg alle reacties te zien!")
async def ad_poll_resultaat(inter, msg_id):
        channel = bot.get_channel(inter.channel.id)
        msg = await channel.fetch_message(msg_id)

        print(msg.reactions)
        print(msg.content)```
#

Ah

tropic pulsar
#

...why are you using the channel's ID to grab the exact same channel

#

channel = bot.get_channel(inter.channel.id)

#

that's just inter.channel lol

loud finch
#

Ah it's a embed, and wont get stuff from a embed

loud finch
tropic pulsar
#

Embeds lie under message.embeds

loud finch
#

Ah okey

loud finch
#

It works! Thanks!