async def on_member_ban(self, guild, user): if user.id == self.bot.user.id or not Features.is_logged(guild.id, "MOD_ACTIONS"): return fid = f"{guild.id}-{user.id}" if fid in self.bot.data["forced_exits"]: return if guild.me.guild_permissions.view_audit_log: async for entry in guild.audit_logs(action=AuditLogAction.ban, limit=25): if entry.target == user and entry.created_at > datetime.datetime.utcfromtimestamp(time.time() - 30): if entry.reason is None: reason = Translator.translate("no_reason", guild.id) else: reason = entry.reason Infraction.update(active=False).where((Infraction.user_id == user.id) & (Infraction.type == "Unban") & (Infraction.guild_id == guild.id)).execute() InfractionUtils.add_infraction(guild.id, entry.target.id, entry.user.id, "Ban", "No reason given." if entry.reason is None else entry.reason) GearbotLogging.log_to(guild.id, "MOD_ACTIONS", f":door: {Translator.translate('ban_log', guild.id, user=Utils.clean_user(user), user_id=user.id, moderator=Utils.clean_user(entry.user), moderator_id=entry.user.id, reason=reason)}") return GearbotLogging.log_to(guild.id, "MOD_ACTIONS", f":door: {Translator.translate('manual_ban_log', guild.id, user=Utils.clean_user(user), user_id=user.id)}") Infraction.update(active=False).where((Infraction.user_id == user.id) & (Infraction.type == "Unban") & (Infraction.guild_id == guild.id)).execute() self.bot.data["forced_exits"].add(fid)
async def unban(self, ctx, member: BannedMember, *, reason: Reason = ""): """unban_help""" if reason == "": reason = Translator.translate("no_reason", ctx.guild.id) fid = f"{ctx.guild.id}-{member.user.id}" self.bot.data["unbans"].add(fid) try: await ctx.guild.unban( member.user, reason=Utils.trim_message( f"Moderator: {ctx.author.name}#{ctx.author.discriminator} ({ctx.author.id}) Reason: {reason}", 500)) except Exception as e: self.bot.data["unbans"].remove(fid) raise e Infraction.update(active=False).where( (Infraction.user_id == member.user.id) & (Infraction.type == "Ban") & (Infraction.guild_id == ctx.guild.id)).execute() InfractionUtils.add_infraction(ctx.guild.id, member.user.id, ctx.author.id, "Unban", reason) await ctx.send( f"{Emoji.get_chat_emoji('YES')} {Translator.translate('unban_confirmation', ctx.guild.id, user=Utils.clean_user(member.user), user_id=member.user.id, reason=reason)}" ) GearbotLogging.log_to( ctx.guild.id, "MOD_ACTIONS", f"{Emoji.get_chat_emoji('INNOCENT')} {Translator.translate('unban_log', ctx.guild.id, user=Utils.clean_user(member.user), user_id=member.user.id, moderator=Utils.clean_user(ctx.author), moderator_id=ctx.author.id, reason=reason)}" )
async def on_member_unban(self, guild, user): fid = f"{guild.id}-{user.id}" if fid in self.bot.data["unbans"]: self.bot.data["unbans"].remove(fid) return elif not Features.is_logged(guild.id, "MOD_ACTIONS"): return Infraction.update(active=False).where((Infraction.user_id == user.id) & (Infraction.type == "Ban") & (Infraction.guild_id == guild.id)).execute() limit = datetime.datetime.utcfromtimestamp(time.time() - 60) log = await self.find_log(guild, AuditLogAction.unban, lambda e: e.target == user and e.created_at > limit) if log is None: # this fails way to often for my liking, alternative is adding a delay but this seems to do the trick for now log = await self.find_log(guild, AuditLogAction.unban, lambda e: e.target == user and e.created_at > limit) if log is not None: i = InfractionUtils.add_infraction(guild.id, user.id, log.user.id, "Unban", "Manual unban") GearbotLogging.log_key(guild.id, 'unban_log', user=Utils.clean_user(user), user_id=user.id, moderator=log.user, moderator_id=log.user.id, reason='Manual unban', inf=i.id) else: i = InfractionUtils.add_infraction(guild.id, user.id, 0, "Unban", "Manual ban") GearbotLogging.log_key(guild.id, 'manual_unban_log', user=Utils.clean_user(user), user_id=user.id, inf=i.id)
async def on_member_ban(self, guild, user): if user.id == self.bot.user.id or not Features.is_logged(guild.id, "MOD_ACTIONS"): return fid = f"{guild.id}-{user.id}" if fid in self.bot.data["forced_exits"]: return self.bot.data["forced_exits"].add(fid) Infraction.update(active=False).where((Infraction.user_id == user.id) & (Infraction.type == "Unban") & (Infraction.guild_id == guild.id)).execute() limit = datetime.datetime.utcfromtimestamp(time.time() - 60) log = await self.find_log(guild, AuditLogAction.ban, lambda e: e.target == user and e.created_at > limit) if log is None: await asyncio.sleep(1) #is the api having a fit or so? #this fails way to often for my liking, alternative is adding a delay but this seems to do the trick for now log = await self.find_log(guild, AuditLogAction.ban, lambda e: e.target == user and e.created_at > limit) if log is not None: if log.reason is None: reason = Translator.translate("no_reason", guild.id) else: reason = log.reason i = InfractionUtils.add_infraction(guild.id, log.target.id, log.user.id, "Ban", reason) GearbotLogging.log_key(guild.id, 'ban_log', user=Utils.clean_user(user), user_id=user.id, moderator=Utils.clean_user(log.user), moderator_id=log.user.id, reason=reason, inf=i.id) else: i = InfractionUtils.add_infraction(guild.id, user.id, 0, "Ban", "Manual ban") GearbotLogging.log_key(guild.id, 'manual_ban_log', user=Utils.clean_user(user), user_id=user.id, inf=i.id)
async def ban_punishment(self, v: Violation): reason = self.assemble_reason(v) self.bot.data["forced_exits"].add(f"{v.guild.id}-{v.member.id}") await v.guild.ban(v.member, reason=reason, delete_message_days=0) Infraction.update(active=False).where( (Infraction.user_id == v.member.id) & (Infraction.type == "Unban") & ( Infraction.guild_id == v.guild.id)).execute() i = InfractionUtils.add_infraction(v.guild.id, v.member.id, self.bot.user.id, 'Ban', reason) GearbotLogging.log_key(v.guild.id, 'ban_log', user=Utils.clean_user(v.member), user_id=v.member.id, moderator=Utils.clean_user(v.guild.me), moderator_id=v.guild.me.id, reason=reason, inf=i.id)
async def on_member_unban(self, guild, user): fid = f"{guild.id}-{user.id}" if fid in self.bot.data["unbans"]: self.bot.data["unbans"].remove(fid) return elif not Features.is_logged(guild.id, "MOD_ACTIONS"): return else: if guild.me.guild_permissions.view_audit_log: async for entry in guild.audit_logs(action=AuditLogAction.unban, limit=2): if entry.target == user and entry.created_at > datetime.datetime.utcfromtimestamp(time.time() - 30): Infraction.update(active=False).where((Infraction.user_id == user.id) & (Infraction.type == "Ban") & (Infraction.guild_id == guild.id)).execute() InfractionUtils.add_infraction(guild.id, entry.target.id, entry.user.id, "Unban", "Manual unban") GearbotLogging.log_to(guild.id, "MOD_ACTIONS", f":door: {Translator.translate('unban_log', guild.id, user=Utils.clean_user(user), user_id=user.id, moderator=entry.user, moderator_id=entry.user.id, reason='Manual unban')}") return GearbotLogging.log_to(guild.id, "MOD_ACTIONS", f"{Emoji.get_chat_emoji('INNOCENT')} {Translator.translate('manual_unban_log', guild.id, user=Utils.clean_user(user), user_id=user.id)}") Infraction.update(active=False).where((Infraction.user_id == user.id) & (Infraction.type == "Ban") & (Infraction.guild_id == guild.id)).execute()
async def _ban(self, ctx, user, reason, confirm): self.bot.data["forced_exits"].add(f"{ctx.guild.id}-{user.id}") await ctx.guild.ban( user, reason=Utils.trim_message( f"Moderator: {ctx.author.name}#{ctx.author.discriminator} ({ctx.author.id}) Reason: {reason}", 500), delete_message_days=0) Infraction.update(active=False).where( (Infraction.user_id == user.id) & (Infraction.type == "Unban") & (Infraction.guild_id == ctx.guild.id)).execute() InfractionUtils.add_infraction(ctx.guild.id, user.id, ctx.author.id, "Ban", reason) GearbotLogging.log_to( ctx.guild.id, "MOD_ACTIONS", f":door: {Translator.translate('ban_log', ctx.guild.id, user=Utils.clean_user(user), user_id=user.id, moderator=Utils.clean_user(ctx.author), moderator_id=ctx.author.id, reason=reason)}" ) if confirm: await GearbotLogging.send_to(ctx, "YES", "ban_confirmation", user=Utils.clean_user(user), user_id=user.id, reason=reason)
from database.DatabaseConnector import Infraction connection = MySQLDatabase( Configuration.get_master_var("DATABASE_NAME"), user=Configuration.get_master_var("DATABASE_USER"), password=Configuration.get_master_var("DATABASE_PASS"), host=Configuration.get_master_var("DATABASE_HOST"), port=Configuration.get_master_var("DATABASE_PORT"), use_unicode=True, charset="utf8mb4") #make connection migrator = MySQLMigrator(connection) #run everything in a transaction so we don't turn the database into 💩 if something goes wrong with connection.atomic(): #fields to add end = TimestampField(null=True) active = BooleanField(default=True) #add fields migrate( migrator.add_column("infraction", "end", end), migrator.add_column("infraction", "active", active), migrator.rename_column("infraction", "timestamp", "start"), ) #some infractions are not active anymore Infraction.update( active=False).where((Infraction.type == "Mute") | (Infraction.type == "Kick")).execute()
async def forceban(self, ctx: commands.Context, user_id: UserID, *, reason: Reason = ""): """forceban_help""" if reason == "": reason = Translator.translate("no_reason", ctx.guild.id) try: member = await commands.MemberConverter().convert( ctx, str(user_id)) except BadArgument: user = await ctx.bot.get_user_info(user_id) self.bot.data["forced_exits"].add(f"{ctx.guild.id}-{user.id}") await ctx.guild.ban( user, reason=Utils.trim_message( f"Moderator: {ctx.author.name}#{ctx.author.discriminator} ({ctx.author.id}) Reason: {reason}", 500), delete_message_days=0) await ctx.send( f"{Emoji.get_chat_emoji('YES')} {Translator.translate('forceban_confirmation', ctx.guild.id, user=Utils.clean_user(user), user_id=user_id, reason=reason)}" ) GearbotLogging.log_to( ctx.guild.id, "MOD_ACTIONS", f":door: {Translator.translate('forceban_log', ctx.guild.id, user=Utils.clean_user(user), user_id=user_id, moderator=Utils.clean_user(ctx.author), moderator_id=ctx.author.id, reason=reason)}" ) tempbans = list(Infraction.select().where( (Infraction.user_id == user.id) & (Infraction.type == "Tempban") & (Infraction.guild_id == ctx.guild.id))) if len(tempbans) > 0: inf = tempbans[0] timeframe = datetime.datetime.utcfromtimestamp( inf.end.timestamp()) - datetime.datetime.utcfromtimestamp( time.time()) hours, remainder = divmod(int(timeframe.total_seconds()), 3600) minutes, seconds = divmod(remainder, 60) tt = Translator.translate("hours", ctx, hours=hours, minutes=minutes) await GearbotLogging.send_to(ctx, "WARNING", "forceban_override_tempban", user=Utils.clean_user(user), timeframe=tt, inf_id=inf.id) Infraction.update(active=False).where( (Infraction.user_id == user.id) & ((Infraction.type == "Unban") | (Infraction.type == "Tempban")) & (Infraction.guild_id == ctx.guild.id)).execute() InfractionUtils.add_infraction(ctx.guild.id, user.id, ctx.author.id, "Forced ban", reason) else: await ctx.send( f"{Emoji.get_chat_emoji('WARNING')} {Translator.translate('forceban_to_ban', ctx.guild.id, user=Utils.clean_user(member))}" ) await ctx.invoke(self.ban, member, reason=reason)
async def censor_message(self, message: discord.Message): ctx = await self.bot.get_context(message) if message.guild is None or \ message.webhook_id is not None or \ message.author == message.guild.me or \ not Configuration.get_var(message.guild.id, "CENSOR_MESSAGES") or \ Permissioncheckers.get_user_lvl(ctx) >= 2: return blacklist = Configuration.get_var(message.guild.id, "WORD_BLACKLIST") max_mentions = Configuration.get_var(message.guild.id, "MAX_MENTIONS") guilds = Configuration.get_var(message.guild.id, "INVITE_WHITELIST") content = message.content.replace('\\', '') censored = False if len(guilds) is not 0: codes = INVITE_MATCHER.findall(content) for code in codes: try: invite: discord.Invite = await self.bot.get_invite(code) except discord.NotFound: await censor_invite(ctx, code, "INVALID INVITE") except KeyError: await censor_invite(ctx, code, "DM group") censored = True else: if invite.guild is None or (not invite.guild.id in guilds and invite.guild.id != message.guild.id): await censor_invite(ctx, code, invite.guild.name) censored = True if not censored: content = content.lower() for bad in (w.lower() for w in blacklist): if bad in content: if message.channel.permissions_for(message.guild.me).manage_messages: try: self.bot.data["message_deletes"].add(message.id) await message.delete() except discord.NotFound as ex: pass # lost the race with another bot? else: clean_message = await clean_content().convert(ctx, message.content) GearbotLogging.log_to(ctx.guild.id, "CENSORED_MESSAGES", f":no_entry_sign: {Translator.translate('censored_message', ctx.guild.id, user=message.author, user_id=message.author.id, message=clean_message, sequence=bad, channel=message.channel.mention)}") else: clean_message = await clean_content().convert(ctx, message.content) GearbotLogging.log_to(ctx.guild.id, "CENSORED_MESSAGES", f":no_entry_sign: {Translator.translate('censor_message_failed', ctx.guild.id, user=message.author, user_id=message.author.id, message=clean_message, sequence=bad, link=message.jump_url)}") mentions = len(message.mentions) + len(message.role_mentions) if mentions > max_mentions > 4: self.bot.data["forced_exits"].add(message.author.id) reason = Translator.translate('autoban_too_many_mentions', message.guild.id, count=mentions) if message.guild.me.guild_permissions.ban_members: await message.guild.ban(message.author, reason=reason) Infraction.update(active=False).where( (Infraction.user_id == message.author.id) & (Infraction.type == "Unban") & (Infraction.guild_id == ctx.guild.id)).execute() InfractionUtils.add_infraction(message.guild.id, message.author.id, self.bot.user.id, "AUTOBAN", reason) GearbotLogging.log_to(ctx.guild.id, "MOD_ACTIONS", f":door: {Translator.translate('ban_log', ctx.guild.id, user=message.author, user_id=message.author.id, moderator=self.bot.user, moderator_id=self.bot.user.id, reason=reason)}") else: self.bot.data["forced_exits"].remove(message.author.id) translated = Translator.translate('automod_ban_failed', message.guild.id, user=message.author, user_id=message.author.id, reason=reason) GearbotLogging.log_to(message.guild.id, "MOD_ACTIONS", f"{Emoji.get_chat_emoji('WARNING')} {translated}")