1

My bot is supposed to delete any discord link that's send in any message on a server except the own Server link. What i've currently achieved is that every link with the content ''discord.gg'' gets deleted and logged so i can see the message content via the console, but i don't know how to whitelist one specific link.

my current code (without my failed attempts):

@bot.event
async def on_message(message):
    print("The message's content was", message.content)
    await bot.process_commands(message)
    msg_cnt = message.content.lower()
    if "discord.gg" in msg_cnt:
        await message.delete()
        print('deleted link:', message.content)

Does someone know a way how to ignore one (or two) specific discord invite links?

I tried to make a second command so the bot would post the link if needet but that isn't the point, it shouldn't delete messages which contains a specific (whitelistet) link.

the attempt (you can ignore that one):

@bot.command()
async def test(ctx):
    '''
    posts test Server link # text for the help command
    '''
    
    await ctx.send("discord.gg/test")

that didn't achieve what i wanted.

Robbe__1
  • 13
  • 3

2 Answers2

0

I think this is doable using fetch_invite. You'd have to parse the message content to get the invite link though. Perhaps looking at regex.

message_content = message.content.lower()
if "discord.gg" in message_content:
    url = # parse the message content to get the invite link
    try:
        invite = await bot.fetch_invite(url)
    except (ValueError, discord.NotFound):
        # invite is invalid
        return
    delete_message = False
    if type(invite) in [discord.PartialInviteGuild, discord.PartialInviteChannel]:
        if invite.id != message.guild.id:
            delete_message = True
    else:
        if invite.guild.id != message.guild.id:
            delete_message = True
    if delete_message:
        await message.delete()
        print("deleted link:", message.content)
    else:
        print("link is for our guild")

I think this would work. If it's not a valid invite (expired or bad link) then it will raise an error which we can catch and return on. Otherwise, if the invite is for a server your bot isn't in, then it gets a discord.PartialInviteGuild type or a discord.PartialInviteChannel type. You can use the id attribute on those to get the guild ID of the server it's an invite for. If it is an invite for a server your bot is in, then we have to use the guild property on the returned Invite type to get the guild ID.

OR, you just maintain a list of the invites for your server (and restrict creation to yourself) and don't delete invites that match the whitelisted ones. There's even a on_invite_create event you can listen to to track new invites created for your guild and add them dynamically to your whitelisted ones.

ESloman
  • 1,961
  • 1
  • 10
  • 14
0

This is what i use for my server. It will delete any invite links from any user that dont have the roles from allowed_roles and send an embed to log_channel, it will also delete messages that contains invite links after the edit of a message. Hope this works for you to.

allowed_roles = [3213131313, 23132132131]
log_channel = 21313213312

@client.event
async def on_message(message):
    if message.author.bot:
        return
    
    if any(invite_link in message.content for invite_link in ['discord.gg/', 'https://discord.gg/', 'discord.gg']): # Anti invite links
        author_roles = [role.id for role in message.author.roles]
        if not any(role in allowed_roles for role in author_roles):
            await message.delete()
            embed = discord.Embed(title=f"Invite link sent by {message.author.name}#{message.author.discriminator}", description=message.content, color=0xff0000)
            embed.add_field(name="Author", value=f"<@{message.author.id}>", inline=True)
            embed.add_field(name="Channel", value=f"{message.channel.mention}", inline=True)
            embed.set_footer(text=footer_text, icon_url=footer_url)
            await client.get_channel(log_channel).send(embed=embed)
            dm_channel = await message.author.create_dm()
            embed = discord.Embed(title="Warning", description=f"Advertising is not allowed in our server. Your message has been deleted and a proof has been sent to the log channel.\n\n Your message:\n {message.content}", color=0xff0000)
            embed.set_footer(text=footer_text, icon_url=footer_url)
            await dm_channel.send(embed=embed)

@client.event # Edited message logs
async def on_message_edit(before, after):
    author = after.author
    if "discord.gg" in after.content or "https://discord.gg/" in after.content or "discord.gg/" in after.content:
        has_role = False
        for role in author.roles:
            if role.id in allowed_roles:
                has_role = True
                break
        
        if not has_role:
            await after.delete()
            embed = discord.Embed(title="Message Deleted", description=f"A message from {author.mention} contained an invite link after editing and was deleted.\n\nOriginal message:\n{before.content}\nEdited message: \n{after.content}", color=discord.Color.red())
            embed.set_footer(text=footer_text, icon_url=footer_url)
            await client.get_channel(log_channel).send(embed=embed)
            dm = await after.author.create_dm()
            embed = discord.Embed(title="Warning", description=f"Advertising is not allowed in our server. Your message after editing has been deleted and a proof has been sent to the log channel.\n\nYour message:\n{after.content}", color=discord.Color.red())
            embed.set_footer(text=footer_text, icon_url=footer_url)
            await dm.send(embed=embed)
    else:
        has_role = False
        for role in author.roles:
            if role.id in allowed_roles:
                has_role = True
                break
        if not has_role:
            embed = discord.Embed(title="Message Edited", description=f"Original message sent by {author.mention}:\n{before.content}\nEdited message:\n{after.content}", color=discord.Color.red())
            embed.set_footer(text=footer_text, icon_url=footer_url)
            await client.get_channel(log_channel).send(embed=embed)

GFT
  • 63
  • 7