diff options
| author | salmonik <raisersoft@proton.me> | 2026-01-15 14:37:14 +0100 |
|---|---|---|
| committer | salmonik <raisersoft@proton.me> | 2026-01-15 14:37:14 +0100 |
| commit | bbba4ae726bfb1db5e7196d4950b032fd2bef95a (patch) | |
| tree | ba09845c77a547a5eed680beb3278599154e32c2 /main.py | |
| parent | more yt-dlp improvements (diff) | |
| download | senator-sperma-bbba4ae726bfb1db5e7196d4950b032fd2bef95a.tar.gz senator-sperma-bbba4ae726bfb1db5e7196d4950b032fd2bef95a.zip | |
asyncio issues when starting
Diffstat (limited to 'main.py')
| -rw-r--r-- | main.py | 32 |
1 files changed, 22 insertions, 10 deletions
| @@ -2,8 +2,7 @@ import logging | |||
| 2 | import os | 2 | import os |
| 3 | import sys | 3 | import sys |
| 4 | import json | 4 | import json |
| 5 | 5 | import asyncio | |
| 6 | # create a pycord bot template | ||
| 7 | import discord | 6 | import discord |
| 8 | from discord.ext import commands | 7 | from discord.ext import commands |
| 9 | from dotenv import load_dotenv | 8 | from dotenv import load_dotenv |
| @@ -18,8 +17,11 @@ logging.basicConfig( | |||
| 18 | ) | 17 | ) |
| 19 | logger = logging.getLogger(__name__) | 18 | logger = logging.getLogger(__name__) |
| 20 | 19 | ||
| 21 | bot = commands.Bot(intents=discord.Intents.all()) | 20 | # Create a new event loop and set it as the current loop |
| 21 | loop = asyncio.new_event_loop() | ||
| 22 | asyncio.set_event_loop(loop) | ||
| 22 | 23 | ||
| 24 | bot = commands.Bot(intents=discord.Intents.all(), loop=loop) | ||
| 23 | 25 | ||
| 24 | @bot.event | 26 | @bot.event |
| 25 | async def on_ready(): | 27 | async def on_ready(): |
| @@ -39,8 +41,8 @@ async def on_ready(): | |||
| 39 | data = json.load(f) | 41 | data = json.load(f) |
| 40 | assert 'volume' in data and isinstance(data['volume'], float) | 42 | assert 'volume' in data and isinstance(data['volume'], float) |
| 41 | assert 'audio_paths' in data and isinstance(data['audio_paths'], list) | 43 | assert 'audio_paths' in data and isinstance(data['audio_paths'], list) |
| 42 | except (json.JSONDecodeError, AssertionError) as e: | 44 | except (json.JSONDecodeError, AssertionError) as error: |
| 43 | logger.error(f"Error loading settings.json: {e}") | 45 | logger.error(f"Error loading settings.json: {error}") |
| 44 | with open('settings.json', 'w') as f: | 46 | with open('settings.json', 'w') as f: |
| 45 | json.dump({ | 47 | json.dump({ |
| 46 | 'volume': 1.0, | 48 | 'volume': 1.0, |
| @@ -65,8 +67,18 @@ if __name__ == "__main__": | |||
| 65 | 'cogs.music.stop', | 67 | 'cogs.music.stop', |
| 66 | 'cogs.music.join', | 68 | 'cogs.music.join', |
| 67 | ] | 69 | ] |
| 68 | for i, extension in enumerate(extensions, 1): | 70 | |
| 69 | bot.load_extension(extension) | 71 | try: |
| 70 | logger.info(f"Loaded {i}/{len(extensions)} extensions") | 72 | for i, extension in enumerate(extensions, 1): |
| 71 | 73 | bot.load_extension(extension) | |
| 72 | bot.run(str(os.getenv("TOKEN"))) | 74 | logger.info(f"Loaded {i}/{len(extensions)} extensions") |
| 75 | |||
| 76 | # Run the bot with the token from environment variables | ||
| 77 | token = os.getenv("TOKEN") | ||
| 78 | if not token: | ||
| 79 | raise ValueError("No token provided. Please set the TOKEN environment variable.") | ||
| 80 | |||
| 81 | bot.run(token) | ||
| 82 | except Exception as e: | ||
| 83 | logger.error(f"Error starting the bot: {e}") | ||
| 84 | sys.exit(1) | ||
