hyperrr
hyperrr

Reputation: 97

Cogs discord.py

I am trying to move my discord.py code into cogs. I keep on getting the same error Exception has occurred: ModuleNotFoundError No module named 'cogs'.

bot.py

from discord.ext import commands
bot = commands.Bot(command_prefix='!')

bot.load_extension('cogs.maincog')

bot.run('token')

maincog.py

from discord.ext import commands


class MainCog(commands.Cog):
    def __init__(self, bot):
        self.bot = bot

    @commands.command()
    async def test(self, ctx):
        await ctx.send("test")


def setup(bot):
    bot.add_cog(MainCog(bot))

Folder:

enter image description here

Thanks!

Upvotes: 2

Views: 2279

Answers (3)

user19932233
user19932233

Reputation:

Although I answered a bit lately, I believe discord has changed and your code won't work anymore since everything has to be, now, awaited. So here is your correct code: bot.py:

import os #this will be used later
import discord
from discord.ext import commands

intents = discord.Intents.default() #make sure to define the intents
intents.message_content = True #enable the message content intent

bot = commands.Bot(command_prefix='!', intents = intents) #define the bot and add the intents

token = "your-token"

cogs = ["maincog"]
async def load_cogs():
    for cogs in admincogs:
        try:
            await bot.load_extension(f'cogs.admin.{cogs.lower()}')
            print(f'{cogs} cog loaded.')
        except Exception as e:
            print(f'Failed to load {cogs} cog: {e}')

@bot.event
async def on_ready():
    await load_cogs()
    print('We have logged in as {0.user}'.format(bot))

@bot.event
async def on_message(message):
    if message.author == bot.user:
        return
    await bot.process_commands(message)

bot.run(token)

In you maincogs.py:

from discord.ext import commands


class MainCog(commands.Cog):
    def __init__(self, bot):
        self.bot = bot

    @commands.command()
    async def test(self, ctx):
        await ctx.send("test")


async def setup(bot):
    await bot.add_cog(MainCog(bot))

I hope this helps!

Upvotes: 0

Letícia Sousa
Letícia Sousa

Reputation: 31

The bot.py don't need to be included on the folder, so put it out and this should work, but there are some improvements:

bot.py (Out of the folder)

import os
from discord.ext import commands

client = commands.Bot(command_prefix='!')
# If you don't want the default help command and want to make by yourself, you can use this:
client.remove_command('help')

# With this event, always when your bot is online, it will be printed on the terminal:
@client.event
async def on_ready():
    print('Bot Online!')

# This causes all files inside the folder with the ending .py to be loaded, without having to load one by one:
for filename in os.listdir('cogs'):
    if filename.endswith('.py'):
        client.load_extension(f'cogs.{filename[:-3]}')

client.run('token')
  • OBS: I writed "client" but you can replace it for "bot" if you want to. It's just the way I prefer.
  • OBS2: If you keep this with "client", replace the "bot" on maincog.py by "client", just like this:

maincog.py (Inside the folder)

from discord.ext import commands


class MainCog(commands.Cog):
    def __init__(self, client):
        self.author = None
        self.client = client

    @commands.command()
    async def test(self, ctx):
        await ctx.send('test')


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

Upvotes: 1

Zachary
Zachary

Reputation: 570

Because maincog is in the same directory as bot, cogs. doesn't need to be included. This is because python will look for a subdirectory called cogs which is not needed in your case. New Bot.py code:

from discord.ext import commands
bot = commands.Bot(command_prefix='!')

bot.load_extension('maincog')

bot.run('token')

Upvotes: 1

Related Questions