From e59c6491951fe96d363170d1ab0f13ec1fca6142 Mon Sep 17 00:00:00 2001 From: Rafael Bradley Date: Wed, 2 Apr 2025 00:57:30 -0300 Subject: [PATCH] feat(shard): :sparkles: A few changes. --- payphone/bot.py | 14 +++++++-- payphone/cogs/admin/cog.py | 55 +++++++++++++++++++++++++----------- payphone/cogs/admin/views.py | 7 +++++ uv.lock | 2 +- 4 files changed, 58 insertions(+), 20 deletions(-) diff --git a/payphone/bot.py b/payphone/bot.py index bbede17..65adf67 100644 --- a/payphone/bot.py +++ b/payphone/bot.py @@ -297,7 +297,7 @@ class Payphone(commands.AutoShardedBot): expires_at (datetime | None): When the ban expires. created_by_id (int | None): The ID of the user who created the ban. reason (str | None): The reason for the ban. - + Returns: (Ban): The ban. """ @@ -316,12 +316,13 @@ class Payphone(commands.AutoShardedBot): return ban - async def unban(self, user_id: int, expired_by: int | None) -> None: + async def unban(self, user_id: int, expired_by: int | None, send_notification: bool = True) -> None: """Unbans a user. Args: user_id (int): The user to unban's ID. expired_by (int | None): The user who expired the ban. + send_notification (bool): Whether to send a notification to the user or not. """ await models.Ban.filter( @@ -334,6 +335,15 @@ class Payphone(commands.AutoShardedBot): await self.redis.delete(f"banned:{user_id}") + if send_notification: + try: + await (await self.get_or_fetch_user(user_id)).send( + f"🔔 You have been unbanned from Payphone{f" by <@{expired_by}>" if expired_by else ""}." + ) + + except discord.DiscordException: + pass + async def on_command_error( self, context: commands.Context, exception: commands.CommandError ) -> None: diff --git a/payphone/cogs/admin/cog.py b/payphone/cogs/admin/cog.py index 12e31de..fa67219 100644 --- a/payphone/cogs/admin/cog.py +++ b/payphone/cogs/admin/cog.py @@ -3,6 +3,8 @@ from datetime import datetime, timedelta, timezone import discord from discord.ext import commands, pages +from tortoise import expressions + from payphone.db import models from payphone.bot import Payphone from payphone.cogs import base @@ -109,12 +111,6 @@ class Admin(base.Cog): await ctx.send(f"{user.mention} was unbanned from Payphone.") - try: - await user.send("🔔 You were unbanned from Payphone!") - - except discord.Forbidden: - pass - user: models.User = await models.User.get(id=user.id) await user.set_cache(self.bot.redis) @@ -210,12 +206,13 @@ class Admin(base.Cog): if not bans and not warns: if user.bans_count or user.warns_count: await ctx.send( - "<@!{user_id}> has no bans nor warns registered in the database, but they" - f" have been banned `{user.bans_count}` times and warned `{user.warns_count}`." + f"❓ <@{user_id}> has no bans nor warns recorded, but they have been banned" + f" `{user.bans_count}` times and warned `{user.warns_count}` times.", + allowed_mentions=discord.AllowedMentions(users=False), ) return - await ctx.send(f"<@!{user_id}> has no bans nor warns.") + await ctx.send(f"<@!{user_id}> hasn't been banned nor warned.") return ban_embeds = [ @@ -357,13 +354,13 @@ class Admin(base.Cog): return await ctx.send("❌ Warning not found.") await models.User.filter(id=warn.user_id).update( - warns_count=models.User.warns_count - 1 + warns_count=expressions.F("warns_count") - 1 ) await warn.delete() await ctx.send( - f"✅ Deleted warning #{warn.id} for user <@{warn.user_id}>.", + f"✅ Deleted warning `#{warn.id}` for user <@{warn.user_id}>.", allowed_mentions=discord.AllowedMentions(users=False), ) @@ -385,13 +382,25 @@ class Admin(base.Cog): return await ctx.send("❌ Ban not found.") await models.User.filter(id=ban.user_id).update( - bans_count=models.User.bans_count - 1 + bans_count=expressions.F("bans_count") - 1 ) await ban.delete() + if ( + not await models.Ban.filter( + expressions.Q( + expires_at__gt=datetime.now(timezone.utc), + expires_at=None, + join_type="OR", + ), + expressions.Q(user_id=ban.user_id), + ).exists() + ) and (ban.expires_at is None or ban.expires_at > datetime.now(timezone.utc)): + await self.bot.unban(ban.user_id, ctx.author.id) + await ctx.send( - f"✅ Deleted ban #{ban.id} for user <@{ban.user_id}>.", + f"✅ Deleted ban `#{ban.id}` for user <@{ban.user_id}>.", allowed_mentions=discord.AllowedMentions(users=False), ) @@ -409,15 +418,27 @@ class Admin(base.Cog): - Bot Moderator """ - await models.User.filter(id=user.id).update(bans_count=0, warns_count=0) - await models.Ban.filter(user_id=user.id).delete() - await models.Warn.filter(user_id=user.id).delete() + user_id = user if isinstance(user, int) else user.id if user else ctx.author.id + + await models.User.filter(id=user_id).update(bans_count=0, warns_count=0) + await models.Ban.filter(user_id=user_id).delete() + await models.Warn.filter(user_id=user_id).delete() + + await self.bot.unban(user_id, ctx.author.id, False) await ctx.send( - f"✅ Cleared the record of <@{user.id}>.", + f"✅ Cleared the record of <@{user_id}>.", allowed_mentions=discord.AllowedMentions(users=False), ) + try: + await (await self.bot.get_or_fetch_user(user_id)).send( + f"✅ Your record has been cleared by {ctx.author.mention}. You are free to use the bot again." + ) + + except discord.DiscordException: + pass + def setup(bot: Payphone) -> None: """Loads the Admin cog into the bot. diff --git a/payphone/cogs/admin/views.py b/payphone/cogs/admin/views.py index 7b51ec5..f6abc9e 100644 --- a/payphone/cogs/admin/views.py +++ b/payphone/cogs/admin/views.py @@ -53,6 +53,13 @@ class ReportView(discord.ui.View): self, select: discord.SelectMenu, interaction: discord.Interaction ) -> None: """Handles the action select.""" + interacter = await models.User.get_or_none(id=interaction.user.id) + + if interacter is None or not interacter.is_moderator: + return await interaction.response.send_message( + "❌ You are not allowed to do this. Only moderators can perform moderative actions.", + ephemeral=True, + ) self.report.resolved = True await self.report.save() diff --git a/uv.lock b/uv.lock index 1959c30..5124b2c 100644 --- a/uv.lock +++ b/uv.lock @@ -414,7 +414,7 @@ wheels = [ [[package]] name = "payphone" -version = "0.68.0" +version = "0.69.0" source = { virtual = "." } dependencies = [ { name = "aerich" },