discord.py problems with loading extensions

Viewed 1965

My discord bot is connected to my discord server and all the commands seem to run as intended. As I try to use the load or unload commands, it gives me an error saying "Command raised an exception: ExtensionNotFound: Extension 'cogs.commands' could not be loaded." I have no idea why it says that they aren't loaded yet the commands an events still run that are in the extensions. I have already tried rewriting what I had for the load and unload commands, I tried renaming the extensions to 'Events' and 'Commands'. I am only a beginner and I am thinking I wrote something wrong.Here are the load, unload, reload and setup commands.

@client.command()
async def load(ctx, extension):
  client.load_extension(f'cogs.{extension}')
  print(f'{extension} successfully loaded')

# cog unloader command
@client.command()
async def unload(ctx, extension):
  client.unload_extension(f'cogs.{extension}')
  print(f'{extension} successfully unloaded')

# cog reloader command, unload then load extenion
@client.command()
async def reload(ctx, extension):
  client.unload_extension(f'cogs.{extension}')
  client.load_extension(f'cogs.{extension}')
  print(f'{extension} successfully re-loaded')

# for loop to find cogs folder
for filename in os.listdir('./cogs'):
  if filename.endswith('.py'):
    client.load_extension(f'cogs.{filename[:-3]}')

Here is the other file containing the events and commands that I currently have written as the extension.

from discord.ext import commands

# --EVENTS--
class Events(commands.Cog):

  def __init__(self, client):
    self.client = client

  # Bot online event
  @commands.Cog.listener()
  async def on_ready(self):
    print('GuhBot v3 is online and ready! C:')

  # Member joined Event
  @commands.Cog.listener()
  async def on_member_join(self, member):
    print(f'{member} joined the server. C:') 

  # Member left Event
  @commands.Cog.listener()
  async def on_member_remove(self, member):
    print(f'{member} left the server. :C')

# --MODERATION--
class Moderation(commands.Cog):

  def __init__(self, client):
    self.client = client

  # clear command. default 5 messages, can be changed by user.
  @commands.command()
  async def clear(self, ctx, amount=5):
    await ctx.channel.purge(limit=amount+1)

# Cog Setup 
def setup(client):
  client.add_cog(Events(client))
  client.add_cog(Moderation(client))```    

1 Answers

I know it's late but better answering now than never. :) If you didn't solve your problem, here is something that might help.

The problem is that cog is not found because you gave him an invalid path (that's why you get ExtensionNotFound exception).

I tried your code like this:

  1. I made this files hierarchy: photo and in the 'cogs' folder I added one file named commands.py;
  1. In main.py I put your first code like this:
from discord.ext import commands
import os 

client = commands.Bot(command_prefix = "!")

@client.command()
async def load(ctx, extension):
  client.load_extension(f'cogs.{extension}')
  print(f'{extension} successfully loaded')

# cog unloader command
@client.command()
async def unload(ctx, extension):
  client.unload_extension(f'cogs.{extension}')
  print(f'{extension} successfully unloaded')

# cog reloader command, unload then load extenion
@client.command()
async def reload(ctx, extension):
  client.unload_extension(f'cogs.{extension}')
  client.load_extension(f'cogs.{extension}')
  print(f'{extension} successfully re-loaded')

# for loop to find cogs folder
for filename in os.listdir('./cogs'):
  if filename.endswith('.py'):
    client.load_extension(f'cogs.{filename[:-3]}')
    
client.run(TOKEN)
  1. In cogs/commands.py file I wrote the cog code like this:
from discord.ext import commands

# --EVENTS--
class Events(commands.Cog):

  def __init__(self, client):
    self.client = client

  # Bot online event
  @commands.Cog.listener()
  async def on_ready(self):
    print('GuhBot v3 is online and ready! C:')

  # Member joined Event
  @commands.Cog.listener()
  async def on_member_join(self, member):
    print(f'{member} joined the server. C:') 

  # Member left Event
  @commands.Cog.listener()
  async def on_member_remove(self, member):
    print(f'{member} left the server. :C')

# --MODERATION--
class Moderation(commands.Cog):

  def __init__(self, client):
    self.client = client

  # clear command. default 5 messages, can be changed by user.
  @commands.command()
  async def clear(self, ctx, amount=5):
    await ctx.channel.purge(limit=amount+1)

# Cog Setup 
def setup(client):
  client.add_cog(Events(client))
  client.add_cog(Moderation(client))
  1. I run the bot and when it's online I typed in Discord !unload commands first, because commands extension is already loaded in main.py file here:
# for loop to find cogs folder
for filename in os.listdir('./cogs'):
  if filename.endswith('.py'):
    client.load_extension(f'cogs.{filename[:-3]}')
  1. Now that extension is unloaded, I typed !load commands and it loaded succesfully.

So make sure paths you give to load_extension and unload_extension functions are correct.

Related