async def mute( self, ctx: commands.Context, member: discord.Member, *, time: UserFriendlyTime(default='No reason', assume_reason=True) = None ) -> None: """Mutes a user""" if get_perm_level(member, await self.bot.db.get_guild_config( ctx.guild.id))[0] >= get_perm_level( ctx.author, await self.bot.db.get_guild_config(ctx.guild.id ))[0]: await ctx.send('User has insufficient permissions') else: duration = None reason = None if not time: duration = None else: if time.dt: duration = time.dt - ctx.message.created_at if time.arg: reason = time.arg await self.alert_user(ctx, member, reason, duration=format_timedelta(duration)) await self.bot.mute(ctx.author, member, duration, reason=reason) if ctx.author != ctx.guild.me: await ctx.send(self.bot.accept)
async def add(self, ctx: commands.Context, member: MemberOrID, *, note): """Add a note""" if get_perm_level(member, await self.bot.db.get_guild_config( ctx.guild.id))[0] >= get_perm_level( ctx.author, await self.bot.db.get_guild_config(ctx.guild.id ))[0]: await ctx.send('User has insufficient permissions') else: guild_data = await self.bot.db.get_guild_config(ctx.guild.id) notes = guild_data.notes guild_config = await self.bot.db.get_guild_config(ctx.guild.id) current_date = ( ctx.message.created_at + timedelta(hours=guild_config.time_offset)).strftime('%Y-%m-%d') if len(notes) == 0: case_number = 1 else: case_number = notes[-1]['case_number'] + 1 push = { 'case_number': case_number, 'date': current_date, 'member_id': str(member.id), 'moderator_id': str(ctx.author.id), 'note': note } await self.bot.db.update_guild_config(ctx.guild.id, {'$push': { 'notes': push }}) await ctx.send(self.bot.accept)
async def unmute(self, ctx: commands.Context, member: discord.Member, *, reason: CannedStr = 'No reason') -> None: """Unmutes a user""" if get_perm_level(member, await self.bot.db.get_guild_config( ctx.guild.id))[0] >= get_perm_level( ctx.author, await self.bot.db.get_guild_config(ctx.guild.id ))[0]: await ctx.send('User has insufficient permissions') else: await self.alert_user(ctx, member, reason) await self.bot.unmute(ctx.guild.id, member.id, None, reason=reason) await ctx.send(self.bot.accept)
async def ban(self, ctx: commands.Context, member: MemberOrID, *, reason: str = None) -> None: """Swings the banhammer""" if get_perm_level(member, await self.bot.db.get_guild_config( ctx.guild.id))[0] >= get_perm_level( ctx.author, await self.bot.db.get_guild_config(ctx.guild.id ))[0]: await ctx.send('User has insufficient permissions') else: await self.alert_user(ctx, member, reason) await ctx.guild.ban(member, reason=reason) await ctx.send(self.bot.accept) await self.send_log(ctx, member, reason)
async def unban( self, ctx: commands.Context, member: MemberOrID, *, time: UserFriendlyTime(default='No reason', assume_reason=True) = None ) -> None: """Unswing the banhammer""" if get_perm_level(member, await self.bot.db.get_guild_config( ctx.guild.id))[0] >= get_perm_level( ctx.author, await self.bot.db.get_guild_config(ctx.guild.id ))[0]: await ctx.send('User has insufficient permissions') else: duration = None reason = None if not time: duration = None else: if time.dt: duration = time.dt - ctx.message.created_at if time.arg: reason = time.arg if duration is None: try: await ctx.guild.unban(member, reason=reason) except discord.NotFound as e: await ctx.send(f'Unable to unban user: {e}') else: await ctx.send(self.bot.accept) await self.send_log(ctx, member, reason) else: await ctx.send(self.bot.accept) seconds = duration.total_seconds() seconds += unixs() await self.bot.db.update_guild_config(ctx.guild.id, { '$push': { 'tempbans': { 'member': str(member.id), 'time': seconds } } }) self.bot.loop.create_task( self.bot.unban(ctx.guild.id, member.id, seconds))
async def kick(self, ctx: commands.Context, member: discord.Member, *, reason: CannedStr = None) -> None: """Kicks a user""" if get_perm_level(member, await self.bot.db.get_guild_config( ctx.guild.id))[0] >= get_perm_level( ctx.author, await self.bot.db.get_guild_config(ctx.guild.id ))[0]: await ctx.send('User has insufficient permissions') else: await self.alert_user(ctx, member, reason) await member.kick(reason=reason) if ctx.author != ctx.guild.me: await ctx.send(self.bot.accept) await self.send_log(ctx, member, reason)
async def ban( self, ctx: commands.Context, member: MemberOrID, *, time: UserFriendlyTime(default='No reason', assume_reason=True) = None ) -> None: """Swings the banhammer""" if get_perm_level(member, await self.bot.db.get_guild_config( ctx.guild.id))[0] >= get_perm_level( ctx.author, await self.bot.db.get_guild_config(ctx.guild.id ))[0]: await ctx.send('User has insufficient permissions') else: duration = None reason = None if not time: duration = None else: if time.dt: duration = time.dt - ctx.message.created_at if time.arg: reason = time.arg await self.alert_user(ctx, member, reason) await self.send_log(ctx, member, reason, duration) await ctx.guild.ban(member, reason=reason) if ctx.author != ctx.guild.me: await ctx.send(self.bot.accept) # log complete, save to DB if duration is not None: seconds = duration.total_seconds() seconds += unixs() await self.bot.db.update_guild_config(ctx.guild.id, { '$push': { 'tempbans': { 'member': str(member.id), 'time': seconds } } }) self.bot.loop.create_task( self.bot.unban(ctx.guild.id, member.id, seconds))
async def softban(self, ctx: commands.Context, member: discord.Member, *, reason: CannedStr = None) -> None: """Bans then immediately unbans user (to purge messages)""" if get_perm_level(member, await self.bot.db.get_guild_config( ctx.guild.id))[0] >= get_perm_level( ctx.author, await self.bot.db.get_guild_config(ctx.guild.id ))[0]: await ctx.send('User has insufficient permissions') else: await self.alert_user(ctx, member, reason) await member.ban(reason=reason) await asyncio.sleep(2) await member.unban(reason=reason) await ctx.send(self.bot.accept) await self.send_log(ctx, member, reason)
async def check_perm_level(ctx: commands.Context, *, command_level: int=None) -> bool: guild_config = await ctx.bot.db.get_guild_config(ctx.guild.id) if isinstance(ctx.author, discord.Member): perm_level = get_perm_level(ctx.author, guild_config)[0] else: perm_level = 10 cmd_level = command_level or get_command_level(ctx.command, guild_config) if not perm_level >= cmd_level: raise Underleveled(f"User's level ({perm_level}) is not enough for the command's required level ({cmd_level})") return True
async def mylevel(self, ctx: commands.Context) -> None: """Checks your permission level""" perm_level = get_perm_level(ctx.author, await self.bot.db.get_guild_config(ctx.guild.id)) await ctx.send(f'You are on level {perm_level[0]} ({perm_level[1]})')
async def add_(self, ctx: commands.Context, member: MemberOrID, *, reason: CannedStr) -> None: """Warn a user Can also be used as `warn <member> [reason]`""" if get_perm_level(member, await self.bot.db.get_guild_config( ctx.guild.id))[0] >= get_perm_level( ctx.author, await self.bot.db.get_guild_config(ctx.guild.id ))[0]: await ctx.send('User has insufficient permissions') else: guild_config = await self.bot.db.get_guild_config(ctx.guild.id) guild_warns = guild_config.warns warn_punishments = guild_config.warn_punishments warn_punishment_limits = [i.warn_number for i in warn_punishments] warns = list( filter(lambda w: w['member_id'] == str(member.id), guild_warns)) cmd = None punish = False num_warns = len(warns) + 1 fmt = f'You have been warned in **{ctx.guild.name}**, reason: {reason}. This is warning #{num_warns}.' if warn_punishments: punishments = list( filter(lambda x: int(x) == num_warns, warn_punishment_limits)) if not punishments: punish = False above = list( filter(lambda x: int(x) > num_warns, warn_punishment_limits)) if above: closest = min(map(int, above)) cmd = warn_punishments.get_kv('warn_number', closest).punishment if cmd == 'ban': cmd = 'bann' if cmd == 'mute': cmd = 'mut' fmt += f' You will be {cmd}ed on warning {closest}.' else: punish = True punishment = warn_punishments.get_kv( 'warn_number', max(map(int, punishments))) cmd = punishment.punishment if cmd == 'ban': cmd = 'bann' if cmd == 'mute': cmd = 'mut' fmt += f' You have been {cmd}ed from the server.' try: await member.send(fmt) except discord.Forbidden: if ctx.author != ctx.guild.me: await ctx.send( 'The user has PMs disabled or blocked the bot.') finally: guild_config = await self.bot.db.get_guild_config(ctx.guild.id) current_date = (ctx.message.created_at + timedelta( hours=guild_config.time_offset)).strftime('%Y-%m-%d') if len(guild_warns) == 0: case_number = 1 else: case_number = guild_warns[-1]['case_number'] + 1 push = { 'case_number': case_number, 'date': current_date, 'member_id': str(member.id), 'moderator_id': str(ctx.author.id), 'reason': reason } await self.bot.db.update_guild_config( ctx.guild.id, {'$push': { 'warns': push }}) if ctx.author != ctx.guild.me: await ctx.send(self.bot.accept) await self.send_log(ctx, member, reason, case_number) # apply punishment if punish: if cmd == 'bann': cmd = 'ban' if cmd == 'mut': cmd = 'mute' ctx.command = self.bot.get_command(cmd) ctx.author = ctx.guild.me if punishment.get('duration'): time = UserFriendlyTime(default=False) time.dt = ctx.message.created_at + timedelta( seconds=punishment.duration) time.arg = f'Hit warn limit {num_warns}' kwargs = {'time': time} else: kwargs = {'reason': f'Hit warn limit {num_warns}'} await ctx.invoke(ctx.command, member, **kwargs)