admin管理员组文章数量:1328052
How would you get the object for the a bot's last message? I tried doing something like:
if (message.content.split(' ')[0] === 'test') {
message.channel.sendMessage('Test')
console.log(client.user.lastMessage.content)
}
If I trigger the conditional, the console gives me an error: TypeError: Cannot read property 'content' of undefined
How would you get the object for the a bot's last message? I tried doing something like:
if (message.content.split(' ')[0] === 'test') {
message.channel.sendMessage('Test')
console.log(client.user.lastMessage.content)
}
If I trigger the conditional, the console gives me an error: TypeError: Cannot read property 'content' of undefined
-
That error means that
client.user.lastMessage
is undefined. That means that it's possible that there was no lastMessage (because remember, the message technically could still be sending, as nodeJS is non-blocking), or maybe the lastMessage attribute is wrong – dGRAMOP Commented Dec 25, 2017 at 16:21 -
So node.js doesn't wait for a thread to finish before moving on to the next mand? If so, I tried to use
setTimeout()
to wait 3 seconds before it logs the message in the console. Is there a way to detect when the message is actually sent before it continues execution? – Meh Commented Dec 25, 2017 at 16:33 - callbacks. Does sendMessage(String) overload a callback parameter? – dGRAMOP Commented Dec 25, 2017 at 16:34
- I mean I recend trying to just console.log(client.user), and seeing if there's a lastMessage param or getter – dGRAMOP Commented Dec 25, 2017 at 16:35
-
Ah. The
lastMessage
variable doesn't exist at all in the object. ThelastMessageID
is alsonull
. Is there an alternative way to set theclient.user.lastMessage
object since it doesn't appear to be inread-only
mode? – Meh Commented Dec 25, 2017 at 16:41
2 Answers
Reset to default 3The reason the value of client.user.lastmessage
is null
is because you are just starting the bot, and it hasn't sent any messages before you are running your 'test'
mand.
To circumnavigate this, I'd check if it's null (in case it isn't) and in the off-chance that it is, use a MessageCollector and wait for your message to be sent.
if (client.user.lastMessage == null) {
// Set the collector for the same channel
// Ensure the id is the same
var myID = client.user.id;
// Create collector and wait for 5 seconds (overkill but you never know with bad internet/lots of traffic)
const collector = new Discord.MessageCollector(msg.channel, m => m.author.id === myID, { time: 5000 });
collector.on('collect', message => {
console.log(message.content);
collector.stop("Got my message");
});
} else {
console.log(client.user.lastMessage.content);
}
Exact code Block I tested with:
client.on('message', msg => {
if (msg.content === '$ping') {
msg.reply("Pong!")
if (client.user.lastMessage == null) {
const collector = new Discord.MessageCollector(msg.channel, m => m.author.id === client.user.id, { time: 10000 });
collector.on('collect', message => {
console.log(message.content);
collector.stop("Got my message");
})
} else {
console.log(client.user.lastMessage.content);
}
}
}
You code snippet is obsolete now. From Discord.js v13 and thereafter the following properties have been removed (including lastMessage) because they are not provided by Discord and are not reliable
- GuildMember#lastMessage
- GuildMember#lastMessageId
- GuildMember#lastMessageChannelId
You have to implement a custom solution to track this information manually.
本文标签: javascriptHow do I Get the Client User39s Last MessageStack Overflow
版权声明:本文标题:javascript - How do I Get the Client User's Last Message? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742239575a2438710.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论