From 3b7718dd10d752ee5674d02b1c0f27ca8622cd05 Mon Sep 17 00:00:00 2001 From: JDJG Date: Thu, 25 Apr 2024 00:55:32 -0400 Subject: [PATCH] There --- cogs/commands.py | 26 ++++---------------------- server.py | 21 ++------------------- utils/extra.py | 24 ++++++++++++++++++++++++ 3 files changed, 30 insertions(+), 41 deletions(-) diff --git a/cogs/commands.py b/cogs/commands.py index 2e13dee..678a483 100644 --- a/cogs/commands.py +++ b/cogs/commands.py @@ -2,10 +2,8 @@ import io import json -import tempfile from typing import TYPE_CHECKING -import asqlite import discord from discord import app_commands from discord.ext import commands @@ -91,28 +89,12 @@ async def _data(self, interaction: discord.Interaction): json_response = io.StringIO(json_string) file = discord.File(json_response, filename="user_data.json") - with tempfile.NamedTemporaryFile(mode="w") as f: - # delete on close may be already used - # delete is necessary possibly but idk, I just know delete_on_close=True isn't in python 3.11 - # is delete needed? - # appraently it's also autodeleted - # https://stackoverflow.com/questions/11043372/how-to-use-tempfile-namedtemporaryfile - # https://docs.python.org/3/library/tempfile.html#tempfile.NamedTemporaryFile + oauth_db = await utils.make_oauth_database(data) - # does f even work with it? + sqlite_file = discord.File(oauth_db, filename="data.db") + # needs to use f.name for the location of the file. - async with asqlite.connect(f.name) as conn: - async with conn.cursor() as cursor: - print(conn) - - # I am unsure about what tables in execute right now. - - # unsure how to handle the json response right now will ask dpy for help for that. - - sqlite_file = discord.File(f.name, filename="data.db") - # needs to use f.name for the location of the file. - - files = [file, sqlite_file] + files = [file, sqlite_file] await interaction.response.send_message( "Here's your data(stats will be around in the future)", diff --git a/server.py b/server.py index 7757f95..e11697d 100644 --- a/server.py +++ b/server.py @@ -3,12 +3,10 @@ import json import os import secrets -import tempfile from contextlib import asynccontextmanager from typing import Dict, Optional, Union import aiohttp -import asqlite import asyncpg import discord from dotenv import load_dotenv @@ -108,23 +106,8 @@ async def full_data(response: Response, code: Optional[str] = None, state: Optio json_string = json.dumps(data, indent=4) json_response = io.StringIO(json_string) - # also add support for the asqlite version in the future too. - - with tempfile.NamedTemporaryFile(mode="w") as f: - # delete on close may be already used - # delete is necessary possibly but idk, I just know delete_on_close=True isn't in python 3.11 - # is delete needed? - # appraently it's also autodeleted - # https://stackoverflow.com/questions/11043372/how-to-use-tempfile-namedtemporaryfile - # https://docs.python.org/3/library/tempfile.html#tempfile.NamedTemporaryFile - - # does f even work with it? - - async with asqlite.connect(f.name) as conn: - async with conn.cursor() as cursor: - print(conn) - - # unsure how to handle the json response right now will ask dpy for help for that. + oauth_db = await utils.make_oauth_database(data) + # can also be used for bot stuff and for later if this is moved. # file support diff --git a/utils/extra.py b/utils/extra.py index aeb8cea..d316969 100644 --- a/utils/extra.py +++ b/utils/extra.py @@ -1,5 +1,7 @@ import enum +import tempfile +import asqlite import discord @@ -25,3 +27,25 @@ def stats_builder(data: dict): connections = data["connections"] # unknown for connection stats. nicknames = data["nicknames"] + + +async def make_oauth_database(oauth_data: dict): + # also add support for the asqlite version in the future too. + + with tempfile.NamedTemporaryFile(mode="w") as f: + # delete on close may be already used + # delete is necessary possibly but idk, I just know delete_on_close=True isn't in python 3.11 + # is delete needed? + # appraently it's also autodeleted + # https://stackoverflow.com/questions/11043372/how-to-use-tempfile-namedtemporaryfile + # https://docs.python.org/3/library/tempfile.html#tempfile.NamedTemporaryFile + + # does f even work with it? + + async with asqlite.connect(f.name) as conn: + async with conn.cursor() as cursor: + print(conn) + # could get the names of the colonums and make them their own tables and make json be a text type? + + return f.name + # all you need appreantly. \ No newline at end of file