I don't know how to do link cooodowns for my bot

Hey, I’m making a bot to moderate my Discord Server which lets people advertise their Server. However I need a cooldown which makes it so that people can only send a link every 3 hours. I’ve been looking everywhere and I can’t find a tutorial. Please can someone help me?

Hello @Gamerholic,

I found this question on Stackoverflow which looks like it shows how to make a working cooldown. Hope that helps!

I want a link cooldown though, not a command cooldown.

Hey @Gamerholic, just invite links or all website links?

I’m looking to remove invite links.

Are you using Discord.JS, Discord.PY, which one?

If you’re using Discord.JS, try this code from StackOverflow I edited:

bot.on('message', (message) => { // Whenever a message is sent
  if (message.content.includes('discord.gg/' || 'discordapp.com/invite/' || 'discord.io/' || 'discord.me/' )) { //if it contains an invite link
    message.delete() // Delete the message
      .then(message.channel.send('Link Deleted:\n**Invite links are not permitted on this server**'))
      // Do whatever you want here, punishment, nothing at all, etc.
  }
})

Note that you don’t have to use bot.on, if you have a command handler you could incorporate this into your message event (If you have one).

I’m using discord.js. I’m looking to remove invite links if they have already posted one in the last 3 hours.

@Gamerholic I’ve just made a code for you that I tested and it works

const adTimeout = new Set();

client.on('message', message => {
	if (message.content.includes('discord.gg')) {
		if (adTimeout.has(message.author.id)) {
			message.reply('3 Hours has not past since the last time you advertised!');
			message.delete();
		}
		else {
			adTimeout.add(message.author.id);
			setTimeout(() => {
				adTimeout.delete(message.author.id);
			}, 10800000);
		}
	}
});

The way it works is that it gets triggered whenever a message is sent, then it checks if the messages includes discord.gg, you can change that to whatever you want, then it checks if the author’s id is in the adTimeout constant that was made at the top of the code. If it is, it will reply to the author telling them they have already advertised within the last 3 hours. If it’s not, it will allow them to advertise and it will add them to it, and then it starts a timeout on 10800000 milli seconds, which is 3 hours, and then after that timeout, it will remove the author’s UserID from the collection and the member can then advertise once again.
Here is a screenshot from the testing
image

2 Likes

Thanks so much, it works!!

2 Likes

No problem, if you need some more help with other things, let me know and I will try to help you by explaining how things work in it.