简体   繁体   中英

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}")

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.

在使用guild.#something的地方使用ctx.guild不需要使用guild = client.get_guild()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM