gpt4 book ai didi

javascript - 如何获取客户端用户的最后一条消息?

转载 作者:行者123 更新时间:2023-11-30 14:52:01 25 4
gpt4 key购买 nike

您将如何获取机器人最后一条消息的对象?我尝试做类似的事情:

if (message.content.split(' ')[0] === 'test') {
message.channel.sendMessage('Test')
console.log(client.user.lastMessage.content)
}

如果我触发条件,控制台会给我一个错误:TypeError: Cannot read property 'content' of undefined

最佳答案

client.user.lastmessage 的值为null 的原因是因为您刚刚启动机器人,并且在您运行之前它没有发送任何消息你的 'test' 命令。

为了绕过这个,我会检查它是否为空(以防它不是),如果它,则使用 MessageCollector并等待您的消息发送。

    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);
}

我测试过的确切代码块:

    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);
}
}
}

关于javascript - 如何获取客户端用户的最后一条消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47970269/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com