首页 > 解决方案 > discord.js - 我需要有关配置文件命令的帮助

问题描述

所以,我为我的机器人创建了一个配置文件命令,但我想添加第二个参数。我想做的命令是 "/profile [username to search]" ,但我现在不知道该怎么做。

如果你能帮助我,我将不胜感激。

这是代码:

if (message.content === '/profile') {
      let botembed = new Discord.RichEmbed()
                .setTitle("**__Exoly User Profile__**")
                .setTimestamp(new Date())
                .setColor("#4286f4")
                .setFooter("Exolia", `${bot.user.avatarURL}`)
                .setThumbnail(`${message.author.avatarURL}`)
                .addField("Username :", `${message.author.username}`, inline = true)
                .addField("Exolytes :", "|---|", inline = true)
                .addField("Played Time :", "|---|", inline = true)
                .addField("Faction :", "Armada", inline = true);
       if (shouldResponseTo(message)) {
           message.delete()
           return message.channel.send(botembed);
       }
    }

标签: javascriptdiscord.js

解决方案


一个快速的好方法来做你想做的事,

//Checks if it starts with (/profile) 
if(message.content.startsWith(`/profile`)){
    //Defines the user that needs to be searched to be either the user pinged OR the user that called the command
    let user = message.mentions.users.first(); || message.author;
    let embed = new Discord.RichEmbed()
    .setTitle(`Profile`);
    .setDescription(`Profile of ${user.username}`);
    //Note: .send(embed) is allowed but not recommended and it's easier and safer todo .send({embed}) or in your case .send({embed:botembed})
    message.channel.send({embed});    
}

您还可以通过以下方式检查之后所说的内容(/profile)

let args = message.content.split(` `);
if(args[0]===`/profile`){
    args.shift();
    //args is now all the arguments that was put in after (/profile)

    //So a message (/profile @user page 3)
    //args would equal ["@user", "page", "3"]

}

推荐阅读