Search code examples
pythondiscorddiscord.py

Extentions and Cogs not working in discord.py v2.1


I am trying to get export my commands in a discord bot I run to a new file, using cogs and extensions. However, the commands do not register, and I'm not sure why. I'll send the code here.

#bot.py
import json
import os
import discord
from discord.ext import commands


# prefix
def get_prefix(client, message):
    with open('prefixes.json', 'r') as f:
        prefixes = json.load(f)
    return prefixes[str(message.guild.id)]

intents = discord.Intents.all()

client = commands.Bot(intents=intents, command_prefix=get_prefix, self_bot=True, strip_after_prefix = True)


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


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


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


@client.event
async def on_ready():
    await client.wait_until_ready()
    print('Task 1/6: Logged in as: {0.user.name} Bots user id: {0.user.id}'.format(client))


client.run("my token")

This is my cogs

#Onstart.py
import discord
from discord.ext import commands, tasks


class onstart(commands.Cog):

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

    @commands.Cog.listener()
    async def on_ready(self):
        print('Logged in as: {0.user.name}\nBots user id: {0.user.id}'.format(self.client))
        print('Discord.py version:')
        print(discord.__version__)
        print('Ready!')

    @commands.command()
    async def ping(self, ctx):
        await ctx.send(f'Ping is {round(self.client.latency * 1000)} ms')

async def setup(client):
    await client.add_cog(onstart(client))
    print("Task 2/6: Onstart.py loaded")

I have tried to read https://discordpy.readthedocs.io/en/stable/migrating.html


Solution

  • Here is a example this should work

    #bot.py
    import json
    import os
    import discord
    from discord.ext import commands
    
    
    # prefix
    def get_prefix(client, message):
        with open('prefixes.json', 'r') as f:
            prefixes = json.load(f)
        return prefixes[str(message.guild.id)]
    
    intents = discord.Intents.all()
    
    class Bot(commands.Bot):
        def __init__(self):
            super().__init__(
                intents=intents, 
                command_prefix=get_prefix, 
                self_bot=True, strip_after_prefix = True
            )
    
    
    client = Bot()
    
    
    
    # cogs
    @client.command()
    async def load(extension):
        await client.load_extension(f'cogs.{extension}')
    
    
    @client.command()
    async def unload(extension):
        await client.unload_extension(f'cogs.{extension}')
    
    
    async def load_extension():
        for filename in os.listdir('./cogs'):
            if filename.endswith('.py'):
                await client.load_extension(f'cogs.{filename[:-3]}')
    
    
    @client.event
    async def on_ready():
        await client.wait_until_ready()
        print('Task 1/6: Logged in as: {0.user.name} Bots user id: {0.user.id}'.format(client))
    
    
    client.run("my token")