antispam.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import discord
  2. from discord.ext import commands
  3. from discord.commands import Option
  4. from discord.commands import slash_command
  5. import configparser
  6. import time
  7. class antispam(commands.Cog):
  8. def __init__(self, bot: discord.Bot):
  9. self.bot = bot
  10. @commands.Cog.listener()
  11. async def on_message(self, message):
  12. if message.author.bot:
  13. return
  14. config = configparser.ConfigParser()
  15. config.read('config.cfg')
  16. spamindex = int(config.get('Moderation', 'Spam_Sensitivity_Index').split('#')[0].strip())
  17. message_content = message.content.lower()
  18. team_role_ids = [int(role_id) for role_id in config.get('Moderation', 'Mod_role_IDs').split(',') if role_id.strip().isdigit()]
  19. author_roles = [role.id for role in message.author.roles]
  20. if any(role_id in author_roles for role_id in team_role_ids):
  21. return
  22. current_time = time.time()
  23. if not hasattr(self, 'user_message_times'):
  24. self.user_message_times = {}
  25. if message.author.id not in self.user_message_times:
  26. self.user_message_times[message.author.id] = []
  27. self.user_message_times[message.author.id].append(current_time)
  28. self.user_message_times[message.author.id] = [t for t in self.user_message_times[message.author.id] if current_time - t <= 10]
  29. if len(self.user_message_times[message.author.id]) > spamindex:
  30. try:
  31. await message.delete()
  32. warning_msg = f"{message.author.mention}, you are sending messages too quickly. Please slow down."
  33. await message.channel.send(warning_msg, delete_after=5)
  34. except discord.Forbidden:
  35. print("Missing permissions to delete messages.")
  36. def setup(bot: discord.Bot):
  37. bot.add_cog(antispam(bot))