discord.py机器人未创建频道

时间:2020-06-29 23:21:59

标签: python discord.py

我正在为命令编写代码,该命令将使我的机器人将某个类别的名称(由用户指定)保存到.YAML文件中,然后向其中添加语音通道,如果不存在该通道已经,还添加了一个文本通道。

class ComplexObject {
    public readonly FirstEnum First; // ~50 different values
    public readonly IFirstModificator FirstModificator; // 4 implementations x 15 values (~60 values total)
    public readonly InternalObject[] Internal; //1-10 values in array => ~4500 different values

    static Dictionary<Type, int> FirstModMap = new[] { typeof(FirstModificator1), typeof(FirstModificator2), typeof(FirstModificator3), typeof(FirstModificator4) }
                                                .Select((t, n) => new { t, n })
                                                .ToDictionary(tn => tn.t, tn => tn.n + 1);
    public override int GetHashCode() {
        var hc = (int)First; // use 6 bits
        // assume IFirstModificator.Value values are 0 - 14 or normalize to be so
        hc = hc << 6 + (FirstModificator.Value * FirstModMap[FirstModificator.GetType()]); // uses 6 bits
        // assume InternalObject[] order matters
        hc = hc << 12 + Internal.Select((io, n) => io.GetHashCode() * (n + 1)).Sum(); // uses 13 bits

        return hc;
    }
}

问题是,尽管代码将创建并写入.YAML文件,但代码没有执行其他操作。 IDE甚至没有显示错误。

有人可以帮我找到问题吗?

1 个答案:

答案 0 :(得分:0)

您的错误处理程序正在抑制该异常。您需要添加

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