0
votes

I'm trying to make a command where it loads a certain cog I created in a "cogs" folder

I've looked through the docs & interwebs (as usual) but nothing worked for me, I might've been doing it wrong or something maybe?

The code is pretty simple but should still work

import discord
from discord.ext import commands
from discord.ext.commands import has_permissions, MissingPermissions
import os

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

@client.event
async def on_ready():
print ("Bot is online!")

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

@client.command()
@has_permissions(administrator=True)
async def unloadchat(ctx, extension):

 client.unload_extension("cogs.chat")

 await ctx.send('Unloaded the "chat" module :thumbsup: ')

client.run('TOKEN')

This isn't the whole bot of course but it's what I'm trying to do, & yes I added in the token, just not making it public

1
Are you getting an error? Is that loop in on_ready? Try adding a print inside the loop that prints all the filenames, to make sure you're in the right directory. Try sticking a print in setup in cogs.chat to make sure it's being called. - Patrick Haugh
It isn't a loop though, I'm confused with that - Jakaboi
The line that begins with for is the start of a loop. - Patrick Haugh
Sorry, I've already fixed this :) - Jakaboi

1 Answers

1
votes

You can try in different ways. Also this will be great

initial_ext = ['cogs.chat']

if __name__ == '__main__':
    for extension in initial_ext:
        try:
            client.load_extension(extension)
        except Exception as e:
            print(f"Failed to load the {extensiom}", file=sys.stderr)
            traceback.print_exc()