Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a category and a channel using python discord.py

I am trying to create a function that first creates a category called "management", then it creates a channel named after the member count of my server.

The expected outcome is that the category and channel gets created accordingly as explained above.

This is the code i am trying to use

@client.command()
async def setup_counter(ctx):
    try:
        await ctx.send("Setting up management!")
        await guild.create_category("Management", overwrites=None, reason=None)
        await guild.create_voice_channel(f"Member Count: {guild.member_count}", overwrites=None, category="Management", reason=None)
        await ctx.send("Setup finished!")
    except Exception as errors:
        print(f"Bot Error: {errors}")
like image 712
Roald Andre Kvarv Avatar asked Feb 28 '26 15:02

Roald Andre Kvarv


1 Answers

You need to select on which server (Guild) you want to do that. Additonally you need to pass a reference to the category not just the name of it. If you know the id of your guild, use this:

@client.command()
async def setup_counter(ctx):
    try:
        guild = client.get_guild(id) # <-- insert yor guild id here
        await ctx.send("Setting up management!")
        category = await guild.create_category("Management", overwrites=None, reason=None)
        await guild.create_voice_channel(f"Member Count: {guild.member_count}", overwrites=None, category=category, reason=None)
        await ctx.send("Setup finished!")
    except Exception as errors:
        print(f"Bot Error: {errors}")

If you dont know the guild id just follow this official article where they discuss finding the server id.

like image 73
Jakob F Avatar answered Mar 02 '26 04:03

Jakob F



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!