async def add_spam(self, ctx, *args): """Add a spam link to automatically jail a user if posted""" regex = ' '.join((x for x in args)) member = ctx.message.author async with async_session() as db: async with db.begin(): scd = SpamDAL(db) # check rule not already in database before adding. check_dupe = await scd.check_duplicate(regex) if check_dupe: await ctx.send( f'```❌ Sorry {member.name}, {regex} is already in spam database!```' ) return # commit new spam rule and return updated rule set rows = await scd.add_spam(member.id, regex) self.spam_dict = { rule.regex: re.compile(rule.regex, re.I) for rule in rows } embed = Embed( color=0x13DC51, title=f'New Phishing Rule Added', description=f'```✅ {regex}```', ) embed.set_footer(text=member.name, icon_url=member.display_avatar) await ctx.send(embed=embed)
async def construct_spam_dict(self): async with async_session() as db: async with db.begin(): scd = SpamDAL(db) rows = await scd.get_all_spam() self.spam_dict = { rule.regex: re.compile(rule.regex, re.I) for rule in rows }
async def update_regex_rule(self, ctx, _id, *args): """Update an existing spam rule by rule ID""" regex = ' '.join((x for x in args)) member = ctx.message.author async with async_session() as db: async with db.begin(): scd = SpamDAL(db) await scd.update_spam_rule(_id, member.id, regex) # reload spam dict on change self.reload_spam_dict() embed = Embed(color=0xA0F1B9, title=f'Rule {_id} | Updated By {member.name}', description=f'```✅ {regex}```') embed.set_footer(text=member.name, icon_url=member.display_avatar) await ctx.send(embed=embed)
async def list_rule_breakers(self, ctx): """Last 10 Rule Breakers and Rule Desc order""" async with async_session() as db: async with db.begin(): scd = SpammerDAL(db) rows = await scd.get_all_spammers() NUM_SPAM = 10 NUM_LEN = 10 all_spammers = [ f' {row.id} | {await self.client.fetch_user(row.member)} | {row.regex}' for row in rows ] response = [] for _ in range(len(all_spammers)): response.append('\n'.join(all_spammers[NUM_SPAM - NUM_LEN:NUM_SPAM])) NUM_SPAM += NUM_LEN for block in response: await ctx.send(f'```{"".join(block)}```' ) if len(block) > 0 else None
async def spam_added_by(self, ctx, _id: str): """Show who added spam link by ID""" member = ctx.message.author async with async_session() as db: async with db.begin(): scd = SpamDAL(db) row = await scd.spam_by_id(_id) if not row: await ctx.send( f'```❌ Sorry {member.name}, Rule: {_id} does not exist!```' ) return user = await self.client.fetch_user(row.member) embed = Embed( color=0x59E685, title=f'Rule {row.id} | Created By {user.name}', description=f'```Rule: {row.regex}```', ) embed.set_footer(text=user.name, icon_url=user.display_avatar) await ctx.send(embed=embed)
async def current_spam_list(self, ctx): """Lists all current items in the spam database""" async with async_session() as db: async with db.begin(): scd = SpamDAL(db) res = await scd.get_all_spam() NUM_SPAM = 25 NUM_LEN = 25 all_spam = [ f' {row.id} | {row.regex}' if row.id < 10 else f' {row.id} | {row.regex}' for row in res ] response = [] for _ in range(len(all_spam)): response.append('\n'.join(all_spam[NUM_SPAM - NUM_LEN:NUM_SPAM])) NUM_SPAM += NUM_LEN for block in response: await ctx.send(f'```{"".join(block)}```' ) if len(block) > 0 else None
async def remove_spam_item(self, ctx, _id: int): """Remove an item from spam list by its ID""" member = ctx.message.author async with async_session() as db: async with db.begin(): scd = SpamDAL(db) row = await scd.spam_by_id(_id) if not row: await ctx.send( f'```❌ Sorry {member.name}, cannot remove Rule {_id} it does not exist!```' ) return await scd.delete_spam(_id) # reload spam dict on item removal self.reload_spam_dict() embed = Embed(color=0xA0F1B9, title=f'Rule {row.id} | Removed By {member.name}', description=f'```❌ {row.regex}```') embed.set_footer(text=member.name, icon_url=member.display_avatar) await ctx.send(embed=embed)
async def remove_spammer_item(self, ctx, _id: int): """Remove an item from spam list by its ID""" member = ctx.message.author async with async_session() as db: async with db.begin(): scd = SpammerDAL(db) row = await scd.spammer_by_id(_id) if not row: await ctx.send( f'```❌ Sorry {member.name}, cannot remove spammer {_id} it does not exist!```' ) return await scd.delete_spammer(_id) embed = Embed( color=0xA0F1B9, title=f'Spammer {row.id} | Removed By {member.name}', description= f'```✅ {await self.client.fetch_user(row.member)} removed from list.```' ) embed.set_footer(text=member.name, icon_url=member.display_avatar) await ctx.send(embed=embed)
async def on_message(self, msg): member = msg.author if msg.author.bot: # Dont run on any bot messages return if isinstance(msg.channel, DMChannel): # Ignore DM return if self.client.user_is_admin(member): # Dont jail friends on after adding a new spam link return if self.spam_dict and msg.channel.id != self.JAIL_CHANNEL_ID: for regex_string, regex in self.spam_dict.items(): if regex.findall(msg.content): await self.send_to_jail(member, reason='Sent illegal spam') await self.post_spam_report(msg, regex_string) async with async_session() as db: async with db.begin(): scd = SpammerDAL(db) await scd.add_spammer(member=member.id, regex=regex_string) await msg.delete() break
async def get_book_dal(): async with async_session() as session: async with session.begin(): yield BookDAL(session)