如何获取客户端用户的上一条消息?

时间:2017-12-25 16:18:38

标签: javascript node.js discord discord.js

你如何得到机器人最后一条消息的对象?我尝试过这样的事情:

class Program { static void Main(string[] args) { Console.WriteLine("Type any bastard letter!!!"); ConsoleKeyInfo keyInfo = Console.ReadKey(); char[] array1 = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' }; if (keyInfo.KeyChar == 'a') { SpeechSynthesizer synth = new SpeechSynthesizer(); Console.WriteLine("You have typed a letter"); synth.Speak("You have typed a letter"); } else { SpeechSynthesizer synth = new SpeechSynthesizer(); Console.WriteLine(" "); Console.WriteLine("did you type {0}", keyInfo.KeyChar.ToString()); synth.Speak("You have typed a bastard number you infentile pillock"); } } }

如果我触发条件,控制台会给我一个错误:if (message.content.split(' ')[0] === 'test') { message.channel.sendMessage('Test') console.log(client.user.lastMessage.content) }

1 个答案:

答案 0 :(得分:1)

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

为了环绕这一点,我要检查它是否为空(如果它不是),并且在 的情况下,请使用messagecolleter并等待你的消息要发送。

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