如何在 Discord [Discord.js] 中找到回复的消息内容 [英] How to find the message content of what was replied to in Discord [Discord.js]
问题描述
我有一段简单的代码,可以在发送消息时记录消息内容.但是,如果该消息是对另一条消息的回复,它只会输出回复的内容,而不是原始消息的内容.
I have a simple piece of code that logs the message content whenever a message is sent. However, if the message is a reply to another message, it will only output what the reply was, and not the content of the original message as well.
client.on('message', async message => {
console.log(message.content);
});
输出将是:测试 4.我怎样才能也提取测试 3?
The output will be: test 4. How can I also extract test 3?
推荐答案
当你收到消息对象时,你有一个名为reference
的变量.如果您的消息是回复引用,则不应为空,并且包含 channelID
、guildID
、messageID
的 id.
When you receive the message object, you have a variable named reference
.
If your message is a reply reference should not be null and contains the ids of channelID
, guildID
, messageID
.
这样你就可以得到上一条消息的消息内容,其中一行看起来像
With this you can then get the message content of the previous message with a line that looks like
client.on('message', async message => {
const repliedTo = await message.channel.messages.fetch(message.reference.messageID);
console.log(repliedTo.content);
});
请注意 message.reference.messageID
您必须添加错误处理以避免在查找未定义变量时崩溃.
Just be careful with the message.reference.messageID
you have to add error handling to avoid crash when looking for an undefined variable.
祝你有美好的一天
这篇关于如何在 Discord [Discord.js] 中找到回复的消息内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!