Discord.js - 你如何记录角色变化?

2022-01-10 00:00:00 javascript discord.js

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

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

这就是我想要的:https://imgur.com/a/FRbTpGQ(来自另一个机器人的示例)

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!

推荐答案

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

相关文章