39 lines
1.6 KiB
Python
39 lines
1.6 KiB
Python
import asyncio, json, datetime, discord
|
|
from discord.ui import View, Select
|
|
|
|
class TaskSelect(Select):
|
|
def __init__(self, options):
|
|
super().__init__(placeholder="Choose a task to complete...",
|
|
min_values=1, max_values=1, options=options)
|
|
|
|
async def callback(self, interaction: discord.Interaction):
|
|
payload = json.loads(self.values[0])
|
|
tasklist_id = payload["tasklist_id"]
|
|
task_id = payload["task_id"]
|
|
|
|
await interaction.response.defer(thinking=True)
|
|
try:
|
|
service = build_tasks_service()
|
|
task = await asyncio.to_thread(lambda:
|
|
service.tasks().get(tasklist=tasklist_id, task=task_id).execute())
|
|
if not task:
|
|
await interaction.followup.send("Task not found.", ephemeral=True)
|
|
return
|
|
|
|
now = datetime.datetime.utcnow().replace(tzinfo=datetime.timezone.utc).isoformat()
|
|
body = dict(task)
|
|
body["status"] = "completed"
|
|
body["completed"] = now
|
|
|
|
updated = await asyncio.to_thread(lambda: service.tasks().update(tasklist=tasklist_id, task=task_id, body=body).execute())
|
|
|
|
if updated.get("status") == "completed":
|
|
await interaction.followup.send("Failed to mark completed.", ephemeral=True)
|
|
|
|
except Exception as e:
|
|
await interaction.followup.send(f"Error: {e}", ephemeral=True)
|
|
|
|
class TasksView(View):
|
|
def __init__(self, options, timeout=120):
|
|
super().__init__(timeout=timeout)
|
|
self.add_item(TaskSelect(options)) |