From 4d34daa1e74cfbefdbba588d8062a93ef98c6869 Mon Sep 17 00:00:00 2001 From: Soenke Date: Sun, 31 May 2026 17:20:22 +0200 Subject: [PATCH] added db test commands --- cogs/database.py | 15 +++++++++++++++ commands/db_test.py | 24 ++++++++++++++++++++++-- main.py | 2 +- 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/cogs/database.py b/cogs/database.py index 74afc1b..e47580a 100644 --- a/cogs/database.py +++ b/cogs/database.py @@ -7,6 +7,21 @@ class Database: async def connect(self): if self.conn is None: self.conn = await aiosqlite.connect("FemBot.db") + + try: + await self.conn.execute(""" + CREATE TABLE IF NOT EXISTS test ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + message TEXT NOT NULL + ) + """) + + await self.conn.commit() + print("Table check/creation completed successfully.") + + except aiosqlite.Error as e: + print(f"Failed to check/creation table: {e}") + return self.conn async def close(self): diff --git a/commands/db_test.py b/commands/db_test.py index 6c8929d..c39637e 100644 --- a/commands/db_test.py +++ b/commands/db_test.py @@ -11,9 +11,9 @@ class DBTest(commands.Cog): await interaction.response.defer(ephemeral=True) try: - db_conn = self.bot.database.conn + db = self.bot.database.conn - async with db_conn.execute("SELECT 1;") as cursor: + async with db.execute("SELECT 1;") as cursor: result = await cursor.fetchone() if result: @@ -25,5 +25,25 @@ class DBTest(commands.Cog): except Exception as e: await interaction.followup.send(f"Database connection failed: {e}") + @app_commands.command(name="test_db_add", description="Add a string to the Database") + @app_commands.describe(message="a message") + async def test_db(self, interaction: discord.Interaction, message: str): + await interaction.response.defer(ephemeral=True) + + try: + db = self.bot.database.conn + + await db.execute( + "INSERT INTO test (message) VALUES (?)", + (message,) + ) + await db.commit() + + await interaction.followup.send("Database INSERT was successful!") + + + except Exception as e: + await interaction.followup.send(f"Database INSERT failed: {e}") + async def setup(bot: commands.Bot): await bot.add_cog(DBTest(bot)) \ No newline at end of file diff --git a/main.py b/main.py index 5a6e75a..7e15854 100644 --- a/main.py +++ b/main.py @@ -10,7 +10,7 @@ load_dotenv() intents = discord.Intents.default() intents.message_content = True -bot = commands.Bot(command_prefix="Warum Kann das nicht weg??? :C", intents=intents) +bot = commands.Bot(command_prefix=None, intents=intents) @bot.event async def on_ready():