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