diff --git a/redbot/cogs/mod/kickban.py b/redbot/cogs/mod/kickban.py index 64550a448..3a52dd691 100644 --- a/redbot/cogs/mod/kickban.py +++ b/redbot/cogs/mod/kickban.py @@ -173,9 +173,8 @@ class KickBanMixin(MixinMeta): ban_type = "hackban" - audit_reason = get_audit_reason(author, reason) + audit_reason = get_audit_reason(author, reason, shorten=True) - queue_entry = (guild.id, user.id) if removed_temp: log.info( "{}({}) upgraded the tempban for {} to a permaban.".format( @@ -199,7 +198,7 @@ class KickBanMixin(MixinMeta): return False, _("I'm not allowed to do that.") except discord.NotFound: return False, _("User with ID {user_id} not found").format(user_id=user.id) - except Exception as e: + except Exception: log.exception( "{}({}) attempted to {} {}({}), but an error occurred.".format( author.name, author.id, ban_type, username, user.id @@ -238,7 +237,6 @@ class KickBanMixin(MixinMeta): timezone.utc, ) if datetime.now(timezone.utc) > unban_time: # Time to unban the user - queue_entry = (guild.id, uid) try: await guild.unban( discord.Object(id=uid), reason=_("Tempban finished") @@ -292,7 +290,7 @@ class KickBanMixin(MixinMeta): elif ctx.guild.me.top_role <= user.top_role or user == ctx.guild.owner: await ctx.send(_("I cannot do that due to Discord hierarchy rules.")) return - audit_reason = get_audit_reason(author, reason) + audit_reason = get_audit_reason(author, reason, shorten=True) toggle = await self.config.guild(guild).dm_on_kickban() if toggle: with contextlib.suppress(discord.HTTPException): @@ -310,7 +308,7 @@ class KickBanMixin(MixinMeta): log.info("{}({}) kicked {}({})".format(author.name, author.id, user.name, user.id)) except discord.errors.Forbidden: await ctx.send(_("I'm not allowed to do that.")) - except Exception as e: + except Exception: log.exception( "{}({}) attempted to kick {}({}), but an error occurred.".format( author.name, author.id, user.name, user.id @@ -349,7 +347,6 @@ class KickBanMixin(MixinMeta): If days is not a number, it's treated as the first word of the reason. Minimum 0 days, maximum 7. If not specified, defaultdays setting will be used instead.""" - author = ctx.author guild = ctx.guild if days is None: days = await self.config.guild(guild).default_days() @@ -485,8 +482,7 @@ class KickBanMixin(MixinMeta): for user_id in user_ids: user = discord.Object(id=user_id) - audit_reason = get_audit_reason(author, reason) - queue_entry = (guild.id, user_id) + audit_reason = get_audit_reason(author, reason, shorten=True) async with self.config.guild(guild).current_tempbans() as tempbans: if user_id in tempbans: tempbans.remove(user_id) @@ -576,7 +572,6 @@ class KickBanMixin(MixinMeta): if invite is None: invite = "" - queue_entry = (guild.id, user.id) await self.config.member(user).banned_until.set(unban_time.timestamp()) async with self.config.guild(guild).current_tempbans() as current_tempbans: current_tempbans.append(user.id) @@ -591,8 +586,11 @@ class KickBanMixin(MixinMeta): invite_link=invite ) await user.send(msg) + + audit_reason = get_audit_reason(author, reason, shorten=True) + try: - await guild.ban(user, reason=reason, delete_message_days=days) + await guild.ban(user, reason=audit_reason, delete_message_days=days) except discord.Forbidden: await ctx.send(_("I can't do that for some reason.")) except discord.HTTPException: @@ -636,13 +634,12 @@ class KickBanMixin(MixinMeta): ) return - audit_reason = get_audit_reason(author, reason) + audit_reason = get_audit_reason(author, reason, shorten=True) invite = await self.get_invite_for_reinvite(ctx) if invite is None: invite = "" - queue_entry = (guild.id, user.id) try: # We don't want blocked DMs preventing us from banning msg = await user.send( _( @@ -660,7 +657,7 @@ class KickBanMixin(MixinMeta): if msg is not None: await msg.delete() return - except discord.HTTPException as e: + except discord.HTTPException: log.exception( "{}({}) attempted to softban {}({}), but an error occurred trying to ban them.".format( author.name, author.id, user.name, user.id @@ -669,7 +666,7 @@ class KickBanMixin(MixinMeta): return try: await guild.unban(user) - except discord.HTTPException as e: + except discord.HTTPException: log.exception( "{}({}) attempted to softban {}({}), but an error occurred trying to unban them.".format( author.name, author.id, user.name, user.id @@ -755,7 +752,7 @@ class KickBanMixin(MixinMeta): return needs_unmute = True if user_voice_state.mute else False needs_undeafen = True if user_voice_state.deaf else False - audit_reason = get_audit_reason(ctx.author, reason) + audit_reason = get_audit_reason(ctx.author, reason, shorten=True) if needs_unmute and needs_undeafen: await user.edit(mute=False, deafen=False, reason=audit_reason) elif needs_unmute: @@ -796,7 +793,7 @@ class KickBanMixin(MixinMeta): return needs_mute = True if user_voice_state.mute is False else False needs_deafen = True if user_voice_state.deaf is False else False - audit_reason = get_audit_reason(ctx.author, reason) + audit_reason = get_audit_reason(ctx.author, reason, shorten=True) author = ctx.author guild = ctx.guild if needs_mute and needs_deafen: @@ -835,14 +832,13 @@ class KickBanMixin(MixinMeta): click the user and select 'Copy ID'.""" guild = ctx.guild author = ctx.author - audit_reason = get_audit_reason(ctx.author, reason) + audit_reason = get_audit_reason(ctx.author, reason, shorten=True) bans = await guild.bans() bans = [be.user for be in bans] user = discord.utils.get(bans, id=user_id) if not user: await ctx.send(_("It seems that user isn't banned!")) return - queue_entry = (guild.id, user_id) try: await guild.unban(user, reason=audit_reason) except discord.HTTPException: diff --git a/redbot/cogs/mutes/mutes.py b/redbot/cogs/mutes/mutes.py index b4c02e22a..ef1ae088e 100644 --- a/redbot/cogs/mutes/mutes.py +++ b/redbot/cogs/mutes/mutes.py @@ -978,7 +978,7 @@ class Mutes(VoiceMutes, commands.Cog, metaclass=CompositeMetaClass): ) author = ctx.message.author guild = ctx.guild - audit_reason = get_audit_reason(author, reason) + audit_reason = get_audit_reason(author, reason, shorten=True) success_list = [] issue_list = [] for user in users: @@ -1122,7 +1122,7 @@ class Mutes(VoiceMutes, commands.Cog, metaclass=CompositeMetaClass): author = ctx.message.author channel = ctx.message.channel guild = ctx.guild - audit_reason = get_audit_reason(author, reason) + audit_reason = get_audit_reason(author, reason, shorten=True) issue_list = [] success_list = [] for user in users: @@ -1187,7 +1187,7 @@ class Mutes(VoiceMutes, commands.Cog, metaclass=CompositeMetaClass): async with ctx.typing(): guild = ctx.guild author = ctx.author - audit_reason = get_audit_reason(author, reason) + audit_reason = get_audit_reason(author, reason, shorten=True) issue_list = [] success_list = [] if guild.id in self._channel_mute_events: @@ -1252,7 +1252,7 @@ class Mutes(VoiceMutes, commands.Cog, metaclass=CompositeMetaClass): channel = ctx.channel author = ctx.author guild = ctx.guild - audit_reason = get_audit_reason(author, reason) + audit_reason = get_audit_reason(author, reason, shorten=True) success_list = [] issue_list = [] for user in users: diff --git a/redbot/cogs/mutes/voicemutes.py b/redbot/cogs/mutes/voicemutes.py index 6965ff7f5..074e6a4e1 100644 --- a/redbot/cogs/mutes/voicemutes.py +++ b/redbot/cogs/mutes/voicemutes.py @@ -121,7 +121,7 @@ class VoiceMutes(MixinMeta): guild = ctx.guild author = ctx.author channel = user_voice_state.channel - audit_reason = get_audit_reason(author, reason) + audit_reason = get_audit_reason(author, reason, shorten=True) success = await self.channel_mute_user( guild, channel, author, user, until, audit_reason @@ -194,7 +194,7 @@ class VoiceMutes(MixinMeta): guild = ctx.guild author = ctx.author channel = user_voice_state.channel - audit_reason = get_audit_reason(author, reason) + audit_reason = get_audit_reason(author, reason, shorten=True) success = await self.channel_unmute_user( guild, channel, author, user, audit_reason diff --git a/redbot/core/utils/mod.py b/redbot/core/utils/mod.py index 9bccac17b..94d88432a 100644 --- a/redbot/core/utils/mod.py +++ b/redbot/core/utils/mod.py @@ -67,7 +67,7 @@ async def slow_deletion(messages: Iterable[discord.Message]): pass -def get_audit_reason(author: discord.Member, reason: str = None): +def get_audit_reason(author: discord.Member, reason: str = None, *, shorten: bool = False): """Construct a reason to appear in the audit log. Parameters @@ -76,6 +76,9 @@ def get_audit_reason(author: discord.Member, reason: str = None): The author behind the audit log action. reason : str The reason behind the audit log action. + shorten : bool + When set to ``True``, the returned audit reason string will be + shortened to fit the max length allowed by Discord audit logs. Returns ------- @@ -83,11 +86,14 @@ def get_audit_reason(author: discord.Member, reason: str = None): The formatted audit log reason. """ - return ( + audit_reason = ( "Action requested by {} (ID {}). Reason: {}".format(author, author.id, reason) if reason else "Action requested by {} (ID {}).".format(author, author.id) ) + if shorten and len(audit_reason) > 512: + audit_reason = f"{audit_reason[:509]}..." + return audit_reason async def is_allowed_by_hierarchy(