Discord py cogs not loading commands

Viewed 406

I made a discord bot with a main file and one other file of commands that are loaded with cogs and the problem is that the commands are not loaded.

I simplified the code triny to resolve the error but even this does not work:

bot.py:

import discord
from discord.ext import commands

import config

# declaring intents and launching client
intents = discord.Intents.default()
intents.members = True
intents.presences = True

bot = commands.Bot(command_prefix='.', description="Useful bot", intents=intents)

startup_extensions = ["private_channels"]

# listener
@bot.event
async def on_ready():
    print(f'Connected to Discord as {bot.user} !')

@bot.event
async def on_message(message):
    if message.author == bot.user:
        return
    print("message received")

# starting extensions
for extension in startup_extensions:
    try:
        bot.load_extension(extension)
    except Exception as e:
        exc = '{}: {}'.format(type(e).__name__, e)
        print('Failed to load extension {}\n{}'.format(extension, exc))

# starting bot
bot.run(config.token)

private_channels.py:

from discord.ext import commands

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

    # creates a new private channel
    @commands.command(name="test")
    async def test(self, ctx):
        await ctx.send("test")

def setup(bot):
    print("Loading private channels")
    bot.add_cog(PrivateChannels(bot))
    print("Private channels loaded")
1 Answers

Okay, the main problem that I had is that on_message event overwrites the commands. To correct that, I added await bot.process_commands(message) to on_message to process commands and now it works.

Related