Search code examples
pythondiscorddiscord.pypython-os

discord.ext.commands.errors.ExtensionNotFound: Extension 'cogs.random' could not be loaded


I'm trying to run my discord bot with commands extension so i can store all my commands separately under commands folder, however every time i want to run the bot, i get bunch of errors in terminal, here is everything:

Terminal: ./bot.py

Traceback (most recent call last):
  File "/home/xinto/.local/lib/python3.6/site-packages/discord/ext/commands/bot.py", line 621, in load_extension
    lib = importlib.import_module(name)
  File "/usr/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'cogs.random'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "handlerbot.py", line 36, in <module>
    client.load_extension(f'cogs.{filename[:-3]}')
  File "/home/xinto/.local/lib/python3.6/site-packages/discord/ext/commands/bot.py", line 623, in load_extension
    raise errors.ExtensionNotFound(name, e) from e
discord.ext.commands.errors.ExtensionNotFound: Extension 'cogs.random' could not be loaded.

bot.py:

#!/usr/bin/python3.6

import discord
from dotenv import load_dotenv
import random
from discord.ext import commands
import os

load_dotenv()
TOKEN = os.getenv('TOKEN')

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

#this script types "Connected!" in terminal if nothing has gone wrong    
@client.event
async def on_ready():
    print('Connected!')        

#this script makes sure that bot doesn't reply to itself
@client.event
async def on_message(message):
    if message.author == client.user:
        return

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

@client.command()
async def unload(ctx, extension):
    client.unload_extension(f'cogs.{extension}')

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

client.run(TOKEN)

random.py:

import discord
import os
from discord.ext import commands

class random(commands.Cog):

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

    @commands.command()
    async def random(self, ctx):
        for filename in os.listdir(./images):
        list = '1.jpg', '2.jpg', '3.jpg', '4.jpg', '5.jpg', '6.jpg', '7.jpg', '8.jpg', '9.jpg', '10.jpg', '11.jpg', '12.jpg'
        await ctx.send('take this', file=discord.File(random.choice(list)))

def setup(client):
    client.add_cog(random(client))

I can't blame random.py as it does the same with other my commands, I've checked the code and couldn't find any reasons why it shouldn't work. any help would be highly appreciated, Thanks!


Solution

  • Okay, I figured this out. I had to rename f'cogs{extension} To f'commands{extension}

    If anyone has same problem, you have to rename cogs with the folder name in which you have commands, I.e for my case it's named commands, hope I helped someone