Hello! It looks like there are a couple of issues in your script related to the usage of asynchronous functions. Specifically, the bot.load_extension() method is synchronous and should not be awaited. Here's how you can fix your script:
-
Remove await from bot.load_extension(extension): Since load_extension is a synchronous method, you should not use await with it.
-
Change load_extensions to a regular function: Since there are no asynchronous calls within load_extensions, it doesn't need to be an asynchronous function.
-
Adjust the main function: You can keep it as an asynchronous function if you plan to add asynchronous pre-start tasks later, but it's not necessary in this context.
Here’s the corrected script:
# main.py
import discord
from discord.ext import commands
import os
import sys
import asyncio
from config import TOKEN # Ensure TOKEN is defined in config.py
# Intents
intents = discord.Intents.default()
intents.message_content = True
intents.members = True # Required for member-related events
# Bot instance
bot = commands.Bot(
command_prefix='!', # Placeholder; not used for slash commands
intents=intents,
description="A modular Discord bot with global slash commands and embeds."
)
@bot.event
async def on_ready():
print(f'Logged in as {bot.user} (ID: {bot.user.id})')
print('------')
try:
# Sync all global commands
synced = await bot.tree.sync()
print(f"Synced {len(synced)} global command(s).")
except Exception as e:
print(f"Failed to sync commands: {e}")
# Function to load all cogs
def load_extensions():
for folder in os.listdir('./cogs'):
cog_path = os.path.join('./cogs', folder)
if os.path.isdir(cog_path):
for file in os.listdir(cog_path):
if file.endswith('.py') and file != '__init__.py':
extension = f'cogs.{folder}.{file[:-3]}'
try:
bot.load_extension(extension)
print(f'Loaded extension: {extension}')
except Exception as e:
print(f'Failed to load extension {extension}: {e}')
# Main function to start the bot
def main():
load_extensions()
bot.run(TOKEN)
if __name__ == '__main__':
main()
Explanation:
-
Removed await from bot.load_extension(extension): Since load_extension is not an asynchronous method, using await would cause an error.
-
Changed load_extensions to a regular function: There's no need for it to be asynchronous as it doesn't contain any await statements.
-
Adjusted the main function: Now, it simply calls load_extensions() and then starts the bot with bot.run(TOKEN). There's no need for asyncio.run(main()) unless you have asynchronous tasks before starting the bot.
Additional Notes:
-
Avoid using asyncio.run() with bot.run(): Using asyncio.run() and bot.run() together can cause event loop conflicts. bot.run() is the recommended way to start the bot as it handles the event loop internally.
-
Using async with bot: If you need to perform asynchronous setup tasks before the bot starts (e.g., database connections), you can use an asynchronous main function with async with bot. However, for your current script, it's unnecessary.
Example with Asynchronous Main Function (Optional):
If you plan to include asynchronous operations before starting the bot, you can structure your script like this:
# ... (rest of your imports and code remains the same)
# Main asynchronous function to start the bot
async def main():
load_extensions()
async with bot:
await bot.start(TOKEN)
# Run the main function
asyncio.run(main())
In this version:
Conclusion:
Your script should now work correctly without errors related to awaiting synchronous methods. Remember to ensure that your config.py contains the correct TOKEN value and that your cogs directory is structured properly with valid cog files.
If you encounter any further issues, feel free to ask!