最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do I Get the Client User's Last Message? - Stack Overflow

programmeradmin4浏览0评论

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

Share Improve this question edited Aug 30, 2020 at 6:29 geisterfurz007 5,8846 gold badges35 silver badges58 bronze badges asked Dec 25, 2017 at 16:18 MehMeh 1261 gold badge4 silver badges14 bronze badges 11
  • 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. The lastMessageID is also null. Is there an alternative way to set the client.user.lastMessage object since it doesn't appear to be in read-only mode? – Meh Commented Dec 25, 2017 at 16:41
 |  Show 6 more ments

2 Answers 2

Reset to default 3

The 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.

发布评论

评论列表(0)

  1. 暂无评论