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)