How do I add a discord category to a server through a bot?

Question:

In order to quickly set up a server, I’m trying to construct a discord bot in Python that accepts the command "!Create {Category name here}" and then generates a category packed with text channels from a list of strings stored in a variable called "textChannelNames". The IDE I’m using is Pycharm.

def run_discord_bot():
    TOKEN = "my discord token"
    intents = discord.Intents.default()
    intents.message_content = True
    client = discord.Client(intents=intents)
    @commands.command("!Create")
    async def addcategory(ctx):
        try:
            await ctx.guild.create_category(categoryName)
            await ctx.send(f"A new category called '{categoryName}' was made")
            for textChannel in textChannelNames:
                addTextChannel(ctx, textChannel)
        except Exception as e:
            print(e)
    async def addTextChannel(ctx, channel_name):
        try:
            guild = ctx.guild
            if ctx.author.guild_permissions.manage_channels:
                await guild.create_text_channel(name='{}'.format(channel_name))
        except Exception as e:
            print(e)
    client.run(TOKEN)

I discovered in debug mode that addcategory does not run but I do not know why. Neither the category nor the text channels are created.

Edit – Here is the fixed code for anyone who has the same issue

def run_discord_bot():
    TOKEN = "my discord token"
    intents = discord.Intents.default()
    intents.message_content = True
    client = discord.Client(intents=intents)
    bot = commands.Bot(command_prefix='!', intents=intents)
    @bot.command(name='create')
    async def addcategory(ctx, categoryName):
        try:
            newCategory = await ctx.guild.create_category(categoryName)
            for textChannel in textChannelNames:
                await addTextChannel(ctx, textChannel, newCategory)
        except Exception as e:
            print(e)
        async def addTextChannel(ctx, channel_name, category):
        guild = ctx.guild
        if ctx.author.guild_permissions.manage_channels:
            newchannel = await guild.create_text_channel(name='{}'.format(channel_name), category=category)
    bot.run(TOKEN)
Asked By: IceWave

||

Answers:

You’re using a Client instead of a Bot, which doesn’t support commands. Hence, your commands are completely ignored by discord.py because Client doesn’t know (or care) that they exist.

If you want commands, use the Commands framework. Docs: https://discordpy.readthedocs.io/en/stable/ext/commands/index.html

Also, your command has a prefix in the name so even if you would be using a Bot it would never get matched anyways. The prefix should be passed to the __init__ of your Bot.

Lastly, you’re not awaiting your addTextChannel function so that won’t do much for you either.

Answered By: stijndcl
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.