Fixed the consent so now it requires it all the time
This commit is contained in:
@ -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)
|
||||
self.stop()
|
||||
|
||||
|
||||
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:
|
||||
cursor = await db.execute("SELECT consent_given FROM user_consents WHERE user_id = ?", (user_id,))
|
||||
result = await cursor.fetchone()
|
||||
if result is None:
|
||||
# Assume consent given if no record exists
|
||||
return True
|
||||
return result[0]
|
||||
|
||||
return result[0] if result else False # Return False if no record exists, prompting consent dialogue
|
||||
|
||||
async def store_user_consent(user_id):
|
||||
"""Store the user's consent to data storage."""
|
||||
|
@ -20,7 +20,8 @@ async def init_db():
|
||||
deadline TEXT,
|
||||
status TEXT,
|
||||
priority TEXT,
|
||||
FOREIGN KEY(project_id) REFERENCES projects(id))'''
|
||||
FOREIGN KEY(project_id) REFERENCES projects(id))
|
||||
'''
|
||||
)
|
||||
await db.execute(
|
||||
'''CREATE TABLE IF NOT EXISTS user_consents(
|
||||
@ -48,20 +49,20 @@ async def get_project_name(project_id):
|
||||
result = await cursor.fetchone()
|
||||
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:
|
||||
# Change the date format to MM/DD/YYYY
|
||||
deadline_date = datetime.strptime(deadline, "%m/%d/%Y").date()
|
||||
await db.execute("INSERT INTO tasks(project_id, description, assignee, deadline, status, priority) VALUES(?, ?, ?, ?, ?, ?)",
|
||||
(project_id, description, assignee, deadline_date, status, priority))
|
||||
await db.execute(
|
||||
"INSERT INTO tasks (project_id, description, assignee, deadline, status, priority, reminder_time) VALUES (?, ?, ?, ?, ?, ?, ?)",
|
||||
(project_id, description, assignee, deadline, status, priority, reminder_time)
|
||||
)
|
||||
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:
|
||||
# Change the date format to MM/DD/YYYY
|
||||
deadline_date = datetime.strptime(deadline, "%m/%d/%Y").date()
|
||||
await db.execute("UPDATE tasks SET description=?, assignee=?, deadline=?, status=?, priority=? WHERE id=?",
|
||||
(description, assignee, deadline_date, status, priority, task_id))
|
||||
await db.execute(
|
||||
"UPDATE tasks SET description=?, assignee=?, deadline=?, status=?, priority=?, reminder_time=? WHERE id=?",
|
||||
(description, assignee, deadline, status, priority, reminder_time, task_id)
|
||||
)
|
||||
await db.commit()
|
||||
|
||||
async def list_projects():
|
||||
|
@ -16,6 +16,7 @@ class Dolly(discord.Client):
|
||||
|
||||
async def setup_hook(self):
|
||||
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))
|
||||
|
||||
@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):
|
||||
if interaction.type == discord.InteractionType.application_command:
|
||||
# Check if the command is about managing consent, which should be allowed regardless
|
||||
if interaction.command.name in ['opt-in', 'opt-out']:
|
||||
return # Let these commands process normally
|
||||
# First, check if the user has consented to data storage.
|
||||
consented = await check_user_consent(interaction.user.id)
|
||||
|
||||
# Check user consent
|
||||
if not await check_user_consent(interaction.user.id):
|
||||
# Present the consent view if no consent is recorded
|
||||
if not consented:
|
||||
# If there is no consent, show the consent dialog,
|
||||
# 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()
|
||||
await interaction.response.send_message(
|
||||
"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
|
||||
)
|
||||
await view.wait()
|
||||
if not view.value:
|
||||
# If after the view they still haven't consented, halt further processing
|
||||
if view.value:
|
||||
await store_user_consent(interaction.user.id)
|
||||
else:
|
||||
await interaction.followup.send("You must consent to data storage to use this bot.", ephemeral=True)
|
||||
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
|
Reference in New Issue
Block a user