fix(bot): 🐛 Prevent users from marking their account for deletion multiple times.

This commit is contained in:
Nyeki.py
2024-11-03 00:50:18 -03:00
parent 9765e4c71c
commit 1f8f6e77da
2 changed files with 49 additions and 40 deletions
+1 -1
View File
@@ -512,7 +512,7 @@ class Users(base.Cog):
thumbnail=profile.avatar_url, thumbnail=profile.avatar_url,
) )
await ctx.send(embed=embed, view=views.MeView(ctx.author.id)) await ctx.send(embed=embed, view=views.MeView(user))
@commands.command(aliases=["l", "leader", "ranking"]) @commands.command(aliases=["l", "leader", "ranking"])
async def leaderboard(self, ctx: commands.Context, board: str = "users") -> None: async def leaderboard(self, ctx: commands.Context, board: str = "users") -> None:
+23 -14
View File
@@ -6,26 +6,19 @@ from payphone.db import models
class MeView(discord.ui.View): class MeView(discord.ui.View):
status: int = 0 status: int = 0
def __init__(self, user_id: int) -> None: def __init__(self, user: models.User) -> None:
"""Initializes the view. """Initializes the view.
Args: Args:
user_id (int): The ID of the user to user (models.user): The user to show the view for.
""" """
self.user_id = user_id
super().__init__(disable_on_timeout=True) super().__init__(disable_on_timeout=True)
@discord.ui.button( async def callback(interaction: discord.Interaction) -> None:
label="Delete My Activity Data", style=discord.ButtonStyle.danger
)
async def callback(
self, button: discord.Button, interaction: discord.Interaction
) -> None:
"""Handles the interaction of the button.""" """Handles the interaction of the button."""
if interaction.user.id != self.user_id: if interaction.user.id != user.id:
await interaction.response.send_message( await interaction.response.send_message(
"❌ You cannot delete other person's account.", ephemeral=True "❌ You cannot delete other person's account.", ephemeral=True
) )
@@ -38,14 +31,15 @@ class MeView(discord.ui.View):
self.status += 1 self.status += 1
return return
await models.User.filter(id=self.user_id).update(is_deleting=True) user.is_deleting = True
await user.save()
self.disable_all_items() self.disable_all_items()
self.children[0].label = "Your account will be deleted" self.children[0].label = "Your account will be deleted"
await interaction.response.send_message( await interaction.response.send_message(
( (
"👋 Your account was scheduled for deletion. It may take up to 24 hours for your " "👋 Your account was scheduled for deletion. It may take up to 24 hours for "
"account information to be deleted.\n\n" "your account information to be deleted.\n\n"
"What will be deleted:\n" "What will be deleted:\n"
"- Your messages\n" "- Your messages\n"
"- Your calls\n" "- Your calls\n"
@@ -58,3 +52,18 @@ class MeView(discord.ui.View):
ephemeral=True, ephemeral=True,
) )
await interaction.message.edit(view=self) await interaction.message.edit(view=self)
if not user.is_deleting:
button = discord.ui.Button(
style=discord.ButtonStyle.danger, label="Delete My Activity Data"
)
button.callback = callback
else:
button = discord.ui.Button(
style=discord.ButtonStyle.danger,
label="Your account is being deleted",
disabled=True,
)
self.add_item(button)