如何使用命令userinfo获取用户ID(Discord机器人)

时间:2020-03-12 22:07:54

标签: javascript discord.js

我对编码非常陌生,我的代码遇到问题。我正在使用JS和我的母语。对于我的代码,如果您尝试将用户的ID与命令userinfo一起使用,它将执行作者的userinfo而不是您所做的人员的ID。

    bot.on('message', message => {
    let args = message.content.substring(PREFIX.length).split(" ");
    let msg = message.content.toUpperCase();
    let user = message.mentions.users.first() || message.author;
    const member = message.guild.member(user);
    const joinDiscord = moment(user.createdAt).format('llll');
    const joinServer = moment(user.joinedAt).format('llll');
    const prefix = '/'; 

    if (message.author.bot) return; 
    if (message.channel.type === 'dm') return; 
    if (!user) user = message.user;

    if (msg.startsWith(prefix + "USERINFO")) {
        roles = member.roles.map(roles => roles).filter(r => r.id!=message.guild.id).sort((b,a) => a.position-b.position).join(' **|** ')

        if (roles == "" || roles == undefined) {
            roles = "`No Roles Available`"
        }
        let embed = new Discord.RichEmbed()
            .setAuthor(user.username + '#' + user.discriminator, user.displayAvatarURL)
            .addField('ID', user.id)
            .addField('Nickname', `${member.nickname !== null ? `${member.nickname}` : 'None'}`, true)
            .addField('Status', `${user.presence.status}`, true)
            .addField('Bot', `${user.bot}`, true)
            .addField('Roles', roles)
            .addField('Joined At', `${moment.utc(member.joinedAt).format("MMMM, Do YYYY, h:mm:ss a")}`,)
            .addField('Created At', `${moment.utc(user.createdAt).format("MMMM, Do YYYY, h:mm:ss a")}`,)
            .setThumbnail(`${user.avatarURL}`)
            .setColor(0x33B5FF)

        message.channel.send(embed);  

    } 

});

1 个答案:

答案 0 :(得分:0)

您可以将message.guild.members.get()用于Discord v11,将message.guild.members.cache.get()用于Discord v12

   bot.on('message', message => {
    if (message.author.bot) return; 
    if (message.channel.type === 'dm') return; 
    let targetMember;
    let args = message.content.substring(PREFIX.length).split(" ");
    let msg = message.content.toUpperCase();
    if(args.length === 0) {
        targetMember = message.member;
    } else {
        targetMember = message.mentions.members.first() || message.guild.members.get(args[0])
    }
    if(!targetMember) targetMember = message.member

    const joinDiscord = moment(targetMember.user.createdAt).format('llll');
    const joinServer = moment(targetMember.joinedAt).format('llll');
    const prefix = '/'; 

    if (message.startsWith(prefix + "USERINFO")) {
        roles = targetMember.roles.map(roles => roles).filter(r => r.id!=message.guild.id).sort((b,a) => a.position-b.position).join(' **|** ')

        if (roles == "" || roles == undefined) {
            roles = "`No Roles Available`"
        }
        let embed = new Discord.RichEmbed()
            .setAuthor(targetMember.user.username + '#' + targetMember.user.discriminator, targetMember.user.displayAvatarURL)
            .addField('ID', targetMember.id)
            .addField('Nickname', `${targetMember.nickname !== null ? `${targetMember.nickname}` : 'None'}`, true)
            .addField('Status', `${targetMember.user.presence.status}`, true)
            .addField('Bot', `${targetMember.user.bot}`, true)
            .addField('Roles', roles)
            .addField('Joined At', `${moment.utc(targetMember.joinedAt).format("MMMM, Do YYYY, h:mm:ss a")}`,)
            .addField('Created At', `${moment.utc(targetMember.user.createdAt).format("MMMM, Do YYYY, h:mm:ss a")}`,)
            .setThumbnail(`${targetMember.user.avatarURL}`)
            .setColor(0x33B5FF)
        message.channel.send(embed);  
    } 

});