From bbba4ae726bfb1db5e7196d4950b032fd2bef95a Mon Sep 17 00:00:00 2001 From: salmonik Date: Thu, 15 Jan 2026 14:37:14 +0100 Subject: asyncio issues when starting --- main.py | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/main.py b/main.py index 30c365f..ea5d7c9 100644 --- a/main.py +++ b/main.py @@ -2,8 +2,7 @@ import logging import os import sys import json - -# create a pycord bot template +import asyncio import discord from discord.ext import commands from dotenv import load_dotenv @@ -18,8 +17,11 @@ logging.basicConfig( ) logger = logging.getLogger(__name__) -bot = commands.Bot(intents=discord.Intents.all()) +# Create a new event loop and set it as the current loop +loop = asyncio.new_event_loop() +asyncio.set_event_loop(loop) +bot = commands.Bot(intents=discord.Intents.all(), loop=loop) @bot.event async def on_ready(): @@ -39,8 +41,8 @@ async def on_ready(): data = json.load(f) assert 'volume' in data and isinstance(data['volume'], float) assert 'audio_paths' in data and isinstance(data['audio_paths'], list) - except (json.JSONDecodeError, AssertionError) as e: - logger.error(f"Error loading settings.json: {e}") + except (json.JSONDecodeError, AssertionError) as error: + logger.error(f"Error loading settings.json: {error}") with open('settings.json', 'w') as f: json.dump({ 'volume': 1.0, @@ -65,8 +67,18 @@ if __name__ == "__main__": 'cogs.music.stop', 'cogs.music.join', ] - for i, extension in enumerate(extensions, 1): - bot.load_extension(extension) - logger.info(f"Loaded {i}/{len(extensions)} extensions") - -bot.run(str(os.getenv("TOKEN"))) + + try: + for i, extension in enumerate(extensions, 1): + bot.load_extension(extension) + logger.info(f"Loaded {i}/{len(extensions)} extensions") + + # Run the bot with the token from environment variables + token = os.getenv("TOKEN") + if not token: + raise ValueError("No token provided. Please set the TOKEN environment variable.") + + bot.run(token) + except Exception as e: + logger.error(f"Error starting the bot: {e}") + sys.exit(1) -- cgit v1.2.3