Skip to content

Commit

Permalink
Merge pull request #339 from PyBotDevs/allow-users-to-delete-their-data
Browse files Browse the repository at this point in the history
Add `/delete_my_data` command to allow any user to delete all of their isobot data permanently
  • Loading branch information
notsniped authored Mar 22, 2024
2 parents 83a6158 + 45d21c9 commit 255285c
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 3 deletions.
14 changes: 13 additions & 1 deletion framework/isobot/currency.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ class CurrencyAPI(Colors):
- get_user_networth(user)
- get_user_count
- new_wallet(user)
- new_bank(user)"""
- new_bank(user)
- delete_user(user)"""

def __init__(self, db_path: str, log_path: str):
self.db_path = db_path
Expand Down Expand Up @@ -175,3 +176,14 @@ def new_bank(self, user: int) -> int:
if str(id) not in currency['bank']:
currency['bank'][str(id)] = 0
return 0

def delete_user(self, user: int) -> int:
"""Permanently deletes all user data for the respective user."""
currency = self.load()
if str(user) in currency['wallet']: del currency['wallet'][str(user)]
if str(user) in currency['bank']: del currency['bank'][str(user)]
self.save(currency)
with open(self.log_path, 'a') as f:
f.write(f'{self.get_time()} framework.isobot.currency User({user}): Successfully deleted all user data from currency database.\n')
f.close()
return 0
7 changes: 7 additions & 0 deletions framework/isobot/db/items.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,10 @@ def fetch_item_count(self, user_id: int, item: str) -> int:
"""Fetches and returns the amount of a specific item owned by the user."""
items = self.load()
return items[str(user_id)][item]

def delete_user(self, user_id: int) -> int:
"""Deletes all user items data for the respective user."""
items = self.load()
del items[str(user_id)]
self.save(items)
return 0
7 changes: 7 additions & 0 deletions framework/isobot/db/levelling.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,10 @@ def get_raw(self):
"""Fetches and returns the raw json data in the levelling database."""
levels = self.load()
return levels

def delete_user(self, user_id: int) -> int:
"""Deletes all user levelling data for the respective user."""
levels = self.load()
del levels[str(user_id)]
self.save(levels)
return 0
7 changes: 7 additions & 0 deletions framework/isobot/db/userdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,10 @@ def set(self, user_id: int, key: str, value) -> int:
userdat[str(user_id)][key] = value
self.save(userdat)
return 0

def delete_user(self, user_id: int) -> int:
"""Deletes all user data for the respective user."""
userdat = self.load()
del userdat[str(user_id)]
self.save(userdat)
return 0
7 changes: 7 additions & 0 deletions framework/isobot/db/weather.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,10 @@ def get_scale(self, user_id: User):
def get_default_location(self, user_id: User):
user_db = self.load()
return user_db[str(user_id)]["location"]

def delete_user(self, user_id: int) -> int:
"""Deletes all user weather data for the respective user."""
user_db = self.load()
del user_db[str(user_id)]
self.save(user_db)
return 0
31 changes: 29 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
from utils import logger, ping
from math import floor
from random import randint
from framework.isobot import currency, colors, settings
from framework.isobot.db import levelling, items, userdata, automod, presence as _presence
from framework.isobot import currency, colors, settings, commands as _commands
from framework.isobot.shop import ShopData
from framework.isobot.db import levelling, items, userdata, automod, weather, presence as _presence
from discord import ApplicationContext, option
from discord.ext import commands
from cogs.isocoin import create_isocoin_key
Expand Down Expand Up @@ -64,6 +65,9 @@ def initial_setup():
userdata = userdata.UserData()
automod = automod.Automod()
_presence = _presence.Presence()
weather = weather.Weather()
_commands = _commands.Commands()
shop_data = ShopData("config/shop.json")

# Theme Loader
if api.auth.get_runtime_options()["themes"]:
Expand Down Expand Up @@ -305,6 +309,29 @@ async def levelup_messages(ctx: ApplicationContext, enabled: bool):
)
await ctx.respond(embed=localembed)

# Extra commands
@client.slash_command(
name="delete_my_data",
description="Deletes all of your isobot data permanently."
)
async def delete_my_data(ctx: ApplicationContext):
"""Deletes all of your isobot data permanently."""
currency.delete_user(ctx.author.id)
levelling.delete_user(ctx.author.id)
items.delete_user(ctx.author.id)
userdata.delete_user(ctx.author.id)
weather.delete_user(ctx.author.id)
localembed = discord.Embed(title=":white_check_mark: Successfully deleted all of your isobot data.", color=discord.Color.green())
localembed.add_field(
name="What has been deleted",
value="- Your currency wallet and bank data\n- Your user levels\n- Items in your inventory\n- Weather save data\n- All of your isobot user settings"
)
localembed.add_field(
name="What has not been deleted",
value="- Any isobot data relating to your servers"
)
await ctx.respond(embed=localembed, ephemeral=True)

@client.slash_command(
name="credits",
description="See the credits for isobot's development."
Expand Down

0 comments on commit 255285c

Please sign in to comment.