问题
I want to know how to get a message content (specifically the embeds) from the message id
? Just like you can get the member using a member id
回答1:
on_raw_reaction_add()
example:
@bot.event
async def on_raw_reaction_add(payload):
channel = bot.get_channel(payload.channel_id)
msg = await channel.fetch_message(payload.message_id)
embed = msg.embeds[0]
# do something you want to
Command example:
@bot.command()
async def getmsg(ctx, channel: discord.TextChannel, msgID: int):
msg = await channel.fetch_message(msgID)
await ctx.send(msg.embeds[0].description)
In the command I'm passing in channel
and msgID
, so a sample command execution would like !getmsg #general 112233445566778899
- the channel must be in the same server that you're executing the command in!
Then I'm getting the message object using the fetch_message()
coroutine, which allows me to get a list of embeds
in said message. I then choose the first, and only, embed by choosing position index 0
.
After that, the bot then sends the description (or whatever attribute you'd like) of the embed.
References:
- discord.TextChannel
- TextChannel.fetch_message()
- Message.embeds
- discord.Embed - This is where you can find the different attributes of the embed
- commands.command() - Command decorator
- on_raw_reaction_add()
- discord.RawReactionActionEvent - The payload returned from the reaction event
- RawReactionActionEvent.message_id - The ID of the message which was reacted to
- RawReactionActionEvent.channel_id - The text channel that the reaction was added in
来源:https://stackoverflow.com/questions/61883534/how-to-get-the-message-content-embed-from-the-message-id