aboutsummaryrefslogtreecommitdiffstats
path: root/bot/utils/moderation.py
diff options
context:
space:
mode:
authorGravatar Johannes Christ <[email protected]>2019-01-01 19:22:48 +0100
committerGravatar Johannes Christ <[email protected]>2019-01-01 19:22:48 +0100
commit06d866fbffff87913e05f6c0b3b5ba788e9def06 (patch)
treebd8d7edf9729c0c6ba1bdfeec8e31fac99963e17 /bot/utils/moderation.py
parentRemove superfluous `self.headers` setting. (diff)
parentMerge pull request #242 from python-discord/eval-indent-fix (diff)
Merge branch 'master' into django
Diffstat (limited to 'bot/utils/moderation.py')
-rw-r--r--bot/utils/moderation.py45
1 files changed, 45 insertions, 0 deletions
diff --git a/bot/utils/moderation.py b/bot/utils/moderation.py
new file mode 100644
index 000000000..724b455bc
--- /dev/null
+++ b/bot/utils/moderation.py
@@ -0,0 +1,45 @@
+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
+):
+
+ payload = {
+ "type": type,
+ "reason": reason,
+ "user_id": str(user.id),
+ "actor_id": str(ctx.message.author.id),
+ "hidden": hidden
+ }
+ if duration:
+ payload['duration'] = duration
+
+ try:
+ response = await ctx.bot.http_session.post(
+ URLs.site_infractions,
+ headers=HEADERS,
+ 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
+
+ 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