有没有办法检查何时添加了某人的角色? [Discord.js]

时间:2020-02-17 17:07:40

标签: javascript node.js node-modules discord.js

我正在尝试获取某人担任某个角色的日期,但我不确定该怎么做以及是否真的有任何方法。我一直在寻找这个很长一段时间。我希望获得帮助。

const Discord = require('discord.js');
let Bot = new Discord.Client();

async function GetDate(userid) {
  if (!userid) return message.reply('')
}

Bot.on("message", async(message) => {
    let messageArray = message.content.split(" ");
    let cmd = messageArray[0];
    let args = messageArray.slice(1);
    
    if(cmd === `!GetDate`) {
      let User = args[0]
      
      message.reply(`${User} is a member since 2020-02-17`)
    }
});

1 个答案:

答案 0 :(得分:2)

您可以监听事件guildMemberUpdate,然后以类型memberRoleUpdate获取审核日志,并获取整个审核日志。

bot.on('guildMemberUpdate', (oldMember, newMember) => {
    if(oldMember.roles.size < newmember.roles.size) {
        const fetchedLogs = await oldMember.guild.fetchAuditLogs({
        limit: 1,
        type: 'MEMBER_ROLE_UPDATE',
    });

        const roleAddLog = fetchedLogs.entries.first();
        if (!roleAddLog ) return;
        const { executor, target, extra } = kickLog;
        console.log(`Role ${extra.name} added to ${<@target.id>} by ${<@executor.id>}`)
    }
});

V2

哦,所以您可以通过ID获得角色,并获得createdAdd角色

client.on('message', message => {
    let targetRole = message.guild.roles.get('PLACEROLEID')
    if(targetRole) console.log(targetRole.createdAt)
});