class MemberIsWhiteListed(unittest.TestCase): blacklisted = mongomock.MongoClient().db.blacklist blocker_service = BlockerService(blacklisted) member = Member('1', '1') def test_whitelisted_correctly(self): self.blocker_service.blacklist(self.member) assert self.blocker_service.find_member(self.member) is not None self.blocker_service.whitelist(self.member) assert self.blocker_service.find_member(self.member) is None
class MemberIsBlackListed(unittest.TestCase): blocker_source = mongomock.MongoClient().db.blacklist blocker_service = BlockerService(blocker_source) karma_source = mongomock.MongoClient().db.karma karma_service = KarmaMemberService(karma_source) member = Member("1", "1") def test_member_added_to_blacklist(self): self.blocker_service.blacklist(self.member) assert self.blocker_service.find_member(self.member) is not None
def __init__(self, bot, karma_service=KarmaMemberService(datasource.karma), blocker_service=BlockerService(datasource.blacklist)): self.bot = bot self.karma_service = karma_service self.blocker_service = blocker_service # this creates a dictionary where each value is a dictionary whose values are a list # with lambda this automatically creates an empty list on non existing keys self._members_on_cooldown = defaultdict(lambda: defaultdict(list)) self._running_timers = defaultdict( lambda: defaultdict(lambda: defaultdict()))
def __init__(self, bot): self.bot = bot self.blocker_service = BlockerService(datasource.blacklist)
class KarmaBlocker(commands.Cog): # Class about blocking and unblocking members from giving karma def __init__(self, bot): self.bot = bot self.blocker_service = BlockerService(datasource.blacklist) @guild_only() @has_required_role(command_name="blacklist") @commands.command( brief="blacklists a member from giving karma", usage="{}blacklist member_id\n{}blacklist <@!member_id>".format( config["prefix"], config["prefix"]), ) async def blacklist(self, ctx, *, args: str) -> None: """ blacklist the users provided to the command, both mentions and ids are valid. :param ctx: context of the invocation :param args: members provided to blacklist :return: None """ member_set = await convert_content_to_member_set(ctx, args.split()) for member in member_set: if member_has_role( member, roles()["admin"]) and ctx.message.author.id != int( config["owner"]): await ctx.channel.send("You cannot blacklist an admin.") else: if member_has_role( member, roles()["moderator"]) and not member_has_role( ctx.message.author, roles()["admin"]): await ctx.channel.send( "Only admins can blacklist an moderator.") else: self.blocker_service.blacklist( Member(ctx.guild.id, member.id)) await ctx.channel.send("Blacklisted {}".format( member.mention)) @guild_only() @has_required_role(command_name="whitelist") @commands.command( brief="removes existing blacklist of the guild member", usage="{}whitelist member_id\n{}whitelist <@!member_id>".format( config["prefix"], config["prefix"]), ) async def whitelist(self, ctx, *, args: str) -> None: """ whitelist the users provided to the command, both mentions and ids are valid. :param ctx: context of the invocation :param args: members provided to whitelist :return: None """ member_set = await convert_content_to_member_set(ctx, args.split()) for member in member_set: if member_has_role( member, roles()["admin"]) and ctx.message.author.id != int( config["owner"]): await ctx.channel.send("You cannot whitelist an admin.") else: if member_has_role( member, roles()["moderator"]) and not member_has_role( ctx.message.author, roles()["admin"]): await ctx.channel.send( "Only admins can whitelist an moderator.") else: self.blocker_service.whitelist( Member(ctx.guild.id, member.id)) await ctx.channel.send("Whitelisted {}".format( member.mention)) @guild_only() @has_required_role(command_name="showblacklist") @commands.command( name="showblacklist", brief= "list all blacklisted members in the guild the command was invoked in", usage="{}showblackist".format(config["prefix"], config["prefix"]), ) async def show_blacklist(self, ctx) -> None: """ prints out the blacklist in the channel, if message is over a certain max size the blacklist is embedded into a text file. :param ctx: context of the invocation :return: None """ blacklist = list( self.blocker_service.find_all_blacklisted(str(ctx.guild.id))) return_message = "" for blacklisted in blacklist: member = ctx.guild.get_member(int(blacklisted["member_id"])) if member is not None: return_message += (member.name + "#" + member.discriminator + " :: " + str(member.id) + "\n") else: return_message += return_message + str( blacklisted["member_id"]) + "\n" if len(return_message) != 0: if len(return_message) <= max_message_length: await ctx.channel.send(return_message) else: await ctx.channel.send( file=File(fp=BytesIO(bytes(return_message, "utf-8")), filename="Blacklist")) else: await ctx.channel.send("Blacklist is empty")
class KarmaBlocker(commands.Cog): # Class about blocking and unblocking members from giving karma def __init__(self, bot): self.bot = bot self.blocker_service = BlockerService(datasource.blacklist) @guild_only() @has_required_role(command_name='blacklist') @commands.command(brief='blacklists a member from giving karma', usage='{}blacklist member_id\n{}blacklist <@!member_id>' .format(config['prefix'], config['prefix'])) async def blacklist(self, ctx, *, args: str) -> None: """ blacklist the users provided to the command, both mentions and ids are valid. :param ctx: context of the invocation :param args: members provided to blacklist :return: None """ member_set = await convert_content_to_member_set(ctx, args.split()) for member in member_set: if member_has_role(member, roles()['admin']) and ctx.message.author.id != int(config['owner']): await ctx.channel.send(f'Skipping {member.display_name}, You cannot blacklist an admin.') continue if member_has_role(member, roles()['moderator']) \ and not member_has_role(ctx.message.author, roles()['admin']): await ctx.channel.send(f'Skipping {member.display_name}, Only admins can blacklist an moderator.') continue self.blocker_service.blacklist(Member(ctx.guild.id, member.id)) await ctx.channel.send(f'Blacklisted {member.mention} from giving karma.') @guild_only() @has_required_role(command_name='whitelist') @commands.command(brief='removes existing blacklist of the guild member', usage='{}whitelist member_id\n{}whitelist <@!member_id>' .format(config['prefix'], config['prefix'])) async def whitelist(self, ctx, *, args: str) -> None: """ whitelist the users provided to the command, both mentions and ids are valid. :param ctx: context of the invocation :param args: members provided to whitelist :return: None """ member_set = await convert_content_to_member_set(ctx, args.split()) for member in member_set: if member_has_role(member, roles()['admin']) and ctx.message.author.id != int(config['owner']): await ctx.channel.send(f'Skipping {member.display_name}, You cannot whitelist an admin.') continue if member_has_role(member, roles()['moderator']) \ and not member_has_role(ctx.message.author, roles()['admin']): await ctx.channel.send(f'Skipping {member.display_name}, Only admins can whitelist an moderator.') self.blocker_service.whitelist(Member(ctx.guild.id, member.id)) await ctx.channel.send(f'Whitelisted {member.mention}') @guild_only() @has_required_role(command_name='showblacklist') @commands.command(name='showblacklist', brief='list all blacklisted members in the guild the command was invoked in', usage='{}showblackist' .format(config['prefix'], config['prefix'])) async def show_blacklist(self, ctx) -> None: """ prints out the blacklist in the channel, if message is over a certain max size the blacklist is embedded into a text file. :param ctx: context of the invocation :return: None """ blacklist = list(self.blocker_service.find_all_blacklisted(str(ctx.guild.id))) return_message = '' for blacklisted in blacklist: member = ctx.guild.get_member(int(blacklisted['member_id'])) if member is not None: return_message += member.name + '#' + member.discriminator + ' :: ' return_message += member.id + '\n' if len(return_message) == 0: await ctx.channel.send('Blacklist is empty') return if len(return_message) > max_message_length: await ctx.channel.send(file=File(fp=BytesIO(bytes(return_message, 'utf-8')), filename='Blacklist')) return await ctx.channel.send(return_message)