Add delete_history to ban
This commit is contained in:
parent
622b003246
commit
f8cd9039b8
1 changed files with 37 additions and 26 deletions
|
@ -1,5 +1,6 @@
|
|||
"""JARVIS BanCog."""
|
||||
import re
|
||||
from datetime import timedelta
|
||||
|
||||
from jarvis_core.db import q
|
||||
from jarvis_core.db.models import Ban, Unban
|
||||
|
@ -23,6 +24,8 @@ from jarvis.utils import build_embed
|
|||
from jarvis.utils.cogs import ModcaseCog
|
||||
from jarvis.utils.permissions import admin_or_permissions
|
||||
|
||||
time_pattern = re.compile(r"(\d+\.?\d+?[s|m|h|d|w]{1})\s?", re.I)
|
||||
|
||||
|
||||
class BanCog(ModcaseCog):
|
||||
"""JARVIS BanCog."""
|
||||
|
@ -35,9 +38,10 @@ class BanCog(ModcaseCog):
|
|||
duration: int,
|
||||
active: bool,
|
||||
mtype: str,
|
||||
delete_history: int = 0,
|
||||
) -> None:
|
||||
"""Apply a Discord ban."""
|
||||
await ctx.guild.ban(user, reason=reason)
|
||||
await ctx.guild.ban(user, reason=reason, delete_message_seconds=delete_history)
|
||||
b = Ban(
|
||||
user=user.id,
|
||||
username=user.username,
|
||||
|
@ -81,9 +85,7 @@ class BanCog(ModcaseCog):
|
|||
|
||||
@slash_command(name="ban", description="Ban a user")
|
||||
@slash_option(name="user", description="User to ban", opt_type=OptionTypes.USER, required=True)
|
||||
@slash_option(
|
||||
name="reason", description="Ban reason", opt_type=OptionTypes.STRING, required=True
|
||||
)
|
||||
@slash_option(name="reason", description="Ban reason", opt_type=OptionTypes.STRING, required=True)
|
||||
@slash_option(
|
||||
name="btype",
|
||||
description="Ban type",
|
||||
|
@ -101,6 +103,12 @@ class BanCog(ModcaseCog):
|
|||
opt_type=OptionTypes.INTEGER,
|
||||
required=False,
|
||||
)
|
||||
@slash_option(
|
||||
name="delete_history",
|
||||
description="Delete message history, format: 1w 3d 7h 5m 20s",
|
||||
opt_type=OptionTypes.STRING,
|
||||
required=False,
|
||||
)
|
||||
@check(admin_or_permissions(Permissions.BAN_MEMBERS))
|
||||
async def _ban(
|
||||
self,
|
||||
|
@ -109,6 +117,7 @@ class BanCog(ModcaseCog):
|
|||
reason: str,
|
||||
btype: str = "perm",
|
||||
duration: int = 4,
|
||||
delete_history: str = None,
|
||||
) -> None:
|
||||
if user.id == ctx.author.id:
|
||||
await ctx.send("You cannot ban yourself.", ephemeral=True)
|
||||
|
@ -122,10 +131,27 @@ class BanCog(ModcaseCog):
|
|||
elif btype == "temp" and duration > 744:
|
||||
await ctx.send("You cannot set a temp ban to > 1 month", ephemeral=True)
|
||||
return
|
||||
if not time_pattern.match(delete_history):
|
||||
await ctx.send("Invalid time string, please follow example: 1w 3d 7h 5m 20s", ephemeral=True)
|
||||
return
|
||||
if len(reason) > 100:
|
||||
await ctx.send("Reason must be < 100 characters", ephemeral=True)
|
||||
return
|
||||
|
||||
if delete_history:
|
||||
units = {"w": "weeks", "d": "days", "h": "hours", "m": "minutes", "s": "seconds"}
|
||||
delta = {"weeks": 0, "days": 0, "hours": 0, "minutes": 0, "seconds": 0}
|
||||
delete_history = delete_history.strip().lower()
|
||||
if delete_history:
|
||||
if times := time_pattern.findall(delete_history):
|
||||
for t in times:
|
||||
delta[units[t[-1]]] += float(t[:-1])
|
||||
delete_history = int(timedelta(**delta).total_seconds)
|
||||
|
||||
if delete_history > 604800:
|
||||
await ctx.send("Delete history cannot be greater than 7 days (604800 seconds)", ephemeral=True)
|
||||
return
|
||||
|
||||
await ctx.defer()
|
||||
|
||||
mtype = btype
|
||||
|
@ -152,7 +178,7 @@ class BanCog(ModcaseCog):
|
|||
except Exception:
|
||||
self.logger.warn(f"Failed to send ban embed to {user.id}")
|
||||
try:
|
||||
await ctx.guild.ban(user, reason=reason)
|
||||
await self.discord_apply_ban(ctx, reason, user, duration, active, mtype, delete_history or 0)
|
||||
except Exception as e:
|
||||
await ctx.send(f"Failed to ban user:\n```\n{e}\n```", ephemeral=True)
|
||||
return
|
||||
|
@ -166,15 +192,9 @@ class BanCog(ModcaseCog):
|
|||
if btype == "soft":
|
||||
active = False
|
||||
|
||||
await self.discord_apply_ban(ctx, reason, user, duration, active, mtype)
|
||||
|
||||
@slash_command(name="unban", description="Unban a user")
|
||||
@slash_option(
|
||||
name="user", description="User to unban", opt_type=OptionTypes.STRING, required=True
|
||||
)
|
||||
@slash_option(
|
||||
name="reason", description="Unban reason", opt_type=OptionTypes.STRING, required=True
|
||||
)
|
||||
@slash_option(name="user", description="User to unban", opt_type=OptionTypes.STRING, required=True)
|
||||
@slash_option(name="reason", description="Unban reason", opt_type=OptionTypes.STRING, required=True)
|
||||
@check(admin_or_permissions(Permissions.BAN_MEMBERS))
|
||||
async def _unban(
|
||||
self,
|
||||
|
@ -212,14 +232,9 @@ class BanCog(ModcaseCog):
|
|||
if len(results) > 1:
|
||||
active_bans = []
|
||||
for ban in bans:
|
||||
active_bans.append(
|
||||
"{0} ({1}): {2}".format(ban.user.username, ban.user.id, ban.reason)
|
||||
)
|
||||
active_bans.append("{0} ({1}): {2}".format(ban.user.username, ban.user.id, ban.reason))
|
||||
ab_message = "\n".join(active_bans)
|
||||
message = (
|
||||
"More than one result. "
|
||||
f"Please use one of the following IDs:\n```{ab_message}\n```"
|
||||
)
|
||||
message = "More than one result. " f"Please use one of the following IDs:\n```{ab_message}\n```"
|
||||
await ctx.send(message)
|
||||
return
|
||||
else:
|
||||
|
@ -230,9 +245,7 @@ class BanCog(ModcaseCog):
|
|||
# We take advantage of the previous checks to save CPU cycles
|
||||
if not discord_ban_info:
|
||||
if isinstance(user, User):
|
||||
database_ban_info = await Ban.find_one(
|
||||
q(guild=ctx.guild.id, user=user.id, active=True)
|
||||
)
|
||||
database_ban_info = await Ban.find_one(q(guild=ctx.guild.id, user=user.id, active=True))
|
||||
else:
|
||||
search = {
|
||||
"guild": ctx.guild.id,
|
||||
|
@ -288,9 +301,7 @@ class BanCog(ModcaseCog):
|
|||
required=False,
|
||||
)
|
||||
@check(admin_or_permissions(Permissions.BAN_MEMBERS))
|
||||
async def _bans_list(
|
||||
self, ctx: InteractionContext, btype: int = 0, active: bool = True
|
||||
) -> None:
|
||||
async def _bans_list(self, ctx: InteractionContext, btype: int = 0, active: bool = True) -> None:
|
||||
types = [0, "perm", "temp", "soft"]
|
||||
search = {"guild": ctx.guild.id}
|
||||
if active:
|
||||
|
|
Loading…
Add table
Reference in a new issue