Skip to content

Commit

Permalink
user infractions/warnings list added
Browse files Browse the repository at this point in the history
  • Loading branch information
99oblivius committed Sep 19, 2024
1 parent 9203d29 commit 0d4ad19
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 7 deletions.
51 changes: 45 additions & 6 deletions src/cogs/moderation/cog.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.

import json
import re
from datetime import datetime, timedelta, timezone
from io import BytesIO
import asyncio
from datetime import datetime, timezone
from collections import defaultdict
from fuzzywuzzy import process

import nextcord
from nextcord.ext import commands
Expand Down Expand Up @@ -48,7 +46,7 @@ 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)
):
):
await self.bot.store.upsert_warning(
guild_id=interaction.guild.id,
user_id=user.id,
Expand All @@ -68,6 +66,47 @@ async def moderation_warn(self, interaction: nextcord.Interaction,
settings = await self.bot.store.get_settings(interaction.guild.id)
await log_moderation(interaction, settings.log_channel, f"{user.name} warned{f' Match # {match_id}' if match_id else ''}", f"```\n{message}```")

@moderation.subcommand(name="history", description="View a user's infractions")
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),
):
warned_users = await self.bot.store.get_user_warnings(guild_id=interaction.guild.id, user_id=user.id, type=warn_filter)
grouped_users = defaultdict(list)
for user in warned_users:
grouped_users[user.type].append(user)

embed = nextcord.Embed(
title="Warned Users Summary",
color=0xff6600,
timestamp=datetime.now(timezone.utc))

for warn_type, warnings in grouped_users.items():
user_list = []
for warn in warnings:
user_info = f"<@{warn.user_id}>"
if warn.moderator_id:
user_info += f" - by <@{user.moderator_id}>"
user_info += f"\n{warn.message}"
user_list.append(user_info)

value = "\n".join(user_list)[:1024]

embed.add_field(
name=f"{warn_type.name} ({len(warnings)})",
value=value,
inline=False)

embed.set_footer(text=f"Total Warnings: {len(warned_users)}")
settings = await self.bot.store.get_settings(interaction.guild.id)
moderation_category = interaction.guild.get_channel(settings.staff_channel).category
await interaction.response.send_message(ephemeral=interaction.channel.category.id != moderation_category.id)

@moderation_infractions.on_autocomplete("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))


def setup(bot):
bot.add_cog(Moderation(bot))
14 changes: 13 additions & 1 deletion src/utils/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -1452,4 +1452,16 @@ async def upsert_warning(self,
warning_id = new_warning.id

await session.commit()
return warning_id
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 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))
result = await session.execute(query)
return result.scalars().all()

0 comments on commit 0d4ad19

Please sign in to comment.