added db test commands
This commit is contained in:
1 parent
d564756511
commit
4d34daa1e7
3 files changed
+38
-3
No files matched your search
@@ -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):
|
||||
|
||||
+22
-2
@@ -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))
|
||||
Reference in new issue
Block a user