42 lines
1.9 KiB
Python
42 lines
1.9 KiB
Python
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 store_user_consent(interaction.user.id)
|
|
await interaction.response.edit_message(content="Thank you for consenting to data storage!", 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="You have declined data storage. Since you have, You can't use my services.", 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 # Return False if no record exists, prompting consent dialogue
|
|
|
|
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() |