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):
|
async def connect(self):
|
||||||
if self.conn is None:
|
if self.conn is None:
|
||||||
self.conn = await aiosqlite.connect("FemBot.db")
|
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
|
return self.conn
|
||||||
|
|
||||||
async def close(self):
|
async def close(self):
|
||||||
|
|||||||
+22
-2
@@ -11,9 +11,9 @@ class DBTest(commands.Cog):
|
|||||||
await interaction.response.defer(ephemeral=True)
|
await interaction.response.defer(ephemeral=True)
|
||||||
|
|
||||||
try:
|
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()
|
result = await cursor.fetchone()
|
||||||
|
|
||||||
if result:
|
if result:
|
||||||
@@ -25,5 +25,25 @@ class DBTest(commands.Cog):
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
await interaction.followup.send(f"Database connection failed: {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):
|
async def setup(bot: commands.Bot):
|
||||||
await bot.add_cog(DBTest(bot))
|
await bot.add_cog(DBTest(bot))
|
||||||
@@ -10,7 +10,7 @@ load_dotenv()
|
|||||||
intents = discord.Intents.default()
|
intents = discord.Intents.default()
|
||||||
intents.message_content = True
|
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
|
@bot.event
|
||||||
async def on_ready():
|
async def on_ready():
|
||||||
|
|||||||
Reference in new issue
Block a user