Files
Nessa/nessa/consent.py
Dan efd6cdc0d5 Fix: Changed everything to conform to Flake8.
Fix: Added some code to fix the reconnect/disconnect error.
Docs: Added setup.cfg to set everything to be 120.
2024-05-20 09:48:34 -04:00

69 lines
2.2 KiB
Python

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()