check tasks completed

This commit is contained in:
Tenzing Kandang 2026-04-08 16:29:06 +02:00
parent 4d352c801c
commit e96a1b9161
2 changed files with 46 additions and 9 deletions

View File

@ -21,7 +21,7 @@ class UserLocation:
conn = sqlite3.connect(self.db_name)
cursor = conn.cursor()
cursor.execute('''
INSERT INTO user_locations (user_id, location) VALUES (?, ?) ON CONFLICT(user_id) DO UPDATE SET location=excluded.location''', (user_id, location))
INSERT INTO user_locations (username, user_id, location) VALUES (?, ?) ON CONFLICT(user_id) DO UPDATE SET location=excluded.location''', (username, user_id, location))
conn.commit()
conn.close()
@ -32,3 +32,29 @@ class UserLocation:
row = cursor.fetchone()
conn.close()
return row[0] if row else None
class TaskCompleted:
def __init__(self, db_name='tasks_completed.db'):
self.db_name = db_name
self.create_table()
def create_table(self):
conn = sqlite3.connect(self.db_name)
cursor = conn.execute('''
CREATE TABLE IF NOT EXISTS task_completed (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER NOT NULL,
username TEXT NOT NULL,
task TEXT NOT NULL,
time TEXT NOT NULL
)''')
conn.commit()
conn.close()
def set_info(self, user_id:int, username: str, task: str, time: str):
conn = sqlite3.connect(self.db_name)
cursor = conn.cursor()
cursor.execute('''
INSERT INTO task_completed (user_id, username, task, time) VALUES (?, ?, ?, ?)''', (user_id, username, task, time))
conn.commit()
conn.close()

25
main.py
View File

@ -5,7 +5,7 @@ from googleapiclient.discovery import build
from google.oauth2.credentials import Credentials
from discord.ext import commands
from dotenv import load_dotenv
from bot.database import UserLocation
from bot.database import UserLocation, TaskCompleted
from asyncio import to_thread
from discord import app_commands
from discord.ui import View, Select
@ -23,6 +23,7 @@ intents = discord.Intents(messages=True, guilds=True)
intents.message_content = True
bot = commands.Bot(command_prefix='!', intents=intents)
user_location_db = UserLocation()
task_completed_db = TaskCompleted()
TZ = datetime.datetime.now().astimezone().tzinfo
def build_calendar_service():
@ -86,7 +87,7 @@ class TaskSelect(Select):
await interaction.followup.send("Task already completed.", ephemeral=True)
return
now = datetime.datetime.utcnow().replace(tzinfo=datetime.timezone.utc).isoformat()
now = datetime.datetime.now(TZ).isoformat()
body = dict(task)
body["status"] = "completed"
body["completed"] = now
@ -96,6 +97,11 @@ class TaskSelect(Select):
if updated.get("status") == "completed":
await interaction.followup.send(f"User: {interaction.user.display_name} marked task: '{updated.get('title')}' as completed.", ephemeral=True)
print(f"User {interaction.user.display_name} just completed task {updated.get('title')}")
username = interaction.user.display_name
user_id = interaction.user.id
task = updated.get('title')
time = now
task_completed_db.set_info(user_id, username, task, time)
except Exception as e:
await interaction.followup.send(f"Error: {e}", ephemeral=True)
@ -135,6 +141,8 @@ async def today(interaction: discord.Interaction):
tasklist=tl_id, showCompleted=True, showHidden=True, maxResults=250).execute() or {})
items = tasks_res.get("items", []) or []
for t in items:
if not isinstance(t, dict):
continue
@ -145,6 +153,8 @@ async def today(interaction: discord.Interaction):
if due_date != today and completed_date != today:
continue
print(f"API RESPONSE: {t}")
total += 1
status = "" if t.get("status") == "completed" else "🔲"
t_id = t.get("id")
@ -203,15 +213,15 @@ async def events(interaction: discord.Interaction):
orderBy='startTime'
).execute()
print(f"API Response: {events_result}")
print(f"API RESPONSE: {events_result}")
events = events_result.get('items',[])
if not events:
embed.description = "No tasks due today"
await interaction.followup.send(embed=embed)
await interaction.followup.send("No events this week!")
return
else:
embed = discord.Embed(
title="📅 This week's events",
@ -242,7 +252,8 @@ async def events(interaction: discord.Interaction):
@bot.command(name="set_location")
async def set_location(ctx, *, location: str):
user_id = ctx.author.id
user_location_db.set_user_location(user_id, location)
username = ctx.author.display_name
user_location_db.set_user_location(username, user_id, location)
await ctx.send(f"Location set to: {location}")
@bot.tree.command(name="weather", description="Check the weather!")
@ -259,7 +270,7 @@ async def current_weather(interaction: discord.Interaction, location: str = None
current_weather = weather_client.get_current_weather(location)
print(f"JSON API {current_weather}")
print(f"API RESPONSE {current_weather}")
# Check that current_weather is a dictionary and contains necessary keys
if isinstance(current_weather, dict) and 'main' in current_weather and 'weather' in current_weather: