Fixed the consent so now it requires it all the time

This commit is contained in:
Dan
2024-05-05 12:38:44 -04:00
parent f0da9a3241
commit 4ab30887d9
3 changed files with 32 additions and 26 deletions

View File

@ -22,17 +22,12 @@ class ConsentView(View):
await interaction.response.edit_message(content="You have declined data storage. You cannot use this bot without consenting.", view=None) await interaction.response.edit_message(content="You have declined data storage. You cannot use this bot without consenting.", view=None)
self.stop() self.stop()
async def check_user_consent(user_id): async def check_user_consent(user_id):
"""Check if the user has given consent to data storage, default to True.""" """Check if the user has given consent to data storage. Assume no consent if no record exists."""
async with aiosqlite.connect(DATABASE) as db: async with aiosqlite.connect(DATABASE) as db:
cursor = await db.execute("SELECT consent_given FROM user_consents WHERE user_id = ?", (user_id,)) cursor = await db.execute("SELECT consent_given FROM user_consents WHERE user_id = ?", (user_id,))
result = await cursor.fetchone() result = await cursor.fetchone()
if result is None: return result[0] if result else False # Return False if no record exists, prompting consent dialogue
# Assume consent given if no record exists
return True
return result[0]
async def store_user_consent(user_id): async def store_user_consent(user_id):
"""Store the user's consent to data storage.""" """Store the user's consent to data storage."""

View File

@ -20,7 +20,8 @@ async def init_db():
deadline TEXT, deadline TEXT,
status TEXT, status TEXT,
priority TEXT, priority TEXT,
FOREIGN KEY(project_id) REFERENCES projects(id))''' FOREIGN KEY(project_id) REFERENCES projects(id))
'''
) )
await db.execute( await db.execute(
'''CREATE TABLE IF NOT EXISTS user_consents( '''CREATE TABLE IF NOT EXISTS user_consents(
@ -48,20 +49,20 @@ async def get_project_name(project_id):
result = await cursor.fetchone() result = await cursor.fetchone()
return result[0] if result else None return result[0] if result else None
async def add_task_to_project(project_id, description, assignee, deadline, status, priority): async def add_task_to_project(project_id, description, assignee, deadline, status, priority, reminder_time=None):
async with aiosqlite.connect(DATABASE) as db: async with aiosqlite.connect(DATABASE) as db:
# Change the date format to MM/DD/YYYY await db.execute(
deadline_date = datetime.strptime(deadline, "%m/%d/%Y").date() "INSERT INTO tasks (project_id, description, assignee, deadline, status, priority, reminder_time) VALUES (?, ?, ?, ?, ?, ?, ?)",
await db.execute("INSERT INTO tasks(project_id, description, assignee, deadline, status, priority) VALUES(?, ?, ?, ?, ?, ?)", (project_id, description, assignee, deadline, status, priority, reminder_time)
(project_id, description, assignee, deadline_date, status, priority)) )
await db.commit() await db.commit()
async def update_task(task_id, description, assignee, deadline, status, priority): async def update_task(task_id, description, assignee, deadline, status, priority, reminder_time=None):
async with aiosqlite.connect(DATABASE) as db: async with aiosqlite.connect(DATABASE) as db:
# Change the date format to MM/DD/YYYY await db.execute(
deadline_date = datetime.strptime(deadline, "%m/%d/%Y").date() "UPDATE tasks SET description=?, assignee=?, deadline=?, status=?, priority=?, reminder_time=? WHERE id=?",
await db.execute("UPDATE tasks SET description=?, assignee=?, deadline=?, status=?, priority=? WHERE id=?", (description, assignee, deadline, status, priority, reminder_time, task_id)
(description, assignee, deadline_date, status, priority, task_id)) )
await db.commit() await db.commit()
async def list_projects(): async def list_projects():

View File

@ -16,6 +16,7 @@ class Dolly(discord.Client):
async def setup_hook(self): async def setup_hook(self):
self.tree.add_command(DollyTracker()) self.tree.add_command(DollyTracker())
self.tree.copy_global_to(guild=discord.Object(id=GUILD_ID))
await self.tree.sync(guild=discord.Object(id=GUILD_ID)) await self.tree.sync(guild=discord.Object(id=GUILD_ID))
@self.tree.command(name="shutdown", description="Shut down the bot", guild=discord.Object(id=GUILD_ID)) @self.tree.command(name="shutdown", description="Shut down the bot", guild=discord.Object(id=GUILD_ID))
@ -33,13 +34,15 @@ class Dolly(discord.Client):
async def on_interaction(self, interaction: discord.Interaction): async def on_interaction(self, interaction: discord.Interaction):
if interaction.type == discord.InteractionType.application_command: if interaction.type == discord.InteractionType.application_command:
# Check if the command is about managing consent, which should be allowed regardless # First, check if the user has consented to data storage.
if interaction.command.name in ['opt-in', 'opt-out']: consented = await check_user_consent(interaction.user.id)
return # Let these commands process normally
# Check user consent if not consented:
if not await check_user_consent(interaction.user.id): # If there is no consent, show the consent dialog,
# Present the consent view if no consent is recorded # unless the command is to opt-out, which should be accessible without prior consent.
if interaction.command.name == 'opt-out':
# Allow users to opt-out directly if they mistakenly initiated any command.
return
view = ConsentView() view = ConsentView()
await interaction.response.send_message( await interaction.response.send_message(
"By using this bot, you consent to the storage of your data necessary for functionality. Please confirm your consent.", "By using this bot, you consent to the storage of your data necessary for functionality. Please confirm your consent.",
@ -47,7 +50,14 @@ class Dolly(discord.Client):
ephemeral=True ephemeral=True
) )
await view.wait() await view.wait()
if not view.value: if view.value:
# If after the view they still haven't consented, halt further processing await store_user_consent(interaction.user.id)
else:
await interaction.followup.send("You must consent to data storage to use this bot.", ephemeral=True) await interaction.followup.send("You must consent to data storage to use this bot.", ephemeral=True)
return # Stop processing if they do not consent return # Stop processing if they do not consent
# For opt-in command, check if they're trying to opt-in after opting out.
if interaction.command.name == 'opt-in':
if consented:
await interaction.response.send_message("You have already consented.", ephemeral=True)
return