Discord.js roleUpdate事件

时间:2020-02-27 08:20:02

标签: discord discord.js

我正在尝试做的是一个简单的事件记录器,它将在其中输出输出,例如角色是否丢失/获得许可。因此,我想到了将两者进行比较,并简单地将它们相减的方法,因此它仅显示一个特定的权限。相反,我得到了1个未定义(权限丢失,下面的检查代码),而其他则变成空白,因为根本没有文本(权限增加,下面的检查代码)。我已经做到了,比如说公会成员是失去角色还是获得角色,但它的工作方式不同。

我做错了什么,如何从这里继续?

代码:

bot.on("roleUpdate", async (oldRole, newRole) => {


if (oldRole.permissions !== newRole.permissions) {

  if (oldRole.permissions > newRole.permissions) {
    //Permission lost
    let oldPerms = oldRole.serialize()
    let newPerms = newRole.serialize()
    var permLost = Object.keys(oldPerms, newPerms).find(perm => perm[newPerms] - oldPerms)
    const embed = new Discord.RichEmbed()
    .setAuthor(`Role changed permissions`)
    .setColor("#ffc500")
    .setDescription(`**${newRole.toString()} has lost the ${permLost} permission**`)
    .setFooter(`ID: ${newRole.id} ?`)
    .setTimestamp()
    logchannel.send(embed).catch()

} else if (oldRole.permissions < newRole.permissions) {
    //Permission given
    let oldPerms = oldRole.serialize()
    let newPerms = newRole.serialize()
    var permGained = Object.keys(oldPerms, newPerms).filter(perm => perm[newPerms] - oldPerms)
    const embed2 = new Discord.RichEmbed()
    .setAuthor(`Role changed permissions`)
    .setColor("#ffc500")
    .setDescription(`**${newRole.toString()} has been given the ${permGained} permission**`)
    .setFooter(`ID: ${newRole.id} ?`)
    .setTimestamp()
    logchannel.send(embed2).catch()

  }
}

});

1 个答案:

答案 0 :(得分:0)

我更改了一些代码,我认为一切都很好。


client.on("roleUpdate", async (oldRole, newRole) => {

    if (oldRole.permissions !== newRole.permissions) {

        const embed = new Discord.RichEmbed()
            .setAuthor(`Role changed permissions`)
            .setColor("#ffc500")
            .setFooter(`ID: ${newRole.id} ?`)
            .setTimestamp()

        const oldPerms = oldRole.serialize();
        const newPerms = newRole.serialize();

        const permUpdated = [];

        for (const [key, element] of Object.entries(oldPerms)) {
            if (newPerms[key] !== element) permUpdated.push(key);
        }

        if (oldRole.permissions > newRole.permissions) {
            //Permission lost

            embed.setDescription(`**${newRole.toString()} has lost the ${permUpdated.join(", ")} permission**`)
            logchannel.send(embed).catch()

        } else if (oldRole.permissions < newRole.permissions) {
            //Permission given

            embed.setDescription(`**${newRole.toString()} has been given the ${permUpdated.join(", ")} permission**`)
            logchannel.send(embed).catch()

        }
    }

});

我更改了新的嵌入...,因为这些嵌入之间的唯一区别是描述,因此您只需要创建一个嵌入并在发送前设置描述即可。

------- Showing some results