diff --git a/.gitignore b/.gitignore index b44597c..65c1c2b 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ __pycache__/ *.py[cod] *$py.class -*.env \ No newline at end of file +*.env +*.db \ No newline at end of file diff --git a/cogs/database.py b/cogs/database.py new file mode 100644 index 0000000..74afc1b --- /dev/null +++ b/cogs/database.py @@ -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() \ No newline at end of file diff --git a/commands/create_meet.py b/commands/create_meet.py index 7ea6528..a0d0192 100644 --- a/commands/create_meet.py +++ b/commands/create_meet.py @@ -222,7 +222,7 @@ class CountryDropdownView(discord.ui.View): class MainInteractiveMenu(discord.ui.View): def __init__(self): - super().__init__() + super().__init__(timeout=None) @discord.ui.button(label="Register a Meetup", style=discord.ButtonStyle.success) 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): 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) diff --git a/commands/db_test.py b/commands/db_test.py new file mode 100644 index 0000000..6c8929d --- /dev/null +++ b/commands/db_test.py @@ -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)) \ No newline at end of file diff --git a/main.py b/main.py index 710b577..5a6e75a 100644 --- a/main.py +++ b/main.py @@ -1,6 +1,7 @@ import os import asyncio import discord +from cogs.database import Database from discord.ext import commands from dotenv import load_dotenv @@ -13,8 +14,11 @@ bot = commands.Bot(command_prefix="Warum Kann das nicht weg??? :C", intents=inte @bot.event async def on_ready(): - print(f'Logged in as {bot.user} (ID: {bot.user.id})') - print('------') + print(f"Logged in as {bot.user} (ID: {bot.user.id})") + print("------") + + await bot.database.connect() + print("Database connected!") try: synced = await bot.tree.sync() @@ -22,16 +26,22 @@ async def on_ready(): except Exception as e: print(f"Failed to sync commands: {e}") +@bot.event +async def on_disconnect(): + await bot.database.close() + async def load_extensions(): - for filename in os.listdir('./commands'): - if filename.endswith('.py'): - await bot.load_extension(f'commands.{filename[:-3]}') - print(f'Loaded extension: commands.{filename[:-3]}') + for filename in os.listdir("./commands"): + if filename.endswith(".py"): + await bot.load_extension(f"commands.{filename[:-3]}") + print(f"Loaded extension: commands.{filename[:-3]}") async def main(): async with bot: + bot.database = Database() + await load_extensions() - await bot.start(os.getenv('DISCORD_TOKEN')) + await bot.start(os.getenv("DISCORD_TOKEN")) asyncio.run(main()) \ No newline at end of file