diff options
author | 2018-11-21 07:24:41 -0500 | |
---|---|---|
committer | 2018-11-21 13:24:41 +0100 | |
commit | 44e41f3244d26a0a8331fda3b1b2e393f980b0b8 (patch) | |
tree | 933832a260f7780c12fe53517b634555d320f433 | |
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
-rw-r--r-- | bot/cogs/bigbrother.py | 18 | ||||
-rw-r--r-- | bot/cogs/moderation.py | 263 | ||||
-rw-r--r-- | bot/utils/moderation.py | 41 |
3 files changed, 77 insertions, 245 deletions
diff --git a/bot/cogs/bigbrother.py b/bot/cogs/bigbrother.py index d3e829d6a..7964c81a8 100644 --- a/bot/cogs/bigbrother.py +++ b/bot/cogs/bigbrother.py @@ -11,6 +11,7 @@ from bot.constants import BigBrother as BigBrotherConfig, Channels, Emojis, Guil from bot.decorators import with_role from bot.pagination import LinePaginator from bot.utils import messages +from bot.utils.moderation import post_infraction log = logging.getLogger(__name__) @@ -215,16 +216,14 @@ class BigBrother: @bigbrother_group.command(name='watch', aliases=('w',)) @with_role(Roles.owner, Roles.admin, Roles.moderator) - async def watch_command(self, ctx: Context, user: User, channel: TextChannel = None): + async def watch_command(self, ctx: Context, user: User, *, reason: str = None): """ - Relay messages sent by the given `user` in the given `channel`. - If `channel` is not specified, logs to the mod log channel. + Relay messages sent by the given `user` to the `#big-brother-logs` channel + + If a `reason` is specified, a note is added for `user` """ - if channel is not None: - channel_id = channel.id - else: - channel_id = Channels.big_brother_logs + channel_id = Channels.big_brother_logs post_data = { 'user_id': str(user.id), @@ -252,6 +251,11 @@ class BigBrother: reason = data.get('error_message', "no message provided") await ctx.send(f":x: the API returned an error: {reason}") + # Add a note (shadow warning) if a reason is specified + if reason: + reason = "bb watch: " + reason # Prepend for situational awareness + await post_infraction(ctx, user, type="warning", reason=reason, hidden=True) + @bigbrother_group.command(name='unwatch', aliases=('uw',)) @with_role(Roles.owner, Roles.admin, Roles.moderator) async def unwatch_command(self, ctx: Context, user: User): diff --git a/bot/cogs/moderation.py b/bot/cogs/moderation.py index 4afeeb768..71654ee1c 100644 --- a/bot/cogs/moderation.py +++ b/bot/cogs/moderation.py @@ -17,6 +17,7 @@ from bot.constants import Colours, Event, Icons, Keys, Roles, URLs from bot.converters import InfractionSearchQuery from bot.decorators import with_role from bot.pagination import LinePaginator +from bot.utils.moderation import post_infraction from bot.utils.scheduling import Scheduler, create_task from bot.utils.time import parse_rfc1123, wait_until @@ -86,25 +87,8 @@ class Moderation(Scheduler): reason=reason ) - try: - response = await self.bot.http_session.post( - URLs.site_infractions, - headers=self.headers, - json={ - "type": "warning", - "reason": reason, - "user_id": str(user.id), - "actor_id": str(ctx.message.author.id) - } - ) - 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']}") + response_object = await post_infraction(ctx, user, type="warning", reason=reason) + if response_object is None: return if reason is None: @@ -129,25 +113,8 @@ class Moderation(Scheduler): reason=reason ) - try: - response = await self.bot.http_session.post( - URLs.site_infractions, - headers=self.headers, - json={ - "type": "kick", - "reason": reason, - "user_id": str(user.id), - "actor_id": str(ctx.message.author.id) - } - ) - 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']}") + response_object = await post_infraction(ctx, user, type="kick", reason=reason) + if response_object is None: return self.mod_log.ignore(Event.member_remove, user.id) @@ -189,25 +156,8 @@ class Moderation(Scheduler): reason=reason ) - try: - response = await self.bot.http_session.post( - URLs.site_infractions, - headers=self.headers, - json={ - "type": "ban", - "reason": reason, - "user_id": str(user.id), - "actor_id": str(ctx.message.author.id) - } - ) - 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']}") + response_object = await post_infraction(ctx, user, type="ban", reason=reason) + if response_object is None: return self.mod_log.ignore(Event.member_ban, user.id) @@ -250,25 +200,8 @@ class Moderation(Scheduler): reason=reason ) - try: - response = await self.bot.http_session.post( - URLs.site_infractions, - headers=self.headers, - json={ - "type": "mute", - "reason": reason, - "user_id": str(user.id), - "actor_id": str(ctx.message.author.id) - } - ) - 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']}") + response_object = await post_infraction(ctx, user, type="mute", reason=reason) + if response_object is None: return # add the mute role @@ -315,26 +248,8 @@ class Moderation(Scheduler): reason=reason ) - try: - response = await self.bot.http_session.post( - URLs.site_infractions, - headers=self.headers, - json={ - "type": "mute", - "reason": reason, - "duration": duration, - "user_id": str(user.id), - "actor_id": str(ctx.message.author.id) - } - ) - 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']}") + response_object = await post_infraction(ctx, user, type="mute", reason=reason, duration=duration) + if response_object is None: return self.mod_log.ignore(Event.member_update, user.id) @@ -385,26 +300,8 @@ class Moderation(Scheduler): reason=reason ) - try: - response = await self.bot.http_session.post( - URLs.site_infractions, - headers=self.headers, - json={ - "type": "ban", - "reason": reason, - "duration": duration, - "user_id": str(user.id), - "actor_id": str(ctx.message.author.id) - } - ) - 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']}") + response_object = await post_infraction(ctx, user, type="ban", reason=reason, duration=duration) + if response_object is None: return self.mod_log.ignore(Event.member_ban, user.id) @@ -452,26 +349,8 @@ class Moderation(Scheduler): :param reason: The reason for the warning. """ - try: - response = await self.bot.http_session.post( - URLs.site_infractions, - headers=self.headers, - json={ - "type": "warning", - "reason": reason, - "user_id": str(user.id), - "actor_id": str(ctx.message.author.id), - "hidden": True - } - ) - 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']}") + response_object = await post_infraction(ctx, user, type="warning", reason=reason, hidden=True) + if response_object is None: return if reason is None: @@ -490,26 +369,8 @@ class Moderation(Scheduler): :param reason: The reason for the kick. """ - try: - response = await self.bot.http_session.post( - URLs.site_infractions, - headers=self.headers, - json={ - "type": "kick", - "reason": reason, - "user_id": str(user.id), - "actor_id": str(ctx.message.author.id), - "hidden": True - } - ) - 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']}") + response_object = await post_infraction(ctx, user, type="kick", reason=reason, hidden=True) + if response_object is None: return self.mod_log.ignore(Event.member_remove, user.id) @@ -544,26 +405,8 @@ class Moderation(Scheduler): :param reason: The reason for the ban. """ - try: - response = await self.bot.http_session.post( - URLs.site_infractions, - headers=self.headers, - json={ - "type": "ban", - "reason": reason, - "user_id": str(user.id), - "actor_id": str(ctx.message.author.id), - "hidden": True - } - ) - 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']}") + response_object = await post_infraction(ctx, user, type="ban", reason=reason, hidden=True) + if response_object is None: return self.mod_log.ignore(Event.member_ban, user.id) @@ -599,26 +442,8 @@ class Moderation(Scheduler): :param reason: The reason for the mute. """ - try: - response = await self.bot.http_session.post( - URLs.site_infractions, - headers=self.headers, - json={ - "type": "mute", - "reason": reason, - "user_id": str(user.id), - "actor_id": str(ctx.message.author.id), - "hidden": True - } - ) - 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']}") + response_object = await post_infraction(ctx, user, type="mute", reason=reason, hidden=True) + if response_object is None: return # add the mute role @@ -658,27 +483,8 @@ class Moderation(Scheduler): :param reason: The reason for the temporary mute. """ - try: - response = await self.bot.http_session.post( - URLs.site_infractions, - headers=self.headers, - json={ - "type": "mute", - "reason": reason, - "duration": duration, - "user_id": str(user.id), - "actor_id": str(ctx.message.author.id), - "hidden": True - } - ) - 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']}") + response_object = await post_infraction(ctx, user, type="mute", reason=reason, duration=duration, hidden=True) + if response_object is None: return self.mod_log.ignore(Event.member_update, user.id) @@ -724,27 +530,8 @@ class Moderation(Scheduler): :param reason: The reason for the temporary ban. """ - try: - response = await self.bot.http_session.post( - URLs.site_infractions, - headers=self.headers, - json={ - "type": "ban", - "reason": reason, - "duration": duration, - "user_id": str(user.id), - "actor_id": str(ctx.message.author.id), - "hidden": True - } - ) - 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']}") + response_object = await post_infraction(ctx, user, type="ban", reason=reason, duration=duration, hidden=True) + if response_object is None: return self.mod_log.ignore(Event.member_ban, user.id) 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 |