Fixed up the code. Now just need to fix the add_task command

This commit is contained in:
Dan
2024-05-04 12:34:03 -04:00
parent 051ceb8d7d
commit 07ce578edf
5 changed files with 56 additions and 37 deletions

1
.gitignore vendored
View File

@ -158,3 +158,4 @@ cython_debug/
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
/dolly.db

View File

@ -1,16 +1,28 @@
# bot/commands.py
import discord
from discord import app_commands
from .database import add_project, add_task
class DollyProjectTrackerCommands(app_commands.Group, name="Dolly", description="Commands for Dolly's project tracker."):
@app_commands.command(name="create-project", description="Create a new project in the database.")
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):
await add_project(name, description)
await interaction.response.send_message(f"Project `{name}` created successfully.")
@app_commands.command(name="add-task", description="Add a new task to a project in the database.")
await interaction.response.send_message(f"Project '{name}' created successfully.")
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):
await add_task(project_name, description, assignee, deadline, status, priority)
await interaction.response.send_message(f"Task `{description}` added to project `{project_name}` successfully.")
await add_task(project_name, description, assignee, deadline, status, priority)
await interaction.response.send_message(f"Task '{description}' added to project '{project_name}'.")
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())

View File

@ -3,33 +3,34 @@ from datetime import datetime
DATABASE = "dolly.db"
async def setup_db():
async def init_db():
async with aiosqlite.connect(DATABASE) as db:
await db.execute('''CREATE TABLE IF NOT EXISTS projects (
id INTEGER PRIMARY KEY,
name TEXT UNIQUE,
description TEXT)'''
await db.execute(
'''CREATE TABLE IF NOT EXISTS projects(
id INTEGER PRIMARY KEY,
name TEXT UNIQUE,
description TEXT)'''
)
await db.execute('''CREATE TABLE IF NOT EXISTS tasks (
id INTEGER PRIMARY KEY,
project_id INTEGER,
description TEXT,
assignee text,
deadline DATE,
status TEXT,
priority TEXT,
FOREIGN KEY(project_id) REFERENCES projects(id))'''
await db.execute(
'''CREATE TABLE IF NOT EXISTS tasks(
id INTEGER PRIMARY KEY,
project_id INTEGER,
description TEXT,
assignee TEXT,
deadline TEXT,
status TEXT,
priority TEXT,
FOREIGN KEY(project_id) REFERENCES projects(id))'''
)
await db.commit()
async def add_project(name, description):
async with aiosqlite.connect(DATABASE) as db:
await db.execute("INSERT INTO projects (name, description) VALUES (?, ?)", (name, description))
await db.execute("INSERT INTO projects(name, description) VALUES(?, ?)", (name, description))
await db.commit()
async def add_task(project_id, description, assignee, deadline, status, priority):
deadline_date = datetime.strptime(deadline, '%Y-%m-%d').date()
async with aiosqlite.connect(DATABASE) as db:
await db.execute("INSERT INTO tasks (project_id, description, assignee, deadline, status, priority) VALUES (?, ?, ?, ?, ?, ?)",
(project_id, description, assignee, deadline_date, status, priority))
deadline_date = datetime.strptime(deadline, "%Y-%m-%d").date()
await db.execute("INSERT INTO tasks(project_id, description, assignee, deadline, status, priority) VALUES(?, ?, ?, ?, ?, ?)", (project_id, description, assignee, deadline_date, status, priority))
await db.commit()

View File

@ -1,17 +1,22 @@
import discord
from discord import app_commands
from .commands import DollyProjectTrackerCommands
from .commands import DollyTracker
from dotenv import load_dotenv
import os
load_dotenv()
GUILD_ID = int(os.getenv("DISCORD_GUILD_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):
# Register the commands via a group to the command tree
self.tree.add_command(DollyProjectTrackerCommands())
await self.tree.sync()
self.tree.add_command(DollyTracker())
self.tree.copy_global_to(guild = discord.Object(id=GUILD_ID))
await self.tree.sync(guild = discord.Object(id=GUILD_ID))
async def on_ready(self):
print(f"Logged in as {self.user} (ID: {self.user.id})\n------")
print(f"Logged on as {self.user}!")

View File

@ -1,15 +1,15 @@
from dolly.dolly import Dolly
from dolly.database import setup_db
from dolly.database import init_db
import asyncio
from dotenv import load_dotenv
import os
load_dotenv()
TOKEN = os.getenv('DISCORD_BOT_TOKEN')
TOKEN = os.getenv("DISCORD_BOT_TOKEN")
async def main():
await setup_db()
await init_db()
client = Dolly()
await client.start(TOKEN)