import aiosqlite import discord from discord.ui import Button, View DATABASE = "nessa.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 store_user_consent(interaction.user.id) await interaction.response.edit_message( content="Hey there! Thanks for" " opt-ing in to allowing me to" " store the data for your " "projects.", view=None, ) self.stop() @discord.ui.button(label="Decline Data Storage", style=discord.ButtonStyle.grey) async def cancel(self, interaction: discord.Interaction, button: Button): self.value = False await interaction.response.edit_message( content="Awe. It's ok. I won't" " store your data but this" "also means you won't be able " "to use my services. Bye!", view=None, ) self.stop() async def check_user_consent(user_id): """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() 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() async def revoke_user_consent(user_id): """Revoke the user's consent to data storage.""" async with aiosqlite.connect(DATABASE) as db: await db.execute( "UPDATE user_consents SET consent_given = FALSE" "WHERE user_id = ?", (user_id,), ) await db.commit()