Skip to content

Commit

Permalink
[WarnSystem] Detect manual kicks as well
Browse files Browse the repository at this point in the history
  • Loading branch information
laggron42 committed Dec 29, 2021
1 parent 3c529d0 commit bac0f70
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 16 deletions.
13 changes: 7 additions & 6 deletions warnsystem/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,19 +401,20 @@ async def warnset_description(
@warnset.command(name="detectmanual")
async def warnset_detectmanual(self, ctx: commands.Context, enable: bool = None):
"""
Defines if the bot should log manual bans to WarnSystem.
Defines if the bot should log manual kicks/bans with WarnSystem.
If enabled, manually banning a member will make the bot log the action in the modlog and\
save it, as if it was performed with WarnSystem. **However, the member will not receive a DM**.
This also works with kicks, but not timeouts *yet*.
Invoke the command without arguments to get the current status.
"""
guild = ctx.guild
current = await self.data.guild(guild).log_manual()
if enable is None:
await ctx.send(
_(
"The bot currently {detect} manual bans. If you want to change this, "
"The bot currently {detect} manual actions. If you want to change this, "
"type `{prefix}warnset detectmanual {opposite}`."
).format(
detect=_("detects") if current else _("doesn't detect"),
Expand All @@ -423,10 +424,10 @@ async def warnset_detectmanual(self, ctx: commands.Context, enable: bool = None)
)
elif enable:
await self.data.guild(guild).log_manual.set(True)
await ctx.send(_("Done. The bot will now listen for manual bans and log them."))
await ctx.send(_("Done. The bot will now listen for manual actions and log them."))
else:
await self.data.guild(guild).log_manual.set(False)
await ctx.send(_("Done. The bot won't listen for manual bans anymore."))
await ctx.send(_("Done. The bot won't listen for manual actions anymore."))

@warnset.command(name="hierarchy")
async def warnset_hierarchy(self, ctx: commands.Context, enable: bool = None):
Expand Down Expand Up @@ -798,7 +799,7 @@ async def warnset_settings(self, ctx: commands.Context):
embeds[0].add_field(name=_("Respect hierarchy"), value=hierarchy)
embeds[0].add_field(name=_("Reinvite unbanned members"), value=reinvite)
embeds[0].add_field(name=_("Show responsible moderator"), value=show_mod)
embeds[0].add_field(name=_("Detect manual bans"), value=manual_bans)
embeds[0].add_field(name=_("Detect manual actions"), value=manual_bans)
embeds[0].add_field(name=_("Update new channels for mute role"), value=update_mute)
embeds[0].add_field(name=_("Remove roles on mute"), value=remove_roles)
embeds[0].add_field(name=_("Days of messages to delete"), value=bandays)
Expand Down
32 changes: 22 additions & 10 deletions warnsystem/warnsystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ def __init__(self, bot):

self.task: asyncio.Task

__version__ = "1.3.21"
__version__ = "1.3.22"
__author__ = ["retke (El Laggron)"]

# helpers
Expand Down Expand Up @@ -1394,6 +1394,13 @@ async def on_guild_channel_create(self, channel: discord.abc.GuildChannel):

@listener()
async def on_member_ban(self, guild: discord.Guild, member: discord.Member):
await self.on_manual_action(guild, member, 5)

@listener()
async def on_member_remove(self, guild: discord.Guild, member: discord.Member):
await self.on_manual_action(guild, member, 3)

async def on_manual_action(self, guild: discord.Guild, member: discord.Member, level: int):
# most of this code is from Cog-Creators, modlog cog
# https://github.com/Cog-Creators/Red-DiscordBot/blob/bc21f779762ec9f460aecae525fdcd634f6c2d85/redbot/core/modlog.py#L68
if not guild.me.guild_permissions.view_audit_log:
Expand All @@ -1402,21 +1409,25 @@ async def on_member_ban(self, guild: discord.Guild, member: discord.Member):
return
# check for that before doing anything else, means WarnSystem isn't setup
try:
await self.api.get_modlog_channel(guild, 5)
await self.api.get_modlog_channel(guild, level)
except errors.NotFound:
return
when = datetime.utcnow()
before = when + timedelta(minutes=1)
after = when - timedelta(minutes=1)
await asyncio.sleep(10) # prevent small delays from causing a 5 minute delay on entry
attempts = 0
# wait up to an hour to find a matching case
while attempts < 12:
action = {
3: discord.AuditLogAction.kick,
5: discord.AuditLogAction.ban,
}[level]
# wait up to 15 min to find a matching case
while attempts < 3:
attempts += 1
try:
entry = await guild.audit_logs(
action=discord.AuditLogAction.ban, before=before, after=after
).find(lambda e: e.target.id == member.id and after < e.created_at < before)
entry = await guild.audit_logs(action=action, before=before, after=after).find(
lambda e: e.target.id == member.id and after < e.created_at < before
)
except discord.Forbidden:
break
except discord.HTTPException:
Expand All @@ -1433,15 +1444,16 @@ async def on_member_ban(self, guild: discord.Guild, member: discord.Member):
guild,
[member],
mod,
5,
level,
reason,
date=date,
log_dm=False,
log_dm=True if level <= 2 else False,
take_action=False,
)
except Exception as e:
log.error(
f"[Guild {guild.id}] Failed to create a case based on manual ban. "
f"[Guild {guild.id}] Failed to create a case "
"based on manual action. "
f"Member: {member} ({member.id}). Author: {mod} ({mod.id}). "
f"Reason: {reason}",
exc_info=e,
Expand Down

0 comments on commit bac0f70

Please sign in to comment.