javascript Discord.js -如何记录角色更改?

mgdq6dx1  于 2023-05-16  发布在  Java
关注(0)|答案(1)|浏览(114)

我在Discord.js v12.2.0上的Discord机器人上设置服务器日志,我目前正在尝试设置角色日志。我在互联网上翻找了一下,我只能在旧版本的Discord.js上找到解决方案,这显然在v12.2.0上不起作用。我已经设置了guildMemberUpdate事件来记录昵称更改,但我不知道如何为角色执行此操作。这可能只是超级简单,但我不知道我会怎么做。
以下是我目前为止的代码:

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;
  }
});

我的目标是https://imgur.com/a/FRbTpGQ(来自另一个bot的示例)
任何帮助将是超级感激。谢谢!

cbjzeqam

cbjzeqam1#

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);
    }
});

相关问题