Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

discord.py get message object from message link

I was wondering if there was a way to get the message from the message link (https://discordapp.com/channels/guild_id/channel_id/message_id) using a Python Discord bot, and if so, how to. I can think of a way using

link = 'https://discordapp.com/channels/guild_id/channel_id/message_id'.split('/')
message = await self.bot.get_guild(int(link[-3])).get_channel(int(link[-2])).fetch_message(int(link[-1]))

but I was wondering if there is a more direct way. Thanks in advance!

like image 725
RJTimmerman Avatar asked Oct 26 '25 23:10

RJTimmerman


1 Answers

To do this you should first get the guild, channel and message ids by doing this:

server_id = int(link[4])
channel_id = int(link[6])
msg_id = int(link[5])

Next you should use the bot to get the guild object, then use the guild object to get the text channel object and then finally use the text channel object to get the message object. Like this:

server = client.get_guild(server_id)
channel = server.get_channel(channel_id)
message = await channel.fetch_message(msg_id)

discord.py get_guild() reference: https://discordpy.readthedocs.io/en/latest/api.html#discord.Client.get_guild

discord.py get_channel() reference: https://discordpy.readthedocs.io/en/latest/api.html#discord.Guild.get_channel

discord.py fetch_message() reference: https://discordpy.readthedocs.io/en/latest/api.html#discord.abc.Messageable.fetch_message

like image 63
kantuni Avatar answered Oct 29 '25 13:10

kantuni



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!