Files
Nessa/dolly/dolly.py

54 lines
2.5 KiB
Python

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"))
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())
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))
async def shutdown(interaction: discord.Interaction):
if interaction.user.id == OWNER_ID:
await interaction.response.send_message("Shutting down...")
await self.close()
else:
await interaction.response.send_message("You do not have permission to use this command.", ephemeral=True)
async def on_ready(self):
print(f"Logged on as {self.user}!")
self.tree.copy_global_to(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:
# 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
# Check user consent
if not await check_user_consent(interaction.user.id):
# Present the consent view if no consent is recorded
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 not view.value:
# If after the view they still haven't consented, halt further processing
await interaction.followup.send("You must consent to data storage to use this bot.", ephemeral=True)
return # Stop processing if they do not consent