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."""
|
"""JARVIS BanCog."""
|
||||||
import re
|
import re
|
||||||
|
from datetime import timedelta
|
||||||
|
|
||||||
from jarvis_core.db import q
|
from jarvis_core.db import q
|
||||||
from jarvis_core.db.models import Ban, Unban
|
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.cogs import ModcaseCog
|
||||||
from jarvis.utils.permissions import admin_or_permissions
|
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):
|
class BanCog(ModcaseCog):
|
||||||
"""JARVIS BanCog."""
|
"""JARVIS BanCog."""
|
||||||
|
@ -35,9 +38,10 @@ class BanCog(ModcaseCog):
|
||||||
duration: int,
|
duration: int,
|
||||||
active: bool,
|
active: bool,
|
||||||
mtype: str,
|
mtype: str,
|
||||||
|
delete_history: int = 0,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Apply a Discord ban."""
|
"""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(
|
b = Ban(
|
||||||
user=user.id,
|
user=user.id,
|
||||||
username=user.username,
|
username=user.username,
|
||||||
|
@ -81,9 +85,7 @@ class BanCog(ModcaseCog):
|
||||||
|
|
||||||
@slash_command(name="ban", description="Ban a user")
|
@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="user", description="User to ban", opt_type=OptionTypes.USER, required=True)
|
||||||
@slash_option(
|
@slash_option(name="reason", description="Ban reason", opt_type=OptionTypes.STRING, required=True)
|
||||||
name="reason", description="Ban reason", opt_type=OptionTypes.STRING, required=True
|
|
||||||
)
|
|
||||||
@slash_option(
|
@slash_option(
|
||||||
name="btype",
|
name="btype",
|
||||||
description="Ban type",
|
description="Ban type",
|
||||||
|
@ -101,6 +103,12 @@ class BanCog(ModcaseCog):
|
||||||
opt_type=OptionTypes.INTEGER,
|
opt_type=OptionTypes.INTEGER,
|
||||||
required=False,
|
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))
|
@check(admin_or_permissions(Permissions.BAN_MEMBERS))
|
||||||
async def _ban(
|
async def _ban(
|
||||||
self,
|
self,
|
||||||
|
@ -109,6 +117,7 @@ class BanCog(ModcaseCog):
|
||||||
reason: str,
|
reason: str,
|
||||||
btype: str = "perm",
|
btype: str = "perm",
|
||||||
duration: int = 4,
|
duration: int = 4,
|
||||||
|
delete_history: str = None,
|
||||||
) -> None:
|
) -> None:
|
||||||
if user.id == ctx.author.id:
|
if user.id == ctx.author.id:
|
||||||
await ctx.send("You cannot ban yourself.", ephemeral=True)
|
await ctx.send("You cannot ban yourself.", ephemeral=True)
|
||||||
|
@ -122,10 +131,27 @@ class BanCog(ModcaseCog):
|
||||||
elif btype == "temp" and duration > 744:
|
elif btype == "temp" and duration > 744:
|
||||||
await ctx.send("You cannot set a temp ban to > 1 month", ephemeral=True)
|
await ctx.send("You cannot set a temp ban to > 1 month", ephemeral=True)
|
||||||
return
|
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:
|
if len(reason) > 100:
|
||||||
await ctx.send("Reason must be < 100 characters", ephemeral=True)
|
await ctx.send("Reason must be < 100 characters", ephemeral=True)
|
||||||
return
|
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()
|
await ctx.defer()
|
||||||
|
|
||||||
mtype = btype
|
mtype = btype
|
||||||
|
@ -152,7 +178,7 @@ class BanCog(ModcaseCog):
|
||||||
except Exception:
|
except Exception:
|
||||||
self.logger.warn(f"Failed to send ban embed to {user.id}")
|
self.logger.warn(f"Failed to send ban embed to {user.id}")
|
||||||
try:
|
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:
|
except Exception as e:
|
||||||
await ctx.send(f"Failed to ban user:\n```\n{e}\n```", ephemeral=True)
|
await ctx.send(f"Failed to ban user:\n```\n{e}\n```", ephemeral=True)
|
||||||
return
|
return
|
||||||
|
@ -166,15 +192,9 @@ class BanCog(ModcaseCog):
|
||||||
if btype == "soft":
|
if btype == "soft":
|
||||||
active = False
|
active = False
|
||||||
|
|
||||||
await self.discord_apply_ban(ctx, reason, user, duration, active, mtype)
|
|
||||||
|
|
||||||
@slash_command(name="unban", description="Unban a user")
|
@slash_command(name="unban", description="Unban a user")
|
||||||
@slash_option(
|
@slash_option(name="user", description="User to unban", opt_type=OptionTypes.STRING, required=True)
|
||||||
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="reason", description="Unban reason", opt_type=OptionTypes.STRING, required=True
|
|
||||||
)
|
|
||||||
@check(admin_or_permissions(Permissions.BAN_MEMBERS))
|
@check(admin_or_permissions(Permissions.BAN_MEMBERS))
|
||||||
async def _unban(
|
async def _unban(
|
||||||
self,
|
self,
|
||||||
|
@ -212,14 +232,9 @@ class BanCog(ModcaseCog):
|
||||||
if len(results) > 1:
|
if len(results) > 1:
|
||||||
active_bans = []
|
active_bans = []
|
||||||
for ban in bans:
|
for ban in bans:
|
||||||
active_bans.append(
|
active_bans.append("{0} ({1}): {2}".format(ban.user.username, ban.user.id, ban.reason))
|
||||||
"{0} ({1}): {2}".format(ban.user.username, ban.user.id, ban.reason)
|
|
||||||
)
|
|
||||||
ab_message = "\n".join(active_bans)
|
ab_message = "\n".join(active_bans)
|
||||||
message = (
|
message = "More than one result. " f"Please use one of the following IDs:\n```{ab_message}\n```"
|
||||||
"More than one result. "
|
|
||||||
f"Please use one of the following IDs:\n```{ab_message}\n```"
|
|
||||||
)
|
|
||||||
await ctx.send(message)
|
await ctx.send(message)
|
||||||
return
|
return
|
||||||
else:
|
else:
|
||||||
|
@ -230,9 +245,7 @@ class BanCog(ModcaseCog):
|
||||||
# We take advantage of the previous checks to save CPU cycles
|
# We take advantage of the previous checks to save CPU cycles
|
||||||
if not discord_ban_info:
|
if not discord_ban_info:
|
||||||
if isinstance(user, User):
|
if isinstance(user, User):
|
||||||
database_ban_info = await Ban.find_one(
|
database_ban_info = await Ban.find_one(q(guild=ctx.guild.id, user=user.id, active=True))
|
||||||
q(guild=ctx.guild.id, user=user.id, active=True)
|
|
||||||
)
|
|
||||||
else:
|
else:
|
||||||
search = {
|
search = {
|
||||||
"guild": ctx.guild.id,
|
"guild": ctx.guild.id,
|
||||||
|
@ -288,9 +301,7 @@ class BanCog(ModcaseCog):
|
||||||
required=False,
|
required=False,
|
||||||
)
|
)
|
||||||
@check(admin_or_permissions(Permissions.BAN_MEMBERS))
|
@check(admin_or_permissions(Permissions.BAN_MEMBERS))
|
||||||
async def _bans_list(
|
async def _bans_list(self, ctx: InteractionContext, btype: int = 0, active: bool = True) -> None:
|
||||||
self, ctx: InteractionContext, btype: int = 0, active: bool = True
|
|
||||||
) -> None:
|
|
||||||
types = [0, "perm", "temp", "soft"]
|
types = [0, "perm", "temp", "soft"]
|
||||||
search = {"guild": ctx.guild.id}
|
search = {"guild": ctx.guild.id}
|
||||||
if active:
|
if active:
|
||||||
|
|
Loading…
Add table
Reference in a new issue