简体   繁体   English

discord.py bot 未创建通道

[英]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.我正在为一个命令编写代码,该命令将使我的机器人将某个类别的名称(由用户指定)保存到 a.YAML 文件中,然后向其中添加一个语音通道,如果还没有的话,也添加一个文本通道。

@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.问题是,虽然代码将创建并写入 .YAML 文件,但代码什么也不做。 The IDE doesn't even show an error. IDE 甚至没有显示错误。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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