Skip to content

Commit

Permalink
fixed warn history filtering and autocomplete
Browse files Browse the repository at this point in the history
  • Loading branch information
99oblivius committed Sep 19, 2024
1 parent 1147fc4 commit 2e62687
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
25 changes: 15 additions & 10 deletions src/cogs/moderation/cog.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ async def moderation(self, interaction: nextcord.Interaction):
async def moderation_warn(self, interaction: nextcord.Interaction,
user: nextcord.User | nextcord.Member = nextcord.SlashOption(description="Which user to add a warning to"),
message: str = nextcord.SlashOption(description="The warn message. The user will see this."),
match_id: int = nextcord.SlashOption(description="Match number the warn is assigned to.", default=None, required=False)
match_id: int = nextcord.SlashOption(description="Match number the warn is assigned to.", default=None, required=False),
silent: bool = nextcord.SlashOption(choices={"Yes": True, "No": False}, default=False, required=False, description="Should the user not be messaged?")
):
await self.bot.store.upsert_warning(
guild_id=interaction.guild.id,
Expand All @@ -56,11 +57,12 @@ async def moderation_warn(self, interaction: nextcord.Interaction,
warn_type=Warn.WARNING)

failed_to_send = False
embed = nextcord.Embed(title="You have been warned", description=f"```\n{message}```", color=0xff6600)
try:
await user.send(embed=embed)
except (nextcord.HTTPException, nextcord.Forbidden):
failed_to_send = True
if not silent:
embed = nextcord.Embed(title="You have been warned", description=f"```\n{message}```", color=0xff6600)
try:
await user.send(embed=embed)
except (nextcord.HTTPException, nextcord.Forbidden):
failed_to_send = True

await interaction.response.send_message(f"{user.mention} has been warned and was{' **NOT** ' if failed_to_send else ' '} notified.", ephemeral=True)
settings = await self.bot.store.get_settings(interaction.guild.id)
Expand All @@ -71,13 +73,17 @@ async def moderation_infractions(self, interaction: nextcord.Interaction,
user: nextcord.User | nextcord.Member = nextcord.SlashOption(description="Which user to add a warning to"),
warn_filter: str = nextcord.SlashOption(name="filter", description="Filter by warn type", required=False),
):
all_warnings = await self.bot.store.get_user_warnings(guild_id=interaction.guild.id, user_id=user.id, warn_filter=warn_filter)
warn_filters = None
if warn_filter:
warn_filters = [next((w for w in Warn if w.value.lower() == warn_filter.lower()))]

all_warnings = await self.bot.store.get_user_warnings(guild_id=interaction.guild.id, user_id=user.id, warn_filters=warn_filters)
grouped_users = defaultdict(list)
for warn in all_warnings:
grouped_users[warn.type].append(warn)

embed = nextcord.Embed(
title="User Infractions Summary",
title=f"User Infractions Summary{f' by {','.join((w.value.capitalize() for w in warn_filters))}' if warn_filters else ''}",
description=f"{user.mention}",
color=0xff6600,
timestamp=datetime.now(timezone.utc))
Expand Down Expand Up @@ -105,8 +111,7 @@ async def moderation_infractions(self, interaction: nextcord.Interaction,

@moderation_infractions.on_autocomplete("warn_filter")
async def autocomplete_infractions(self, interaction: nextcord.Interaction, warn_filter):
matches = process.extract(warn_filter, ((w, w.value.capitalize()) for w in Warn), limit=25, processor=lambda x: x[1].lower())
await interaction.response.send_autocomplete(choices=dict(matches))
await interaction.response.send_autocomplete(choices=[w.value.capitalize() for w in Warn])


def setup(bot):
Expand Down
6 changes: 3 additions & 3 deletions src/utils/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -1455,13 +1455,13 @@ async def upsert_warning(self,
return warning_id

@log_db_operation
async def get_user_warnings(self, guild_id: int, user_id: int, warn_filter: List[Warn] | None = None) -> List[MMBotWarnedUsers]:
async def get_user_warnings(self, guild_id: int, user_id: int, warn_filters: List[Warn] | None = None) -> List[MMBotWarnedUsers]:
async with self._session_maker() as session:
query = (select(MMBotWarnedUsers)
.where(
MMBotWarnedUsers.guild_id == guild_id,
MMBotWarnedUsers.user_id == user_id))
if warn_filter:
query = query.where(MMBotWarnedUsers.type.in_(warn_filter))
if warn_filters:
query = query.where(MMBotWarnedUsers.type.in_(warn_filters))
result = await session.execute(query)
return result.scalars().all()

0 comments on commit 2e62687

Please sign in to comment.