mirror of
https://github.com/Cog-Creators/Red-DiscordBot.git
synced 2025-11-06 11:18:54 -05:00
Shorten the audit log reason appropriately, remove unused vars (#4189)
* [Mod] Check for reason length & remove unused vars This PR also remove unused variable. * Blame me * Apply Kowlin order. Text will get truncate in audit logs in case we're going over 512 characters. * This is already handled by ban_user() * Use `get_audit_reason()` in `[p]tempban` * Add a new kwarg to `get_audit_reason()` instead * Include `[p]voiceban` and `[p]voiceunban` * Include Mutes cog Co-authored-by: jack1142 <6032823+jack1142@users.noreply.github.com>
This commit is contained in:
parent
d3ffb22e86
commit
d53ff57794
@ -173,9 +173,8 @@ class KickBanMixin(MixinMeta):
|
|||||||
|
|
||||||
ban_type = "hackban"
|
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:
|
if removed_temp:
|
||||||
log.info(
|
log.info(
|
||||||
"{}({}) upgraded the tempban for {} to a permaban.".format(
|
"{}({}) upgraded the tempban for {} to a permaban.".format(
|
||||||
@ -199,7 +198,7 @@ class KickBanMixin(MixinMeta):
|
|||||||
return False, _("I'm not allowed to do that.")
|
return False, _("I'm not allowed to do that.")
|
||||||
except discord.NotFound:
|
except discord.NotFound:
|
||||||
return False, _("User with ID {user_id} not found").format(user_id=user.id)
|
return False, _("User with ID {user_id} not found").format(user_id=user.id)
|
||||||
except Exception as e:
|
except Exception:
|
||||||
log.exception(
|
log.exception(
|
||||||
"{}({}) attempted to {} {}({}), but an error occurred.".format(
|
"{}({}) attempted to {} {}({}), but an error occurred.".format(
|
||||||
author.name, author.id, ban_type, username, user.id
|
author.name, author.id, ban_type, username, user.id
|
||||||
@ -238,7 +237,6 @@ class KickBanMixin(MixinMeta):
|
|||||||
timezone.utc,
|
timezone.utc,
|
||||||
)
|
)
|
||||||
if datetime.now(timezone.utc) > unban_time: # Time to unban the user
|
if datetime.now(timezone.utc) > unban_time: # Time to unban the user
|
||||||
queue_entry = (guild.id, uid)
|
|
||||||
try:
|
try:
|
||||||
await guild.unban(
|
await guild.unban(
|
||||||
discord.Object(id=uid), reason=_("Tempban finished")
|
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:
|
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."))
|
await ctx.send(_("I cannot do that due to Discord hierarchy rules."))
|
||||||
return
|
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()
|
toggle = await self.config.guild(guild).dm_on_kickban()
|
||||||
if toggle:
|
if toggle:
|
||||||
with contextlib.suppress(discord.HTTPException):
|
with contextlib.suppress(discord.HTTPException):
|
||||||
@ -310,7 +308,7 @@ class KickBanMixin(MixinMeta):
|
|||||||
log.info("{}({}) kicked {}({})".format(author.name, author.id, user.name, user.id))
|
log.info("{}({}) kicked {}({})".format(author.name, author.id, user.name, user.id))
|
||||||
except discord.errors.Forbidden:
|
except discord.errors.Forbidden:
|
||||||
await ctx.send(_("I'm not allowed to do that."))
|
await ctx.send(_("I'm not allowed to do that."))
|
||||||
except Exception as e:
|
except Exception:
|
||||||
log.exception(
|
log.exception(
|
||||||
"{}({}) attempted to kick {}({}), but an error occurred.".format(
|
"{}({}) attempted to kick {}({}), but an error occurred.".format(
|
||||||
author.name, author.id, user.name, user.id
|
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.
|
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."""
|
Minimum 0 days, maximum 7. If not specified, defaultdays setting will be used instead."""
|
||||||
author = ctx.author
|
|
||||||
guild = ctx.guild
|
guild = ctx.guild
|
||||||
if days is None:
|
if days is None:
|
||||||
days = await self.config.guild(guild).default_days()
|
days = await self.config.guild(guild).default_days()
|
||||||
@ -485,8 +482,7 @@ class KickBanMixin(MixinMeta):
|
|||||||
|
|
||||||
for user_id in user_ids:
|
for user_id in user_ids:
|
||||||
user = discord.Object(id=user_id)
|
user = discord.Object(id=user_id)
|
||||||
audit_reason = get_audit_reason(author, reason)
|
audit_reason = get_audit_reason(author, reason, shorten=True)
|
||||||
queue_entry = (guild.id, user_id)
|
|
||||||
async with self.config.guild(guild).current_tempbans() as tempbans:
|
async with self.config.guild(guild).current_tempbans() as tempbans:
|
||||||
if user_id in tempbans:
|
if user_id in tempbans:
|
||||||
tempbans.remove(user_id)
|
tempbans.remove(user_id)
|
||||||
@ -576,7 +572,6 @@ class KickBanMixin(MixinMeta):
|
|||||||
if invite is None:
|
if invite is None:
|
||||||
invite = ""
|
invite = ""
|
||||||
|
|
||||||
queue_entry = (guild.id, user.id)
|
|
||||||
await self.config.member(user).banned_until.set(unban_time.timestamp())
|
await self.config.member(user).banned_until.set(unban_time.timestamp())
|
||||||
async with self.config.guild(guild).current_tempbans() as current_tempbans:
|
async with self.config.guild(guild).current_tempbans() as current_tempbans:
|
||||||
current_tempbans.append(user.id)
|
current_tempbans.append(user.id)
|
||||||
@ -591,8 +586,11 @@ class KickBanMixin(MixinMeta):
|
|||||||
invite_link=invite
|
invite_link=invite
|
||||||
)
|
)
|
||||||
await user.send(msg)
|
await user.send(msg)
|
||||||
|
|
||||||
|
audit_reason = get_audit_reason(author, reason, shorten=True)
|
||||||
|
|
||||||
try:
|
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:
|
except discord.Forbidden:
|
||||||
await ctx.send(_("I can't do that for some reason."))
|
await ctx.send(_("I can't do that for some reason."))
|
||||||
except discord.HTTPException:
|
except discord.HTTPException:
|
||||||
@ -636,13 +634,12 @@ class KickBanMixin(MixinMeta):
|
|||||||
)
|
)
|
||||||
return
|
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)
|
invite = await self.get_invite_for_reinvite(ctx)
|
||||||
if invite is None:
|
if invite is None:
|
||||||
invite = ""
|
invite = ""
|
||||||
|
|
||||||
queue_entry = (guild.id, user.id)
|
|
||||||
try: # We don't want blocked DMs preventing us from banning
|
try: # We don't want blocked DMs preventing us from banning
|
||||||
msg = await user.send(
|
msg = await user.send(
|
||||||
_(
|
_(
|
||||||
@ -660,7 +657,7 @@ class KickBanMixin(MixinMeta):
|
|||||||
if msg is not None:
|
if msg is not None:
|
||||||
await msg.delete()
|
await msg.delete()
|
||||||
return
|
return
|
||||||
except discord.HTTPException as e:
|
except discord.HTTPException:
|
||||||
log.exception(
|
log.exception(
|
||||||
"{}({}) attempted to softban {}({}), but an error occurred trying to ban them.".format(
|
"{}({}) attempted to softban {}({}), but an error occurred trying to ban them.".format(
|
||||||
author.name, author.id, user.name, user.id
|
author.name, author.id, user.name, user.id
|
||||||
@ -669,7 +666,7 @@ class KickBanMixin(MixinMeta):
|
|||||||
return
|
return
|
||||||
try:
|
try:
|
||||||
await guild.unban(user)
|
await guild.unban(user)
|
||||||
except discord.HTTPException as e:
|
except discord.HTTPException:
|
||||||
log.exception(
|
log.exception(
|
||||||
"{}({}) attempted to softban {}({}), but an error occurred trying to unban them.".format(
|
"{}({}) attempted to softban {}({}), but an error occurred trying to unban them.".format(
|
||||||
author.name, author.id, user.name, user.id
|
author.name, author.id, user.name, user.id
|
||||||
@ -755,7 +752,7 @@ class KickBanMixin(MixinMeta):
|
|||||||
return
|
return
|
||||||
needs_unmute = True if user_voice_state.mute else False
|
needs_unmute = True if user_voice_state.mute else False
|
||||||
needs_undeafen = True if user_voice_state.deaf 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:
|
if needs_unmute and needs_undeafen:
|
||||||
await user.edit(mute=False, deafen=False, reason=audit_reason)
|
await user.edit(mute=False, deafen=False, reason=audit_reason)
|
||||||
elif needs_unmute:
|
elif needs_unmute:
|
||||||
@ -796,7 +793,7 @@ class KickBanMixin(MixinMeta):
|
|||||||
return
|
return
|
||||||
needs_mute = True if user_voice_state.mute is False else False
|
needs_mute = True if user_voice_state.mute is False else False
|
||||||
needs_deafen = True if user_voice_state.deaf 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
|
author = ctx.author
|
||||||
guild = ctx.guild
|
guild = ctx.guild
|
||||||
if needs_mute and needs_deafen:
|
if needs_mute and needs_deafen:
|
||||||
@ -835,14 +832,13 @@ class KickBanMixin(MixinMeta):
|
|||||||
click the user and select 'Copy ID'."""
|
click the user and select 'Copy ID'."""
|
||||||
guild = ctx.guild
|
guild = ctx.guild
|
||||||
author = ctx.author
|
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 = await guild.bans()
|
||||||
bans = [be.user for be in bans]
|
bans = [be.user for be in bans]
|
||||||
user = discord.utils.get(bans, id=user_id)
|
user = discord.utils.get(bans, id=user_id)
|
||||||
if not user:
|
if not user:
|
||||||
await ctx.send(_("It seems that user isn't banned!"))
|
await ctx.send(_("It seems that user isn't banned!"))
|
||||||
return
|
return
|
||||||
queue_entry = (guild.id, user_id)
|
|
||||||
try:
|
try:
|
||||||
await guild.unban(user, reason=audit_reason)
|
await guild.unban(user, reason=audit_reason)
|
||||||
except discord.HTTPException:
|
except discord.HTTPException:
|
||||||
|
|||||||
@ -978,7 +978,7 @@ class Mutes(VoiceMutes, commands.Cog, metaclass=CompositeMetaClass):
|
|||||||
)
|
)
|
||||||
author = ctx.message.author
|
author = ctx.message.author
|
||||||
guild = ctx.guild
|
guild = ctx.guild
|
||||||
audit_reason = get_audit_reason(author, reason)
|
audit_reason = get_audit_reason(author, reason, shorten=True)
|
||||||
success_list = []
|
success_list = []
|
||||||
issue_list = []
|
issue_list = []
|
||||||
for user in users:
|
for user in users:
|
||||||
@ -1122,7 +1122,7 @@ class Mutes(VoiceMutes, commands.Cog, metaclass=CompositeMetaClass):
|
|||||||
author = ctx.message.author
|
author = ctx.message.author
|
||||||
channel = ctx.message.channel
|
channel = ctx.message.channel
|
||||||
guild = ctx.guild
|
guild = ctx.guild
|
||||||
audit_reason = get_audit_reason(author, reason)
|
audit_reason = get_audit_reason(author, reason, shorten=True)
|
||||||
issue_list = []
|
issue_list = []
|
||||||
success_list = []
|
success_list = []
|
||||||
for user in users:
|
for user in users:
|
||||||
@ -1187,7 +1187,7 @@ class Mutes(VoiceMutes, commands.Cog, metaclass=CompositeMetaClass):
|
|||||||
async with ctx.typing():
|
async with ctx.typing():
|
||||||
guild = ctx.guild
|
guild = ctx.guild
|
||||||
author = ctx.author
|
author = ctx.author
|
||||||
audit_reason = get_audit_reason(author, reason)
|
audit_reason = get_audit_reason(author, reason, shorten=True)
|
||||||
issue_list = []
|
issue_list = []
|
||||||
success_list = []
|
success_list = []
|
||||||
if guild.id in self._channel_mute_events:
|
if guild.id in self._channel_mute_events:
|
||||||
@ -1252,7 +1252,7 @@ class Mutes(VoiceMutes, commands.Cog, metaclass=CompositeMetaClass):
|
|||||||
channel = ctx.channel
|
channel = ctx.channel
|
||||||
author = ctx.author
|
author = ctx.author
|
||||||
guild = ctx.guild
|
guild = ctx.guild
|
||||||
audit_reason = get_audit_reason(author, reason)
|
audit_reason = get_audit_reason(author, reason, shorten=True)
|
||||||
success_list = []
|
success_list = []
|
||||||
issue_list = []
|
issue_list = []
|
||||||
for user in users:
|
for user in users:
|
||||||
|
|||||||
@ -121,7 +121,7 @@ class VoiceMutes(MixinMeta):
|
|||||||
guild = ctx.guild
|
guild = ctx.guild
|
||||||
author = ctx.author
|
author = ctx.author
|
||||||
channel = user_voice_state.channel
|
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(
|
success = await self.channel_mute_user(
|
||||||
guild, channel, author, user, until, audit_reason
|
guild, channel, author, user, until, audit_reason
|
||||||
@ -194,7 +194,7 @@ class VoiceMutes(MixinMeta):
|
|||||||
guild = ctx.guild
|
guild = ctx.guild
|
||||||
author = ctx.author
|
author = ctx.author
|
||||||
channel = user_voice_state.channel
|
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(
|
success = await self.channel_unmute_user(
|
||||||
guild, channel, author, user, audit_reason
|
guild, channel, author, user, audit_reason
|
||||||
|
|||||||
@ -67,7 +67,7 @@ async def slow_deletion(messages: Iterable[discord.Message]):
|
|||||||
pass
|
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.
|
"""Construct a reason to appear in the audit log.
|
||||||
|
|
||||||
Parameters
|
Parameters
|
||||||
@ -76,6 +76,9 @@ def get_audit_reason(author: discord.Member, reason: str = None):
|
|||||||
The author behind the audit log action.
|
The author behind the audit log action.
|
||||||
reason : str
|
reason : str
|
||||||
The reason behind the audit log action.
|
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
|
Returns
|
||||||
-------
|
-------
|
||||||
@ -83,11 +86,14 @@ def get_audit_reason(author: discord.Member, reason: str = None):
|
|||||||
The formatted audit log reason.
|
The formatted audit log reason.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
return (
|
audit_reason = (
|
||||||
"Action requested by {} (ID {}). Reason: {}".format(author, author.id, reason)
|
"Action requested by {} (ID {}). Reason: {}".format(author, author.id, reason)
|
||||||
if reason
|
if reason
|
||||||
else "Action requested by {} (ID {}).".format(author, author.id)
|
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(
|
async def is_allowed_by_hierarchy(
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user