I’m new to developing Discord bots using Python and I’m facing some challenges when trying to load cogs. Everything works fine in a single file, but I encounter runtime warnings as soon as I implement cogs.
main.py
import discord
import os
from discord.ext import commands
from dotenv import load_dotenv
load_dotenv()
token = os.getenv('TOKEN')
intents = discord.Intents.default()
intents.messages = True
intents.message_content = True
bot = commands.Bot(command_prefix='!', intents=intents)
@bot.event
async def on_ready():
print("Bot is up and ready!")
@bot.command()
async def load_cog(ctx, cog_name):
try:
bot.load_extension(f'cogs.{cog_name}')
await ctx.send(f'Cog {cog_name} has been loaded.')
except Exception as e:
await ctx.send(f'Error loading cog: {e}')
if __name__ == "__main__":
for filename in os.listdir('./cogs'):
if filename.endswith('.py'):
bot.load_extension(f'cogs.{filename[:-3]}')
bot.run(token)
cogs/sample_game.py
from discord.ext import commands
import random
class SampleGame(commands.Cog):
def __init__(self, bot):
self.bot = bot
@commands.command()
async def start_game(self, ctx):
# Game logic goes here
pass
def setup(bot):
bot.add_cog(SampleGame(bot))
I’m getting runtime warnings that I can’t resolve:
RuntimeWarning: coroutine 'BotBase.load_extension' was never awaited
bot.load_extension(f'cogs.{filename[:-3]}')
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
What am I doing incorrectly with the cog implementation? The bot connects successfully, but these warnings are concerning me.