Fix ModcaseCog logging
This commit is contained in:
parent
5bb592183e
commit
ee86320c16
1 changed files with 36 additions and 8 deletions
|
@ -1,12 +1,24 @@
|
|||
"""Cog wrapper for command caching."""
|
||||
from datetime import datetime, timedelta, timezone
|
||||
|
||||
from dis_snek import Context, Scale, Snake
|
||||
from dis_snek import InteractionContext, Scale, Snake
|
||||
from dis_snek.client.utils.misc_utils import find
|
||||
from dis_snek.models.discord.embed import EmbedField
|
||||
from dis_snek.models.snek.tasks.task import Task
|
||||
from dis_snek.models.snek.tasks.triggers import IntervalTrigger
|
||||
from jarvis_core.db import q
|
||||
from jarvis_core.db.models import Action, Ban, Kick, Modlog, Mute, Note, Warning
|
||||
from jarvis_core.db.models import (
|
||||
Action,
|
||||
Ban,
|
||||
Kick,
|
||||
Modlog,
|
||||
Mute,
|
||||
Note,
|
||||
Setting,
|
||||
Warning,
|
||||
)
|
||||
|
||||
from jarvis.utils import build_embed
|
||||
|
||||
MODLOG_LOOKUP = {"Ban": Ban, "Kick": Kick, "Mute": Mute, "Warning": Warning}
|
||||
|
||||
|
@ -19,7 +31,7 @@ class CacheCog(Scale):
|
|||
self.cache = {}
|
||||
self._expire_interaction.start()
|
||||
|
||||
def check_cache(self, ctx: Context, **kwargs: dict) -> dict:
|
||||
def check_cache(self, ctx: InteractionContext, **kwargs: dict) -> dict:
|
||||
"""Check the cache."""
|
||||
if not kwargs:
|
||||
kwargs = {}
|
||||
|
@ -46,7 +58,7 @@ class ModcaseCog(Scale):
|
|||
self.bot = bot
|
||||
self.add_scale_postrun(self.log)
|
||||
|
||||
async def log(self, ctx: Context, *args: list, **kwargs: dict) -> None:
|
||||
async def log(self, ctx: InteractionContext, *args: list, **kwargs: dict) -> None:
|
||||
"""
|
||||
Log a moderation activity in a moderation case.
|
||||
|
||||
|
@ -57,19 +69,35 @@ class ModcaseCog(Scale):
|
|||
|
||||
if name not in ["Lock", "Lockdown", "Purge", "Roleping"]:
|
||||
user = kwargs.pop("user", None)
|
||||
if not user:
|
||||
# Log warning about missing user
|
||||
if not user and not ctx.target_id:
|
||||
self.logger.warn(f"Admin action {name} missing user, exiting")
|
||||
return
|
||||
elif ctx.target_id:
|
||||
user = ctx.target
|
||||
coll = MODLOG_LOOKUP.get(name, None)
|
||||
if not coll:
|
||||
# Log warning about unsupported action
|
||||
self.logger.warn(f"Unsupported action {name}, exiting")
|
||||
return
|
||||
|
||||
action = await coll.find_one(q(user=user.id, guild=ctx.guild_id, active=True))
|
||||
if not action:
|
||||
# Log warning about missing action
|
||||
self.logger.warn(f"Missing action {name}, exiting")
|
||||
return
|
||||
|
||||
action = Action(action_type=name.lower(), parent=action.id)
|
||||
note = Note(admin=self.bot.user.id, content="Moderation case opened automatically")
|
||||
await Modlog(user=user.id, admin=ctx.author.id, actions=[action], notes=[note]).commit()
|
||||
notify = await Setting.find_one(q(guild=ctx.guild.id, setting="notify", value=True))
|
||||
if notify and name not in ["Kick", "Ban"]: # Ignore Kick and Ban, as these are unique
|
||||
fields = [
|
||||
EmbedField(name="Action Type", value=name, inline=False),
|
||||
EmbedField(
|
||||
name="Reason", value=kwargs.get("reason", None) or "N/A", inline=False
|
||||
),
|
||||
]
|
||||
embed = build_embed(
|
||||
title="Admin action taken",
|
||||
description=f"Admin action has been taken against you in {ctx.guild.name}",
|
||||
fields=fields,
|
||||
)
|
||||
await user.send(embed=embed)
|
||||
|
|
Loading…
Add table
Reference in a new issue