async def test_getGuildValidInterval(self): ast = AntiSpamTracker(AntiSpamHandler(get_mocked_bot()), 3) self.assertEqual(ast._get_guild_valid_interval(123456789), 30000) ast.update_cache(get_mocked_message(), {"should_be_punished_this_message": True}) self.assertEqual(ast._get_guild_valid_interval(123456789), 30000) ast.user_tracking[123456789]["valid_interval"] = 15000 self.assertEqual(ast._get_guild_valid_interval(123456789), 15000)
async def test_initialization(self): # Test AntiSpamHandler type assertion with self.assertRaises(TypeError): # noinspection PyTypeChecker AntiSpamTracker(1, 2) AntiSpamTracker(AntiSpamHandler(get_mocked_bot()), 3) with self.assertRaises(TypeError): # noinspection PyArgumentList AntiSpamTracker() ash = AntiSpamHandler(get_mocked_bot()) message_interval = ash.options.get("message_interval") ast = AntiSpamTracker(ash, 3) self.assertEqual(message_interval, ast.valid_global_interval) ast = AntiSpamTracker(ash, 3, 15000) self.assertNotEqual(message_interval, ast.valid_global_interval) self.assertEqual(15000, ast.valid_global_interval) self.assertEqual(False, bool(ast.user_tracking)) self.assertEqual(ast.punish_min_amount, 3) with self.assertRaises(TypeError): # noinspection PyTypeChecker AntiSpamTracker(ash, 3, dict())
async def test_cleanCache(self): ast = AntiSpamTracker(AntiSpamHandler(get_mocked_bot()), 3, 50) ast.update_cache(get_mocked_message(), {"should_be_punished_this_message": True}) self.assertEqual(1, len(ast.user_tracking[123456789][12345])) ast.clean_cache() self.assertEqual(len(ast.user_tracking), 1) self.assertEqual(1, len(ast.user_tracking[123456789][12345])) await asyncio.sleep(0.06) # This should now fully clean the cache ast.clean_cache() self.assertEqual(ast.user_tracking, dict())
import discord from discord.ext import commands from AntiSpam import AntiSpamHandler, AntiSpamTracker from jsonLoader import read_json bot = commands.Bot(command_prefix="!", intents=discord.Intents.all()) file = read_json("token") bot.handler = AntiSpamHandler(bot) bot.tracker = AntiSpamTracker(bot.handler, 3) @bot.event async def on_ready(): # On ready, print some details to standard out print(f"-----\nLogged in as: {bot.user.name} : {bot.user.id}\n-----") @bot.event async def on_message(message): data = await bot.handler.propagate(message) bot.tracker.update_cache(message, data) if bot.tracker.is_spamming(message): # Insert code to mute the user # Insert code to tell admins # ETC
async def test_getUserCount(self): ast = AntiSpamTracker(AntiSpamHandler(get_mocked_bot()), 3) ast.update_cache(get_mocked_message(), {"should_be_punished_this_message": True}) user_count = ast.get_user_count(get_mocked_message()) self.assertEqual(user_count, 1) for i in range(3): ast.update_cache(get_mocked_message(), {"should_be_punished_this_message": True}) user_count = ast.get_user_count(get_mocked_message()) self.assertEqual(user_count, 4) with self.assertRaises(UserNotFound): message = get_mocked_message(guild_kwargs={"id": 15}) ast.get_user_count(message) with self.assertRaises(UserNotFound): message = get_mocked_message(member_kwargs={"id": 25}) ast.get_user_count(message)
async def test_updateCache(self): ast = AntiSpamTracker(AntiSpamHandler(get_mocked_bot()), 3) self.assertEqual(False, bool(ast.user_tracking)) ast.update_cache(get_mocked_message(), {"should_be_punished_this_message": True}) self.assertEqual(True, bool(ast.user_tracking)) ast.user_tracking = {} # overwrite so we can test more self.assertEqual(False, bool(ast.user_tracking)) ast.update_cache(get_mocked_message(), {"should_be_punished_this_message": False}) self.assertEqual(False, bool(ast.user_tracking)) with self.assertRaises(TypeError): # noinspection PyTypeChecker ast.update_cache(1, dict()) with self.assertRaises(TypeError): # noinspection PyTypeChecker ast.update_cache(get_mocked_message(), 1) ast.user_tracking = {} self.assertEqual(0, len(ast.user_tracking)) ast.update_cache(get_mocked_message(), {"should_be_punished_this_message": True}) ast.update_cache(get_mocked_message(), {"should_be_punished_this_message": True}) self.assertEqual(1, len(ast.user_tracking)) self.assertEqual(1, len(ast.user_tracking[123456789])) self.assertEqual(2, len(ast.user_tracking[123456789][12345]))
async def test_removePunishment(self): ast = AntiSpamTracker(AntiSpamHandler(get_mocked_bot()), 5) ast.remove_punishments(get_mocked_message()) ast.update_cache(get_mocked_message(), {"should_be_punished_this_message": True}) self.assertEqual(1, ast.get_user_count(get_mocked_message())) ast.remove_punishments(get_mocked_message()) with self.assertRaises(UserNotFound): ast.get_user_count(get_mocked_message())
async def test_isSpamming(self): ast = AntiSpamTracker(AntiSpamHandler(get_mocked_bot()), 5) self.assertEqual(ast.is_spamming(get_mocked_message()), False) ast.update_cache(get_mocked_message(), {"should_be_punished_this_message": True}) self.assertEqual(ast.is_spamming(get_mocked_message()), False) for i in range(3): ast.update_cache(get_mocked_message(), {"should_be_punished_this_message": True}) # Cap is 5, should have 4 messages rn self.assertEqual(ast.is_spamming(get_mocked_message()), False) self.assertEqual(4, len(ast.user_tracking[123456789][12345])) ast.update_cache(get_mocked_message(), {"should_be_punished_this_message": True}) self.assertEqual(ast.is_spamming(get_mocked_message()), True) ast.update_cache(get_mocked_message(), {"should_be_punished_this_message": True}) self.assertEqual(ast.is_spamming(get_mocked_message()), True)
async def test_removeOutdatedTimestamps(self): ast = AntiSpamTracker(AntiSpamHandler(get_mocked_bot()), 3, 50) ast.update_cache(get_mocked_message(), {"should_be_punished_this_message": True}) self.assertEqual(ast.get_user_count(get_mocked_message()), 1) ast.remove_outdated_timestamps(123456789, 12345) self.assertEqual(ast.get_user_count(get_mocked_message()), 1) await asyncio.sleep(0.06) ast.remove_outdated_timestamps(123456789, 12345) self.assertEqual(ast.get_user_count(get_mocked_message()), 0)