importtoxicityastox#this is a file called toxicity.py, which contains the toxicity function that allows you to check if a message is toxic or not (it uses the perspective api)
classModeration(discord.Cog):
def__init__(self,bot:discord.Bot)->None:
super().__init__()
self.bot=bot
@discord.slash_command(name="moderation",description="Enable or disable AI moderation & set the rules")
@discord.option(name="enable",description="Enable or disable AI moderation",reqired=True,)
@discord.option(name="log_channel",description="The channel where the moderation logs will be sent",required=True)
@discord.option(name="moderator_role",description="The role of the moderators",required=True)
#we set the default permissions to the administrator permission, so only the server administrators can use this command
#we also do that with the manage_messages permission, so the moderators can't be moderated
ifmessage.author.guild_permissions.manage_messages:return#if the user is a moderator, we don't want to moderate him because he is allowed to say whatever he wants because he is just like a dictator
ifmessage.author.guild_permissions.administrator:return#if the user is an administrator, we don't want to moderate him because he is allowed to say whatever he wants because he is a DICTATOR
ifnotis_enabled:return
content=message.content
message_toxicity=tox.get_toxicity(content)
ifmessage_toxicity>=0.40:
awaitmessage.delete()
embed=discord.Embed(title="Message deleted",description=f"{message.author.mention} Your message was deleted because it was too toxic. Please keep this server safe and friendly. If you think this was a mistake, please contact a moderator.",color=discord.Color.red())
embed=discord.Embed(title="Message deleted",description=f"The message \n***{content}***\n of {message.author.mention} sent in {message.channel.mention} on date **{formatted_message_sent_date}** was deleted because it was too toxic. The toxicity score was of **{message_toxicity}**",color=discord.Color.red())
awaitchannel.send(embed=embed)
elif0.37<message_toxicity<0.40:#if the message is not toxic, but it is close to being toxic, we send a warning
embed=discord.Embed(title="Possible toxic message",description=f"A possible [toxic message: **{content}**]({message.jump_url}) was sent by {message.author.mention} in {message.channel.mention}. Please check it out.",color=discord.Color.orange())
awaitchannel.send(embed=embed)
#we also reac with an orange circle emoji to the message
awaitmessage.add_reaction("🟠")
#we reply to the message with a ping to the moderators