Search code examples
pythondiscorddiscord.py

Discord.py cant load cogs


i tried to link the cogs in my main.py. But it always give an error. We also Check if some package are missing but it dont. The bot start without the listdir but after we try to link it it doesnt work. we also write: class Help(commands.Cog): def init(self, client): self.client = client Into the whole py's. But still not working.

import discord
import os
import sys
import asyncio
import importlab
import importlib
from discord.ext import commands

TOKEN = "Tokeninhere"

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

@client.event
async def on_ready():
    print('Bot is ready')
    activity = discord.Activity(type=discord.ActivityType.listening, name=F"{PREFIX}help")
    await client.change_presence(status=discord.Status.idle, activity=activity)

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

The Error that pops up:

Traceback (most recent call last):
Traceback (most recent call last):
  File "main.py", line 23, in <module>
    client.load_extension(f'./Cog/{filename[:-3]}')
  File "/home/runner/Tijon-Bot-Main/venv/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 670, in load_extension
    name = self._resolve_name(name, package)
  File "/home/runner/Tijon-Bot-Main/venv/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 629, in _resolve_name
    return importlib.util.resolve_name(name, package)
  File "/nix/store/p21fdyxqb3yqflpim7g8s1mymgpnqiv7-python3-3.8.12/lib/python3.8/importlib/util.py", line 32, in resolve_name
    raise ValueError(f'no package specified for {repr(name)} '
ValueError: no package specified for './Cog/mods' (required for relative module names)
 

Thanks for help!


Solution

  • Try this:

     client.load_extension(f'Cog.{filename[:-3]}')
    

    And make sure you have a setup function in your cog.

    def setup(bot):
        client.add_cog(Help(bot))