Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

99
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!