aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Johannes Christ <[email protected]>2021-11-10 10:53:07 +0100
committerGravatar GitHub <[email protected]>2021-11-10 10:53:07 +0100
commit3a23e567658e7e68881151b626454bd30828cdfe (patch)
treec434572dcd5c392209feb60368f41ea8f3265d7b
parentcommands: add pip as an alias to pypi (#1942) (diff)
parentMerge branch 'main' into infractions-by-command (diff)
Merge pull request #1926 from python-discord/infractions-by-command
Add support for `!infractions by <m|me|uid>`
-rw-r--r--bot/exts/moderation/infraction/management.py60
1 files changed, 58 insertions, 2 deletions
diff --git a/bot/exts/moderation/infraction/management.py b/bot/exts/moderation/infraction/management.py
index 0a33ac5e2..109b89a95 100644
--- a/bot/exts/moderation/infraction/management.py
+++ b/bot/exts/moderation/infraction/management.py
@@ -243,8 +243,9 @@ class ModManagement(commands.Cog):
else:
user_str = str(user.id)
+ formatted_infraction_count = self.format_infraction_count(len(infraction_list))
embed = discord.Embed(
- title=f"Infractions for {user_str} ({len(infraction_list)} total)",
+ title=f"Infractions for {user_str} ({formatted_infraction_count} total)",
colour=discord.Colour.orange()
)
await self.send_infraction_list(ctx, embed, infraction_list)
@@ -256,15 +257,70 @@ class ModManagement(commands.Cog):
'bot/infractions/expanded',
params={'search': reason}
)
+
+ formatted_infraction_count = self.format_infraction_count(len(infraction_list))
embed = discord.Embed(
- title=f"Infractions matching `{reason}` ({len(infraction_list)} total)",
+ title=f"Infractions matching `{reason}` ({formatted_infraction_count} total)",
colour=discord.Colour.orange()
)
await self.send_infraction_list(ctx, embed, infraction_list)
# endregion
+ # region: Search for infractions by given actor
+
+ @infraction_group.command(name="by", aliases=("b",))
+ async def search_by_actor(
+ self,
+ ctx: Context,
+ actor: t.Union[discord.Member, t.Literal["m", "me"]],
+ oldest_first: bool = False
+ ) -> None:
+ """
+ Search for infractions made by `actor`.
+
+ Use "m" or "me" as the `actor` to get infractions by author.
+
+ Use "1" for `oldest_first` to send oldest infractions first.
+ """
+ if isinstance(actor, str):
+ actor = ctx.author
+
+ if oldest_first:
+ ordering = 'inserted_at' # oldest infractions first
+ else:
+ ordering = '-inserted_at' # newest infractions first
+
+ infraction_list = await self.bot.api_client.get(
+ 'bot/infractions/expanded',
+ params={
+ 'actor__id': str(actor.id),
+ 'ordering': ordering
+ }
+ )
+
+ formatted_infraction_count = self.format_infraction_count(len(infraction_list))
+ embed = discord.Embed(
+ title=f"Infractions by {actor} ({formatted_infraction_count} total)",
+ colour=discord.Colour.orange()
+ )
+
+ await self.send_infraction_list(ctx, embed, infraction_list)
+
+ # endregion
# region: Utility functions
+ @staticmethod
+ def format_infraction_count(infraction_count: int) -> str:
+ """
+ Returns a string-formatted infraction count.
+
+ API limits returned infractions to a maximum of 100, so if `infraction_count`
+ is 100 then we return `"100+"`. Otherwise, return `str(infraction_count)`.
+ """
+ if infraction_count == 100:
+ return "100+"
+ return str(infraction_count)
+
async def send_infraction_list(
self,
ctx: Context,