SQLite Setup

This commit is contained in:
Soenke committed 2026-05-29 18:18:26 +02:00
1 parent 7882ba4915
commit d564756511
5 files changed
+63 -9

No files matched your search

+1
View File
@@ -3,3 +3,4 @@ __pycache__/
*$py.class *$py.class
*.env *.env
*.db
+14
View File
@@ -0,0 +1,14 @@
import aiosqlite
class Database:
def __init__(self):
self.conn = None
async def connect(self):
if self.conn is None:
self.conn = await aiosqlite.connect("FemBot.db")
return self.conn
async def close(self):
if self.conn:
await self.conn.close()
+2 -2
View File
@@ -222,7 +222,7 @@ class CountryDropdownView(discord.ui.View):
class MainInteractiveMenu(discord.ui.View): class MainInteractiveMenu(discord.ui.View):
def __init__(self): def __init__(self):
super().__init__() super().__init__(timeout=None)
@discord.ui.button(label="Register a Meetup", style=discord.ButtonStyle.success) @discord.ui.button(label="Register a Meetup", style=discord.ButtonStyle.success)
async def primary_button_callback(self, interaction: discord.Interaction, button: discord.ui.Button): async def primary_button_callback(self, interaction: discord.Interaction, button: discord.ui.Button):
@@ -246,7 +246,7 @@ class CreateMeetup(commands.Cog):
async def startup(self): async def startup(self):
await self.bot.wait_until_ready() await self.bot.wait_until_ready()
channel = await self.bot.fetch_channel(os.getenv('CREATE_MEETUP_CHANNEL_ID')) channel = await self.bot.fetch_channel(os.getenv("CREATE_MEETUP_CHANNEL_ID"))
await channel.purge(limit=None, bulk=True) await channel.purge(limit=None, bulk=True)
+29
View File
@@ -0,0 +1,29 @@
import discord
from discord.ext import commands
from discord import app_commands
class DBTest(commands.Cog):
def __init__(self, bot: commands.Bot):
self.bot = bot
@app_commands.command(name="test_db", description="Tests the database connection")
async def test_db(self, interaction: discord.Interaction):
await interaction.response.defer(ephemeral=True)
try:
db_conn = self.bot.database.conn
async with db_conn.execute("SELECT 1;") as cursor:
result = await cursor.fetchone()
if result:
await interaction.followup.send("Database connection successful!")
else:
await interaction.followup.send("Database responded, but returned no data.")
except Exception as e:
await interaction.followup.send(f"Database connection failed: {e}")
async def setup(bot: commands.Bot):
await bot.add_cog(DBTest(bot))
+17 -7
View File
@@ -1,6 +1,7 @@
import os import os
import asyncio import asyncio
import discord import discord
from cogs.database import Database
from discord.ext import commands from discord.ext import commands
from dotenv import load_dotenv from dotenv import load_dotenv
@@ -13,8 +14,11 @@ bot = commands.Bot(command_prefix="Warum Kann das nicht weg??? :C", intents=inte
@bot.event @bot.event
async def on_ready(): async def on_ready():
print(f'Logged in as {bot.user} (ID: {bot.user.id})') print(f"Logged in as {bot.user} (ID: {bot.user.id})")
print('------') print("------")
await bot.database.connect()
print("Database connected!")
try: try:
synced = await bot.tree.sync() synced = await bot.tree.sync()
@@ -22,16 +26,22 @@ async def on_ready():
except Exception as e: except Exception as e:
print(f"Failed to sync commands: {e}") print(f"Failed to sync commands: {e}")
@bot.event
async def on_disconnect():
await bot.database.close()
async def load_extensions(): async def load_extensions():
for filename in os.listdir('./commands'): for filename in os.listdir("./commands"):
if filename.endswith('.py'): if filename.endswith(".py"):
await bot.load_extension(f'commands.{filename[:-3]}') await bot.load_extension(f"commands.{filename[:-3]}")
print(f'Loaded extension: commands.{filename[:-3]}') print(f"Loaded extension: commands.{filename[:-3]}")
async def main(): async def main():
async with bot: async with bot:
bot.database = Database()
await load_extensions() await load_extensions()
await bot.start(os.getenv('DISCORD_TOKEN')) await bot.start(os.getenv("DISCORD_TOKEN"))
asyncio.run(main()) asyncio.run(main())