[英]How to get the message content/embed from the message id?
I want to know how to get a message content (specifically the embeds) from the message id
?我想知道如何从
message id
获取消息内容(特别是嵌入)? Just like you can get the member using a member id
就像您可以使用
member id
获取会员一样
on_raw_reaction_add()
example: on_raw_reaction_add()
示例:@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
@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!在命令中,我传入了
channel
和msgID
,因此示例命令执行将像!getmsg #general 112233445566778899
- 通道必须在您正在执行命令的同一服务器中!
Then I'm getting the message object using the fetch_message()
coroutine, which allows me to get a list of embeds
in said message.然后我使用
fetch_message()
协程收到消息 object,它允许我在所述消息中获取embeds
列表。 I then choose the first, and only, embed by choosing position index 0
.然后我通过选择 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 discord.Embed
- 在这里您可以找到嵌入的不同属性commands.command()
- Command decorator commands.command()
- 命令装饰器on_raw_reaction_add()
discord.RawReactionActionEvent
- The payload returned from the reaction event discord.RawReactionActionEvent
- 从反应事件返回的有效负载RawReactionActionEvent.message_id
- The ID of the message which was reacted to RawReactionActionEvent.message_id
- 响应的消息的 IDRawReactionActionEvent.channel_id
- The text channel that the reaction was added in RawReactionActionEvent.channel_id
- 添加反应的文本通道@bot.event
async def on_raw_reaction_add(ctx):
if (ctx.user_id != bot_id):
channel = bot.get_channel(ctx.channel_id)
msg = await channel.fetch_message(ctx.message_id)
msg_embed = msg.embeds[0]
if (...):
When I use this, terminal always send me that, but I don't understand where is the problem当我使用它时,终端总是向我发送那个,但我不明白问题出在哪里
Ignoring exception in on_raw_reaction_add
Traceback (most recent call last):
File "...\client.py", line 343, in _run_event
await coro(*args, **kwargs)
File "...\main.py", line 124, in on_raw_reaction_add
msg_embed = msg.embeds[0]
IndexError: list index out of range
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.