adding a consent requirement

This commit is contained in:
Dan
2024-05-04 21:30:49 -04:00
parent 2f2f9ab568
commit c7791fc4aa
3 changed files with 65 additions and 8 deletions

37
dolly/consent.py Normal file
View File

@ -0,0 +1,37 @@
import discord
from discord.ui import View, Button
import aiosqlite
DATABASE = "dolly.db"
class ConsentView(View):
def __init__(self):
super().__init__()
self.value = None
@discord.ui.button(label="Consent to Data Storage", style=discord.ButtonStyle.green)
async def confirm(self, interaction: discord.Interaction, button: Button):
self.value = True
await interaction.response.send_message("Thank you for consenting to data storage!", ephemeral=True)
self.stop()
@discord.ui.button(label="Decline", style=discord.ButtonStyle.grey)
async def cancel(self, interaction: discord.Interaction, button: Button):
self.value = False
await interaction.response.send_message("You have declined data storage. You cannot use this bot without consenting.", ephemeral=True)
self.stop()
async def check_user_consent(user_id):
"""Check if the user has given consent to data storage."""
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()
return result[0] if result else False
async def store_user_consent(user_id):
"""Store the user's consent to data storage."""
async with aiosqlite.connect(DATABASE) as db:
await db.execute("INSERT OR REPLACE INTO user_consents (user_id, consent_given) VALUES (?, TRUE)", (user_id,))
await db.commit()

View File

@ -22,6 +22,12 @@ async def init_db():
priority TEXT,
FOREIGN KEY(project_id) REFERENCES projects(id))'''
)
await db.execute(
'''CREATE TABLE IF NOT EXISTS user_consents(
user_id INTEGER PRIMARY KEY,
consent_given BOOLEAN NOT NULL
)'''
)
await db.commit()
async def add_project(name, description):
@ -47,7 +53,7 @@ async def add_task_to_project(project_id, description, assignee, deadline, statu
# 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))
(project_id, description, assignee, deadline_date, status, priority))
await db.commit()
async def update_task(task_id, description, assignee, deadline, status, priority):

View File

@ -1,25 +1,23 @@
import discord
from discord import app_commands
from .commands import DollyTracker
from .consent import ConsentView, check_user_consent, store_user_consent
from dotenv import load_dotenv
import os
load_dotenv()
GUILD_ID = int(os.getenv("DISCORD_GUILD_ID"))
OWNER_ID = int(os.getenv("AUTHORIZED_USER_ID")) # Ensure your .env has your Discord user ID
OWNER_ID = int(os.getenv("AUTHORIZED_USER_ID"))
class Dolly(discord.Client):
def __init__(self):
super().__init__(intents=discord.Intents.default())
self.tree = app_commands.CommandTree(self)
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))
# Adding a guild-specific shutdown command
@self.tree.command(name="shutdown", description="Shut down the bot", guild=discord.Object(id=GUILD_ID))
async def shutdown(interaction: discord.Interaction):
if interaction.user.id == OWNER_ID:
@ -30,6 +28,22 @@ class Dolly(discord.Client):
async def on_ready(self):
print(f"Logged on as {self.user}!")
# Ensuring commands are only available in specified guild
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))
async def on_interaction(self, interaction: discord.Interaction):
if interaction.type == discord.InteractionType.application_command:
if not await check_user_consent(interaction.user.id):
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.",
view=view,
ephemeral=True
)
await view.wait()
if view.value:
await store_user_consent(interaction.user.id)
else:
# Optionally, handle the case where the user declines consent
await interaction.followup.send("You must consent to data storage to use this bot.", ephemeral=True)
return # Stop processing if they do not consent