From 1112cd83bf92f1c6ab92a2dc3791a7b32bba8cd4 Mon Sep 17 00:00:00 2001 From: tenzi Date: Wed, 25 Mar 2026 21:59:33 +0100 Subject: [PATCH] task completed e --- database.py | 34 ++++++++++++++++++++++++++++++++++ task_bot.py | 37 ++++++++++++++++++------------------- 2 files changed, 52 insertions(+), 19 deletions(-) create mode 100644 database.py diff --git a/database.py b/database.py new file mode 100644 index 0000000..3dad44e --- /dev/null +++ b/database.py @@ -0,0 +1,34 @@ +import sqlite3 + +class UserLocation: + def __init__(self, db_name='user_location.db'): + self.db_name = db_name + self.create_table() + + def create_table(self): + conn = sqlite3.connect(self.db_name) + cursor = conn.cursor() + cursor.execute(''' + CREATE TABLE IF NOT EXISTS user_locations ( + user_id INTEGER PRIMARY KEY, + location TEXT NOT NULL + ) + ''') + conn.commit() + conn.close() + + def set_user_location(self, user_id: int, location: str): + 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)) + conn.commit() + conn.close() + + def get_user_location(self, user_id: int) -> str: + conn = sqlite3.connect(self.db_name) + cursor = conn.cursor() + cursor.execute('SELECT location FROM user_locations WHERE user_id = ?', (user_id,)) + row = cursor.fetchone() + conn.close() + return row[0] if row else None \ No newline at end of file diff --git a/task_bot.py b/task_bot.py index 6e4536d..bc0b6b4 100644 --- a/task_bot.py +++ b/task_bot.py @@ -81,7 +81,7 @@ async def authenticate(): @bot.tree.command(name="daily_tasks", description="Check today's saved tasks") async def today(interaction: discord.Interaction): - await interaction.response.defer() + await interaction.response.defer(thinking=True) try: service = build_tasks_service() tl_res = service.tasklists().list(maxResults=100).execute() @@ -91,30 +91,28 @@ async def today(interaction: discord.Interaction): color=0x2ecc71, timestamp=datetime.datetime.now(tz=TZ) ) - any_tasks = False for tl in lists: tl_id = tl["id"] tl_title = tl.get("title", "Untitled") tasks_res = service.tasks().list(tasklist=tl_id, showCompleted=True, maxResults=200).execute() items = tasks_res.get("items", []) - day_tasks = [t for t in items if is_due_today(t.get("due"))] - if not day_tasks: + if not items: continue - any_tasks = True + lines = [] - for t in sorted(day_tasks, key=lambda x: x.get("due") or ""): - status = "βœ…done" if t.get("status") == "completed" else "πŸ”²" - time = due_time_str(t.get("due")) + for t in items: + status = "βœ…" if t.get("status") == "completed" else "πŸ”²" title = t.get("title", "(no title)") - tid = t.get("id", "") - lines.append(f"{status} {title}{(' - ' + time) if time else ''} (id:{tid})") - value = "\n".join(lines)[:1024] - embed.add_field(name=tl_title, value=value, inline=False) + lines.append(f"{status} {title}") + value = "\n".join(lines)[:1024] + embed.add_field(name=tl_title, value=value, inline=False) + + if not embed.fields: + embed.description = "No tasks due today." + + await interaction.followup.send(embed=embed) - if not any_tasks: - embed.description = "No tasks due today." - await interaction.followup.send(embed=embed) except Exception as e: await interaction.followup.send(f"An error occured:{e}") @@ -167,9 +165,9 @@ async def events(interaction: discord.Interaction): return else: embed = discord.Embed( - title="πŸ“… Weekly Events Summary", - description="This week's events", - color=discord.Color.blue() + title="πŸ“… This week's events", + color=discord.Color.blue(), + timestamp=datetime.datetime.now(tz=TZ) ) for event in events: @@ -223,7 +221,8 @@ async def current_weather(interaction: discord.Interaction, location: str = None embed = discord.Embed( title=f"Current weather in {location}", description=f"Temperature: {temp}Β°C\nWeather condition: {weather_condition}", - color=discord.Color.yellow() + color=discord.Color.yellow(), + timestamp=datetime.datetime.now(tz=TZ) ) embed.set_thumbnail(url=f"https://openweathermap.org/img/wn/{icon}.png")