diff options
author | 2018-11-21 07:24:41 -0500 | |
---|---|---|
committer | 2018-11-21 13:24:41 +0100 | |
commit | 44e41f3244d26a0a8331fda3b1b2e393f980b0b8 (patch) | |
tree | 933832a260f7780c12fe53517b634555d320f433 /bot/utils/moderation.py | |
parent | Superstarify modlog (#199) (diff) |
Add note support for bb watch, moderation API call refactor (#201)
* Add helper for moderation API calls
See: #200
* Add missing short-circuit for infraction fail
* Add bb watch note support
See: 103
* Add keyword-only argument to bb watch
To support a full reason string
* Add empty duration to moderation API helper
* Prepend bb watch to watch note string
Diffstat (limited to 'bot/utils/moderation.py')
-rw-r--r-- | bot/utils/moderation.py | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/bot/utils/moderation.py b/bot/utils/moderation.py new file mode 100644 index 000000000..1b36b4118 --- /dev/null +++ b/bot/utils/moderation.py @@ -0,0 +1,41 @@ +import logging +from typing import Union + +from aiohttp import ClientError +from discord import Member, Object, User +from discord.ext.commands import Context + +from bot.constants import Keys, URLs + +log = logging.getLogger(__name__) + +HEADERS = {"X-API-KEY": Keys.site_api} + + +async def post_infraction( + ctx: Context, user: Union[Member, Object, User], type: str, reason: str, duration: str = None, hidden: bool = False +): + try: + response = await ctx.bot.http_session.post( + URLs.site_infractions, + headers=HEADERS, + json={ + "type": type, + "reason": reason, + "duration": duration, + "user_id": str(user.id), + "actor_id": str(ctx.message.author.id), + "hidden": hidden, + }, + ) + except ClientError: + log.exception("There was an error adding an infraction.") + await ctx.send(":x: There was an error adding the infraction.") + return + + response_object = await response.json() + if "error_code" in response_object: + await ctx.send(f":x: There was an error adding the infraction: {response_object['error_message']}") + return + + return response_object |