Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

297
Visualizações
Fetch 2 last messages from a specific user discord.js

I'm trying to fetch the last 2 messages someone sent but I couldn't find how. I tried:

console.log(user.lastMessage) // to get the last one since i was desperate, i also tried lastMessageId

and

message.channel.messages.fetch({ author: message.author}).then(async (messages) => {
     console.log(messages.first(2)[1].content)
})

The first method would return undefined. The second method also returned messages from other users.

This is for my ranking system, I want to add a 15 seconds cooldown between each messages added to the database.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

By using message.channel.messages.fetch() you are getting a Collection with all the messages in the channel up to a certain point. But the thing is that, the Collection is in reverse which means that the newest messages are in the first while the older ones are at the end. So you just have to perform a .find() function to see the first message where the author was a specific person. An example would look like this =>

const messages = await message.channel.messages.fetch()
const userLastMessage = messages.find(msg => msg.author.id === message.author.id)
about 4 years ago · Juan Pablo Isaza Relatório

0

Fetching is absolutely not the way to do what you want to do, since you would fetch messages every single time someone sends a message. On top of that, there is actually no way to fetch messages from only one member (not user, users aren't members). The question then is: what happens if someone did not send a message in past 100 messages? Do you fetch another 100? What if they didn't speak in this channel yet or even never sent a message in the server before?

The correct way to do this is to store a timestamp of when someone sent a message, and when they send another message, compare it to your stored value to check if enough time has passed since the previous message.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda