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

104
Visualizações
Check if someone sent the message twice

On my server, we have a channel for just one word, "Oi".

If someone sends something other than the word "Oi", it gets deleted. But now I need a code that deletes the message if someone sends it twice in a row. They have to wait for someone else to send if they want to send.

This is my current code if you want to check it out for some reason:

if (message.channel.id === "ChannelIdWhichImNotGonnaTell") {
  if (message.content === "Oi") {
    let log = client.channels.cache.get("ChannelIdWhichImNotGonnaTell")
    log.send(`New Oi By ${message.author.tag}`)
  } else {
    message.delete()
  }
}
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

There's some several work around for this. But I found this is the efficient way to do that.

As you said 'They have to wait for someone else to send oi' then you should fetch the "old message" before "the new message" that sent. and try to get the User ID. Then compare it with the new message one.

Here is the Example code :

if (message.content === 'Oi') {

    message.channel.messages.fetch({limit: 2})
    .then(map => {
    let messages = Array.from(map.values());
    let msg = messages[1];
    if (msg.author.id === message.author.id){

        message.delete();
        // do something

    } else {

       let log = client.channels.cache.get("ChannelID")
       log.send(`New Oi By ${message.author.tag}`)

    }}).catch(error => console.log("Error fetching messages in channel"));

}

It will compare the "old messages" author UserID with the "new messages" author UserID. If it's match. Then it will be deleted.

about 4 years ago · Juan Pablo Isaza Relatório

0

You can fetch() the last two messages by using the limit option and the last() in the returned collection will be the second last message in the channel (the one before the last one triggered your code).

Then you can compare the author's IDs; if they are the same, you can delete the message:

if (message.channel.id === oiChannelID) {
  if (message.content === 'Oi') {
    // fetch the last two messages
    // this includes this one and the previous one too
    let lastMessages = await message.channel.messages.fetch({ limit: 2 });
    // this is the message sent before the one triggered this command
    let previousMessage = lastMessages.last();

    if (previousMessage.author.id === message.author.id) {
      console.log('No two Ois, mate');
      message.delete();
      // don't execute the rest of the code
      return;
    }

    let log = client.channels.cache.get(logChannelID);
    log.send(`New Oi By ${message.author.tag}`);
  } else {
    message.delete();
  }
}

enter image description here

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