aboutsummaryrefslogtreecommitdiffstats
path: root/bot/utils/moderation.py
diff options
context:
space:
mode:
Diffstat (limited to 'bot/utils/moderation.py')
-rw-r--r--bot/utils/moderation.py44
1 files changed, 36 insertions, 8 deletions
diff --git a/bot/utils/moderation.py b/bot/utils/moderation.py
index 9ea2db07c..7860f14a1 100644
--- a/bot/utils/moderation.py
+++ b/bot/utils/moderation.py
@@ -1,11 +1,11 @@
import logging
from datetime import datetime
-from typing import Union
+from typing import Optional, Union
-from aiohttp import ClientError
from discord import Member, Object, User
from discord.ext.commands import Context
+from bot.api import ResponseCodeError
from bot.constants import Keys
log = logging.getLogger(__name__)
@@ -21,8 +21,8 @@ async def post_infraction(
expires_at: datetime = None,
hidden: bool = False,
active: bool = True,
-) -> Union[dict, None]:
- """Post infraction to the API."""
+) -> Optional[dict]:
+ """Posts an infraction to the API."""
payload = {
"actor": ctx.message.author.id,
"hidden": hidden,
@@ -36,9 +36,37 @@ async def post_infraction(
try:
response = await ctx.bot.api_client.post('bot/infractions', json=payload)
- except ClientError:
- log.exception("There was an error adding an infraction.")
- await ctx.send(":x: There was an error adding the infraction.")
- return
+ except ResponseCodeError as exp:
+ if exp.status == 400 and 'user' in exp.response_json:
+ log.info(
+ f"{ctx.author} tried to add a {type} infraction to `{user.id}`, "
+ "but that user id was not found in the database."
+ )
+ await ctx.send(f":x: Cannot add infraction, the specified user is not known to the database.")
+ return
+ else:
+ log.exception("An unexpected ResponseCodeError occurred while adding an infraction:")
+ await ctx.send(":x: There was an error adding the infraction.")
+ return
return response
+
+
+async def already_has_active_infraction(ctx: Context, user: Union[Member, Object, User], type: str) -> bool:
+ """Checks if a user already has an active infraction of the given type."""
+ active_infractions = await ctx.bot.api_client.get(
+ 'bot/infractions',
+ params={
+ 'active': 'true',
+ 'type': type,
+ 'user__id': str(user.id)
+ }
+ )
+ if active_infractions:
+ await ctx.send(
+ f":x: According to my records, this user already has a {type} infraction. "
+ f"See infraction **#{active_infractions[0]['id']}**."
+ )
+ return True
+ else:
+ return False