简体   繁体   English

Discord.JS 用户信息命令

[英]Discord.JS Userinfo command

I'm trying to make this bot able to do this...我正在努力让这个机器人能够做到这一点......

  • Display User Roles显示用户角色
  • You do d!au @User and their user info shows你做 d!au @User 和他们的用户信息显示

The only thing is that I do not know how to do this, I've found some other stack overflow questions but their bot requires "moment" and I have no idea what moment is.唯一的问题是我不知道该怎么做,我发现了其他一些堆栈溢出问题,但他们的机器人需要“时刻”,我不知道是什么时刻。 This is in a command file, not a index.js file FYI.这是在命令文件中,而不是 index.js 文件仅供参考。

    var commando = require('discord.js-commando');
var discord = require('discord.js');

class aboutuser extends commando.Command 
{
    constructor(client) {
        super(client, {
            name: 'aboutuser',
            group: 'help',
            memberName: 'aboutuser',
            description: 'Lists information about a specific user.',
            aliases: ['au', 'aboutu', 'auser', 'user'],
        })
    }
async run(message, args){

    let userinfo = {};
    userinfo.bot = message.client.user.bot;
    userinfo.createdat = message.client.user.createdAt;
    userinfo.discrim = message.client.user.discriminator;
    userinfo.id = message.client.user.id;
    userinfo.mfa = message.client.user.mfaEnabled;
    userinfo.pre = message.client.user.premium;
    userinfo.presen = message.client.user.presence;
    userinfo.tag = message.client.user.tag;
    userinfo.uname = message.client.user.username;
    userinfo.verified = message.client.user.verified;

    userinfo.avatar = message.client.user.avatarURL;

    var myInfo = new discord.RichEmbed()
        .setAuthor(userinfo.uname, userinfo.avatar)
        .addField("Bot?",userinfo.bot, true)
        .addField("Created At",userinfo.createdat, true)
        .addField("Discriminator",userinfo.discrim, true)
        .addField("Client ID",userinfo.id, true)
        .addField("2FA/MFA Enabled?",userinfo.mfa, true)
        .addField("Paid Account?",userinfo.pre, true)
        .addField("Presence",userinfo.presen, true)
        .addField("Client Tag",userinfo.tag, true)
        .addField("Username",userinfo.uname, true)
        .addField("Verified?",userinfo.verified, true)
        .setColor(0xf0e5da)
        .setFooter('s!aboutserver')
        .setTitle("About this user...")
        .setThumbnail(userinfo.avatar)


        message.channel.sendEmbed(myInfo);

}

}
module.exports = aboutuser;

I want to make my bot able to display a user's roles and make it so that you can tag them.我想让我的机器人能够显示用户的角色并使其能够标记它们。

Reality : The command results in a error, but the bot is online.现实:命令导致错误,但机器人在线。 Referance Error, blank is not defined.参考错误,未定义空白。

Expects : A bot that can list the roles of a user, and you can see the information of other users when using the command. Expects : 一个可以列出用户角色的bo​​t,在使用命令的时候可以看到其他用户的信息。

I've only pasted the code that WORKS, not the ones that end up as failiures.我只粘贴了有效的代码,而不是那些以失败告终的代码。

The following code should do all the stuff you want:下面的代码应该做你想做的所有事情:

var commando = require('discord.js-commando');
var discord = require('discord.js');

class aboutuser extends commando.Command 
{
    constructor(client) {
        super(client, {
            name: 'aboutuser',
            group: 'help',
            memberName: 'aboutuser',
            description: 'Lists information about a specific user.',
            aliases: ['au', 'aboutu', 'auser', 'user'],
        })
    }
async run(message, args){
    const userMention = message.mentions.users.first() || msg.author;
    const memberMention = message.mentions.members.first() || msg.member;

    let userinfo = {};
    userinfo.bot = userMention.bot;
    userinfo.createdat = userMention.createdAt;
    userinfo.discrim = userMention.discriminator;
    userinfo.id = userMention.id;
    userinfo.mfa = userMention.mfaEnabled;
    userinfo.pre = userMention.premium;
    userinfo.presen = userMention.presence;
    userinfo.tag = userMention.tag;
    userinfo.uname = userMention.username;
    userinfo.verified = userMention.verified;

    userinfo.avatar = userMention.avatarURL;

    const rolesOfTheMember = memberMention.roles.filter(r => r.name !== '@everyone').map(role => role.name).join(', ')

    var myInfo = new discord.RichEmbed()
        .setAuthor(userinfo.uname, userinfo.avatar)
        .addField("Bot?",userinfo.bot, true)
        .addField("Created At",userinfo.createdat, true)
        .addField("Discriminator",userinfo.discrim, true)
        .addField("Client ID",userinfo.id, true)
        .addField("2FA/MFA Enabled?",userinfo.mfa, true)
        .addField("Paid Account?",userinfo.pre, true)
        .addField("Presence",userinfo.presen, true)
        .addField("Client Tag",userinfo.tag, true)
        .addField("Username",userinfo.uname, true)
        .addField("Verified?",userinfo.verified, true)
        .setColor(0xf0e5da)
        .setFooter('s!aboutserver')
        .setTitle("About this user...")
        .setThumbnail(userinfo.avatar)


        message.channel.sendEmbed(myInfo);

}

}
module.exports = aboutuser;

I added two new variables that check if there was a mention of a guildMember or not.我添加了两个新变量来检查是否提到了 guildMember。 If yes, the command shows the stats of the mentioned member, if not, the bot shows the stats of the message author.如果是,则该命令显示提及成员的统计信息,如果不是,则机器人显示消息作者的统计信息。

Then I added also a new variable "rolesOfTheMember" which is a list of all roles that the member owns.然后我还添加了一个新变量“rolesOfTheMember”,它是成员拥有的所有角色的列表。 You can simply add this variable in your Discord RichEmbed and then list the roles that the guildMember has on the Discord server!您可以简单地在 Discord RichEmbed 中添加此变量,然后列出 guildMember 在 Discord 服务器上的角色!

Have fun!玩得开心!

Moment is a package that allows you (pretty) easy time formating. Moment 是一个包,它允许您(非常)轻松地形成时间。 You install it same as discord.js, so in your command prompt you do npm install moment (if you installed discord.js using some other syntax, use the same one here too, just replace discord.js with moment).你安装它与 discord.js 一样,所以在你的命令提示符下你做npm install moment (如果你使用其他语法安装了 discord.js,在这里也使用相同的语法,只需用 moment 替换 discord.js)。 That will install the package and then you can use it, just don't forget to put const moment = require('moment');这将安装包然后你可以使用它,只是不要忘记把const moment = require('moment'); to the beginning of your code and you should be good.到你的代码的开头,你应该很好。

All of the time formating can be found here: https://momentjs.com/docs/#/parsing/string-format/所有的时间格式都可以在这里找到: https : //momentjs.com/docs/#/parsing/string-format/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM