简体   繁体   中英

discord.py bot not creating channels

I'm writing my code for a command that will make my bot save a certain category's name (specified by the user) to a.YAML file, and then add a voice channel to it and, if one isn't present already, also add a text channel.

@bot.command()
@has_permissions(administrator=True)
async def hub_create(ctx, *category):
    if discord.utils.get(ctx.guild.categories, name=' '.join(category[:])) != None:
        catname = discord.utils.get(ctx.guild.categories, name=' '.join(category[:]))
        with open (r'C:\Users\Cindyarta\PycharmProjects\voicerooms\%s.yaml' % ' '.join(category[:]), 'a+') as file:
            yaml.dump([' '.join(category[:])], file)
            yaml.dump(['0'], file)
            CategoryChannel = catname
            await ctx.guild.create_voice_channel('AFK Room', category=catname)
            if len(CategoryChannel.text_channels) == 0:
                await ctx.guild.create_text_channel('voice-rooms', category=catname)
            await ctx.send("Hub created.")
    else:
        await ctx.send("Not a valid category. Please check if it exists or if I can see it.")
@hub_create.error
async def hub_create_error(ctx, error):
    if isinstance(error, CheckFailure):
        await ctx.send("Not a valid category. Please check if it exists or if I can see it.")

Problem is that while the code will create and write to the.YAML file, the code does nothing else. The IDE doesn't even show an error.

Can anyone help me find the problem?

Your error handler is suppressing the exception. You need to add

else:
    raise error

so it will propagate the error back up if it can't handle it. The below works for me, but my bot might have different permissions than yours.

@bot.command()
@has_permissions(administrator=True)
async def hub_create(ctx, *, category: discord.CategoryChannel):
    with open (r'C:\Users\Cindyarta\PycharmProjects\voicerooms\%s.yaml' % category.name, 'a+') as file:
        yaml.dump([category.name], file)
        yaml.dump(['0'], file)
    await category.create_voice_channel('AFK Room')
    if len(category.text_channels) == 0:
        await category.create_text_channel('voice-rooms')
    await ctx.send("Hub created.")

@hub_create.error
async def hub_create_error(ctx, error):
    if isinstance(error, BadArgument):
        await ctx.send("Not a valid category. Please check if it exists or if I can see it.")
    elif isinstance(error, CheckFailure):
        await ctx.send("Admins only.")
    else:
        raise error

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