# Implements SQLite3 database functionality import sqlite3 import os from datetime import datetime, timedelta from contextlib import closing import discord ### Create and setup databse, if it doesn't exist if not os.path.exists("database.db"): open("database.db", "w").close() # Create database file # Create table with closing(sqlite3.connect("database.db")) as connection: with closing(connection.cursor()) as cursor: cursor.execute("CREATE TABLE IF NOT EXISTS ready (user_id INTEGER PRIMARY KEY, time INTEGER)") ### Functions def insert_time(interaction, hours): # Get the timestamp hours from now: user_id = interaction.user.id time = datetime.now() + timedelta(hours=hours) time_int = int(round(time.timestamp())) with closing(sqlite3.connect("database.db")) as connection: with closing(connection.cursor()) as cursor: # TODO: This doesn't actually seem to INSERT anything. Why? try: cursor.execute("INSERT OR REPLACE INTO ready VALUES (?, ?)", (user_id, time_int)) print(f"User {user_id} ready until {time}") except sqlite3.Error as e: print(f"SQLite error: {e}") def remove_time(interaction): user_id = interaction.user.id with closing(sqlite3.connect("database.db")) as connection: with closing(connection.cursor()) as cursor: try: cursor.execute("DELETE FROM ready WHERE user_id = ?", (user_id)) print(f"User {user_id} no longer ready") except sqlite3.Error as e: print(f"SQLite error: {e}")