Search code examples
discordinvite

Inviteblock Discord.js


I want to create a bot that blocks invitations.
this is my code

bot.on("message", async message => {
    const regex = /(https?:\/\/)?(www\.)?(discord\.(gg|io|me|li|club)|discordapp\.com\/invite|discord\.com\/invite)\/.+[a-z]/gi;
    if (regex.exec(message.content)) 
        await message.channel.send(
          `${message.author} **você não pode postar link de outros servidores aqui!**\n Vais levar ban se voltares a`
        );
    
  });

The problem is that this also blocks the adm invites.


Solution

  • You need to check if the user has the admin role.

    bot.on("message", async message => {
        const regex = /(https?:\/\/)?(www\.)?(discord\.(gg|io|me|li|club)|discordapp\.com\/invite|discord\.com\/invite)\/.+[a-z]/gi;
    
        if (!message.member.roles.cache.has('xxxxx')) {  //replace the Xs with the admin role ID
            if (regex.exec(message.content)) {
               await message.channel.send(
                  `${message.author} **você não pode postar link de outros servidores aqui!**\n Vais levar ban se voltares a`
               )
            }
        } 
    });