Matt
Matt

Reputation: 29

(Discord Bot) Get message that follows command prefix

I'm trying to reference the previous message sent by a client using discord.js

How would I go about getting the last message sent?

client.on("messageCreate", (message) => {

    if (!message.content.startsWith(prefix) || message.author.bot) return;

    const args = message.content.slice(prefix.length).split(/ +/);
    const command = args.shift().toLowerCase();

    if (command === 'set-reminder'){
        channelName = client.channels.cache.find(channel => channel.name === "spam");

        message.channel.send("Command Works"); 
        channelName.messages.fetch({ limit: 1 }).then(messages => {
    // find "command works" message (previous message from client)
           let lastMessage = messages.first();
          
           console.log(lastMessage.content);
        })
    }
})```

Upvotes: 0

Views: 309

Answers (1)

Cyberflixt
Cyberflixt

Reputation: 36

The message.channel.send function is async

You can await the send function or use .then

The send function also returns the message, you don't need a query to get it

await

let message = await message.channel.send("Command Works");
console.log(message.content);

then

message.channel.send("Command Works").then((message) => {
   console.log(message.content);
});

(async means not-synchronous : you have to wait to get its result)

Upvotes: 1

Related Questions