Files
Nessa/dolly/commands.py

49 lines
2.6 KiB
Python

import discord
from discord import app_commands
from .database import add_project, get_project_id, add_task_to_project
from datetime import datetime
import logging
logger = logging.getLogger('Dolly')
class ProjectCommands(app_commands.Group):
def __init__(self):
super().__init__(name="project", description="Manage projects.")
@app_commands.command(name="create", description="Create a new project.")
async def create_project(self, interaction: discord.Interaction, name: str, description: str):
try:
project_id = await add_project(name, description)
await interaction.response.send_message(f"Project '{name}' created successfully with ID: {project_id}.")
except Exception as e:
await interaction.response.send_message("An error occurred while creating the project.", ephemeral=True)
logger.error(f"Error in create_project: {e}")
class TaskCommands(app_commands.Group):
def __init__(self):
super().__init__(name="task", description="Manage tasks.")
@app_commands.command(name="add", description="Add a new task to a project.")
async def add_task(self, interaction: discord.Interaction, project_name: str, description: str, assignee: str, deadline: str, status: str, priority: str):
try:
datetime.strptime(deadline, "%m/%d/%Y") # Validate date format
project_id = await get_project_id(project_name)
if project_id:
await add_task_to_project(project_id, description, assignee, deadline, status, priority)
await interaction.response.send_message(f"Task '{description}' added to project '{project_name}'.")
logger.info(f"Task added to project {project_name}: {description}")
else:
await interaction.response.send_message(f"Project '{project_name}' not found.", ephemeral=True)
logger.warning(f"Attempted to add task to non-existent project: {project_name}")
except ValueError:
await interaction.response.send_message("Invalid date format. Please use MM/DD/YYYY format.", ephemeral=True)
logger.error(f"Invalid date format provided by user: {deadline}")
except Exception as e:
await interaction.response.send_message("An error occurred while adding the task.", ephemeral=True)
logger.error(f"Unexpected error in add_task: {e}")
class DollyTracker(app_commands.Group, name="dolly", description="Dolly the Project Tracker."):
def __init__(self):
super().__init__()
self.add_command(ProjectCommands())
self.add_command(TaskCommands())