Discord.js - How do you log role changes?

倾然丶 夕夏残阳落幕 提交于 2021-01-20 12:33:49

问题


I'm setting up server logs on my Discord bot on Discord.js v12.2.0 and I'm currently trying to set up role logging. I've rummaged around on the internet a bit and I can only find solutions for this on older versions of Discord.js, which obviously don't work on v12.2.0. I've set up the guildMemberUpdate event to log nickname changes, but I simply don't know how to do it for roles. It might just be super simple but I'm not sure how I would go about it.

Here's my code so far:

client.on('guildMemberUpdate', (oldMember, newMember) => {
  if (!oldMember.nickname && newMember.nickname) {
    const membernewnicklog = new Discord.MessageEmbed()
      .setAuthor(`${newMember.user.tag}`, `${newMember.user.displayAvatarURL({ format: "png", dynamic: true })}`)
      .setDescription(`**${newMember} nickname added**`)
      .setFooter(`${newMember.user.username}'s ID: ${newMember.id}`)
      .setTimestamp()
      .setColor('#ffff00')
      .addField("New nickname", newMember.nickname)
    client.channels.cache.get('736996028787589201').send(membernewnicklog);
    return;
  }
  if (oldMember.nickname && !newMember.nickname) {
    const memberremovenicklog = new Discord.MessageEmbed()
      .setAuthor(`${oldMember.user.tag}`, `${oldMember.user.displayAvatarURL({ format: "png", dynamic: true })}`)
      .setDescription(`**${oldMember} nickname removed**`)
      .setFooter(`${oldMember.user.username}'s ID: ${oldMember.id}`)
      .setTimestamp()
      .setColor('#f04747')
      .addField("Old nickname", oldMember.nickname)
    client.channels.cache.get('736996028787589201').send(memberremovenicklog);
    return;
  }
  if (oldMember.nickname && newMember.nickname) {
    const memberchangednicklog = new Discord.MessageEmbed()
      .setAuthor(`${newMember.user.tag}`, `${newMember.user.displayAvatarURL({ format: "png", dynamic: true })}`)
      .setDescription(`**${newMember} nickname changed**`)
      .setFooter(`${newMember.user.username}'s ID: ${newMember.id}`)
      .setTimestamp()
      .setColor('#ff4500')
      .addField("Before", oldMember.nickname)
      .addField("After", newMember.nickname);
    client.channels.cache.get('736996028787589201').send(memberchangednicklog);
    return;
  }
});

And here's what I'm going for: https://imgur.com/a/FRbTpGQ (an example from another bot)

Any help would be super appreciated. Thanks!


回答1:


client.on("guildMemberUpdate", (oldMember, newMember) => {
    // Old roles Collection is higher in size than the new one. A role has been removed.
    if (oldMember.roles.cache.size > newMember.roles.cache.size) {
        // Creating an embed message.
        const Embed = new discord.MessageEmbed();
        Embed.setColor("RED");
        Embed.setAuthor(newMember.user.tag, newMember.user.avatarURL());
        
        // Looping through the role and checking which role was removed.
        oldMember.roles.cache.forEach(role => {
            if (!newMember.roles.cache.has(role.id)) {
                Embed.addField("Role Removed", role);
            }
        });

        client.channels.cache.get("ChannelID").send(Embed);
    } else if (oldMember.roles.cache.size < newMember.roles.cache.size) {
        const Embed = new discord.MessageEmbed();
        Embed.setColor("GREEN");
        Embed.setAuthor(newMember.user.tag, newMember.user.avatarURL());
        
        // Looping through the role and checking which role was added.
        newMember.roles.cache.forEach(role => {
            if (!oldMember.roles.cache.has(role.id)) {
                Embed.addField("Role Added", role);
            }
        });
        client.channels.cache.get("ChannelID").send(Embed);
    }
});


来源:https://stackoverflow.com/questions/63162081/discord-js-how-do-you-log-role-changes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!