簡體   English   中英

discord.py bot 未創建通道

[英]discord.py bot not creating channels

我正在為一個命令編寫代碼,該命令將使我的機器人將某個類別的名稱(由用戶指定)保存到 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.")

問題是,雖然代碼將創建並寫入 .YAML 文件,但代碼什么也不做。 IDE 甚至沒有顯示錯誤。

誰能幫我找到問題?

您的錯誤處理程序正在抑制異常。 您需要添加

else:
    raise error

因此,如果它無法處理它,它將傳播錯誤。 以下內容適用於我,但我的機器人可能具有與您不同的權限。

@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