Skip to content

Commit

Permalink
Merge branch 'main' into renovate/python-3.x
Browse files Browse the repository at this point in the history
  • Loading branch information
kzndotsh authored Nov 20, 2024
2 parents 06db07f + 4473ba2 commit 3539631
Show file tree
Hide file tree
Showing 22 changed files with 303 additions and 478 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ repos:
- id: check-toml

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.7.3
rev: v0.7.4
hooks:
# Run the linter.
- id: ruff
Expand Down
33 changes: 20 additions & 13 deletions tux/cogs/guild/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,14 @@ async def config_set_logs(
category : Literal["Public", "Private"]
The category of logs to configure.
"""
await interaction.response.defer(ephemeral=True)

if category == "Public":
view = ConfigSetPublicLogs()
elif category == "Private":
view = ConfigSetPrivateLogs()

await interaction.response.send_message(view=view, ephemeral=True)
await interaction.followup.send(view=view, ephemeral=True)

@channels.command(name="set")
@app_commands.guild_only()
Expand All @@ -70,9 +71,9 @@ async def config_set_channels(
interaction : discord.Interaction
The discord interaction object.
"""

await interaction.response.defer(ephemeral=True)
view = ConfigSetChannels()
await interaction.response.send_message(view=view, ephemeral=True)
await interaction.followup.send(view=view, ephemeral=True)

@perms.command(name="set")
@app_commands.guild_only()
Expand Down Expand Up @@ -112,17 +113,17 @@ async def config_set_perms(
"""

assert interaction.guild
await interaction.response.defer(ephemeral=True)

await self.db.update_perm_level_role(
interaction.guild.id,
setting.value,
role.id,
)

await interaction.response.send_message(
await interaction.followup.send(
f"Perm level {setting.value} role set to {role.mention}.",
ephemeral=True,
delete_after=30,
)

@roles.command(name="set")
Expand Down Expand Up @@ -156,13 +157,13 @@ async def config_set_roles(
"""

assert interaction.guild
await interaction.response.defer(ephemeral=True)

if setting.value == "jail_role_id":
await self.db.update_jail_role_id(interaction.guild.id, role.id)
await interaction.response.send_message(
await interaction.followup.send(
f"{setting.value} role set to {role.mention}.",
ephemeral=True,
delete_after=30,
)

@roles.command(name="get")
Expand All @@ -182,6 +183,7 @@ async def config_get_roles(
"""

assert interaction.guild
await interaction.response.defer(ephemeral=True)

embed = EmbedCreator.create_embed(
title="Config - Roles",
Expand All @@ -194,7 +196,7 @@ async def config_get_roles(
jail_role = f"<@&{jail_role_id}>" if jail_role_id else "Not set"
embed.add_field(name="Jail Role", value=jail_role, inline=False)

await interaction.response.send_message(embed=embed, ephemeral=True, delete_after=30)
await interaction.followup.send(embed=embed, ephemeral=True)

@perms.command(name="get")
@app_commands.guild_only()
Expand All @@ -213,6 +215,7 @@ async def config_get_perms(
"""

assert interaction.guild
await interaction.response.defer(ephemeral=True)

embed = EmbedCreator.create_embed(
embed_type=EmbedType.INFO,
Expand All @@ -227,7 +230,7 @@ async def config_get_perms(
role = f"<@&{role_id}>" if role_id else "Not set"
embed.add_field(name=f"Perm Level {i}", value=role, inline=True)

await interaction.response.send_message(embed=embed, ephemeral=True, delete_after=30)
await interaction.followup.send(embed=embed, ephemeral=True)

@channels.command(name="get")
@app_commands.guild_only()
Expand All @@ -246,6 +249,7 @@ async def config_get_channels(
"""

assert interaction.guild
await interaction.response.defer(ephemeral=True)

embed = EmbedCreator.create_embed(
title="Config - Channels",
Expand All @@ -266,7 +270,7 @@ async def config_get_channels(
general_channel = f"<#{general_channel_id}>" if general_channel_id else "Not set"
embed.add_field(name="General Channel", value=general_channel, inline=False)

await interaction.response.send_message(embed=embed, ephemeral=True, delete_after=30)
await interaction.followup.send(embed=embed, ephemeral=True)

@logs.command(name="get")
@app_commands.guild_only()
Expand All @@ -285,6 +289,7 @@ async def config_get_logs(
"""

assert interaction.guild
await interaction.response.defer(ephemeral=True)

embed = EmbedCreator.create_embed(
title="Config - Logs",
Expand Down Expand Up @@ -317,7 +322,7 @@ async def config_get_logs(
dev_log = f"<#{dev_log_id}>" if dev_log_id else "Not set"
embed.add_field(name="Dev Log", value=dev_log, inline=True)

await interaction.response.send_message(embed=embed, ephemeral=True, delete_after=30)
await interaction.followup.send(embed=embed, ephemeral=True)

@prefix.command(name="set")
@app_commands.guild_only()
Expand All @@ -339,10 +344,11 @@ async def config_set_prefix(
"""

assert interaction.guild
await interaction.response.defer(ephemeral=True)

await self.db.update_guild_prefix(interaction.guild.id, prefix)

await interaction.response.send_message(
await interaction.followup.send(
embed=EmbedCreator.create_embed(
bot=self.bot,
user_name=interaction.user.name,
Expand Down Expand Up @@ -370,10 +376,11 @@ async def config_clear_prefix(
"""

assert interaction.guild
await interaction.response.defer(ephemeral=True)

await self.db.delete_guild_prefix(interaction.guild.id)

await interaction.response.send_message(
await interaction.followup.send(
embed=EmbedCreator.create_embed(
bot=self.bot,
user_name=interaction.user.name,
Expand Down
47 changes: 30 additions & 17 deletions tux/cogs/moderation/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import asyncio
from datetime import datetime

import discord
Expand Down Expand Up @@ -99,8 +100,8 @@ async def send_embed(
if isinstance(log_channel, discord.TextChannel):
await log_channel.send(embed=embed)

@staticmethod
async def send_dm(
self,
ctx: commands.Context[Tux],
silent: bool,
user: discord.Member,
Expand All @@ -123,11 +124,9 @@ async def send_dm(
action : str
The action being performed.
"""

if not silent:
try:
await user.send(f"You have been {action} from {ctx.guild} for the following reason:\n> {reason}")

except (discord.Forbidden, discord.HTTPException) as e:
logger.warning(f"Failed to send DM to {user}. {e}")
return False
Expand Down Expand Up @@ -174,7 +173,7 @@ async def check_conditions(
title="You cannot self-moderate",
description=f"You cannot {action} yourself.",
)
await ctx.send(embed=embed, ephemeral=True, delete_after=30)
await ctx.send(embed=embed, ephemeral=True)
return False

if isinstance(moderator, discord.Member) and user.top_role >= moderator.top_role:
Expand All @@ -186,7 +185,7 @@ async def check_conditions(
title="You cannot self-moderate",
description=f"You cannot {action} a user with a higher or equal role.",
)
await ctx.send(embed=embed, ephemeral=True, delete_after=30)
await ctx.send(embed=embed, ephemeral=True)
return False

if user == ctx.guild.owner:
Expand All @@ -198,7 +197,7 @@ async def check_conditions(
title="You cannot self-moderate",
description=f"You cannot {action} the server owner.",
)
await ctx.send(embed=embed, ephemeral=True, delete_after=30)
await ctx.send(embed=embed, ephemeral=True)
return False

return True
Expand All @@ -213,11 +212,32 @@ async def handle_case_response(
dm_sent: bool,
duration: str | None = None,
):
"""
Handle the response for a case.
Parameters
----------
ctx : commands.Context[Tux]
The context of the command.
case_type : CaseType
The type of case.
case_number : int | None
The case number.
reason : str
The reason for the case.
user : discord.Member | discord.User
The target of the case.
dm_sent : bool
Whether the DM was sent.
duration : str | None, optional
The duration of the case, by default None.
"""

moderator = ctx.author

fields = [
("Moderator", f"__{moderator}__\n`{moderator.id}`", True),
("Target", f"__{user}__\n`{user.id}`", True),
("Moderator", f"**{moderator}**\n`{moderator.id}`", True),
("Target", f"**{user}**\n`{user.id}`", True),
("Reason", f"> {reason}", False),
]

Expand All @@ -242,13 +262,9 @@ async def handle_case_response(
icon_url=CONST.EMBED_ICONS["ACTIVE_CASE"],
)

if dm_sent:
embed.description = "A DM has been sent to the user."
else:
embed.description = "DMs are disabled for this user."
embed.description = "-# DM successful" if dm_sent else "-# DM unsuccessful"

await self.send_embed(ctx, embed, log_type="mod")
await ctx.send(embed=embed, delete_after=30, ephemeral=True)
await asyncio.gather(self.send_embed(ctx, embed, log_type="mod"), ctx.send(embed=embed, ephemeral=True))

async def is_pollbanned(self, guild_id: int, user_id: int) -> bool:
"""
Expand All @@ -267,9 +283,6 @@ async def is_pollbanned(self, guild_id: int, user_id: int) -> bool:
True if the user is poll banned, False otherwise.
"""

# ban_cases = await self.case_controller.get_all_cases_by_type(guild_id, CaseType.POLLBAN)
# unban_cases = await self.case_controller.get_all_cases_by_type(guild_id, CaseType.POLLUNBAN)

ban_cases = await self.db.case.get_all_cases_by_type(guild_id, CaseType.POLLBAN)
unban_cases = await self.db.case.get_all_cases_by_type(guild_id, CaseType.POLLUNBAN)

Expand Down
4 changes: 3 additions & 1 deletion tux/cogs/moderation/ban.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,15 @@ async def ban(
if not await self.check_conditions(ctx, member, moderator, "ban"):
return

await ctx.defer(ephemeral=True)

try:
dm_sent = await self.send_dm(ctx, flags.silent, member, flags.reason, "banned")
await ctx.guild.ban(member, reason=flags.reason, delete_message_days=flags.purge_days)

except (discord.Forbidden, discord.HTTPException) as e:
logger.error(f"Failed to ban {member}. {e}")
await ctx.send(f"Failed to ban {member}. {e}", delete_after=30, ephemeral=True)
await ctx.send(f"Failed to ban {member}. {e}", ephemeral=True)
return

case = await self.db.case.insert_case(
Expand Down
27 changes: 15 additions & 12 deletions tux/cogs/moderation/cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,14 @@ def __init__(self, bot: Tux) -> None:
)
@commands.guild_only()
@checks.has_pl(2)
async def cases(self, ctx: commands.Context[Tux]) -> None:
async def cases(self, ctx: commands.Context[Tux], case_number: int | None) -> None:
"""
Manage moderation cases in the server.
"""
if ctx.invoked_subcommand is None:
if case_number is not None:
await ctx.invoke(self.cases_view, number=case_number, flags=CasesViewFlags())

if case_number is None and ctx.subcommand_passed is None:
await ctx.send_help("cases")

@cases.command(
Expand Down Expand Up @@ -111,13 +114,13 @@ async def cases_modify(

# If the command is used via prefix, let the user know to use the slash command
if ctx.message.content.startswith(str(ctx.prefix)):
await ctx.send("Please use the slash command for this command.", delete_after=30, ephemeral=True)
await ctx.send("Please use the slash command for this command.", ephemeral=True)
return

case = await self.db.case.get_case_by_number(ctx.guild.id, number)

if not case:
await ctx.send("Case not found.", delete_after=30, ephemeral=True)
await ctx.send("Case not found.", ephemeral=True)
return

if case.case_number is not None:
Expand All @@ -143,7 +146,7 @@ async def _view_single_case(

case = await self.db.case.get_case_by_number(ctx.guild.id, number)
if not case:
await ctx.send("Case not found.", delete_after=30)
await ctx.send("Case not found.", ephemeral=True)
return

user = self.bot.get_user(case.case_user_id)
Expand Down Expand Up @@ -183,7 +186,7 @@ async def _view_cases_with_flags(
total_cases = await self.db.case.get_all_cases(ctx.guild.id)

if not cases:
await ctx.send("No cases found.")
await ctx.send("No cases found.", ephemeral=True)
return

await self._handle_case_list_response(ctx, cases, len(total_cases))
Expand All @@ -210,7 +213,7 @@ async def _update_case(
assert ctx.guild

if case.case_number is None:
await ctx.send("Failed to update case.", delete_after=30, ephemeral=True)
await ctx.send("Failed to update case.", ephemeral=True)
return

updated_case = await self.db.case.update_case(
Expand All @@ -221,7 +224,7 @@ async def _update_case(
)

if updated_case is None:
await ctx.send("Failed to update case.", delete_after=30, ephemeral=True)
await ctx.send("Failed to update case.", ephemeral=True)
return

user = await commands.UserConverter().convert(ctx, str(case.case_user_id))
Expand Down Expand Up @@ -282,7 +285,7 @@ async def _handle_case_response(
description="Failed to find case.",
)

await ctx.send(embed=embed, delete_after=30, ephemeral=True)
await ctx.send(embed=embed, ephemeral=True)

async def _handle_case_list_response(
self,
Expand All @@ -298,7 +301,7 @@ async def _handle_case_list_response(
title="Cases",
description="No cases found.",
)
await ctx.send(embed=embed, delete_after=30, ephemeral=True)
await ctx.send(embed=embed, ephemeral=True)
return

cases_per_page = 10
Expand Down Expand Up @@ -326,8 +329,8 @@ def _create_case_fields(
reason: str,
) -> list[tuple[str, str, bool]]:
return [
("Moderator", f"__{moderator}__\n`{moderator.id}`", True),
("User", f"__{user}__\n`{user.id}`", True),
("Moderator", f"**{moderator}**\n`{moderator.id}`", True),
("User", f"**{user}**\n`{user.id}`", True),
("Reason", f"> {reason}", False),
]

Expand Down
Loading

0 comments on commit 3539631

Please sign in to comment.