如何设置某些命令参数?

时间:2020-07-22 01:32:31

标签: javascript node.js discord.js

所以我想做的是使命令如“ .help [command]”。例如,我有一个ping命令,所以我想运行命令“ .help ping”,然后会弹出一个嵌入窗口,显示该命令的嵌入帮助。我当前的命令“ .help”会向您发送一个DM,其中包含我在DM嵌入中输入的命令

help.js代码

var Discord = require('discord.js');

module.exports = {
  run: async(client, message, args) => {

    message.channel.send(":bow_and_arrow:  I've sent you a DM with a list of commands.")
    const embed = new Discord.MessageEmbed()
      .setColor("PURPLE")
      .setTitle("Here is a list of all the bot commands!")
      .setDescription('You can use `.help <command>` for more information')
      .addField("Player Commands", "`.ping` | `.userinfo` | `.botinfo` | `.serverinfo`")
      .addField("Moderation Commands", "`.` | `.` | `.` | `.`")
      .addField("Admin Commands", "`.` | `.` | `.` | `.`")
      .addField("Misc Commands", "`.` | `.` | `.` | `.`")
      .addField("Developer Commands", "`.eval`")
    message.author.send(embed);

  }
}

1 个答案:

答案 0 :(得分:1)

您可以执行以下操作。我几乎创建了一个集合,并加载命令(位于/ commands /中)以获取其module.exports.help并将其设置为命令处理程序。在收到一条消息时,我抓住了他们的消息,然后尝试运行module.exports.run(位于/ commands / $ {command}中)。对于help命令,我检查是否有参数,是否尝试从集合中获取命令并返回有关该命令的信息。

// Command Loader (main file)

var fs = require('fs')

client.commands = new Discord.Collection();

fs.readdir("./commands/", (err, files) => {
    if (err) console.log(err);
    let jsfile = files.filter(f => f.split(".").pop() === "js");
    if (jsfile.length <= 0) {
        console.log("Couldn't find commands.");
        return;
    }

    console.log(`Loading ${jsfile.length} commands!`);

    jsfile.forEach((f, i) => {
        let props = require(`./commands/${f}`);
        console.log(`${i + 1}: ${f} loaded!`);
        client.commands.set(props.help.name, props);
    });
});

// Client.on("message")
let prefix = "."
let [command, ...args] = msg.content.slice(prefix.length).split(/\s+/g)

// Command handler

if (!message.content.startsWith(prefix)) return;
let commandfile = client.commands.get(command);
if (commandfile) commandfile.run(client, message, args);
// ./commands/ping.js
module.exports.run = async function (client, message, args) {
    message.channel.send(`Latency Is: \`\`\`${Math.round(client.ping)}ms\`\`\``)
}

module.exports.help = {
    name: "ping",
    description: "returns latency of bot",
    usage: ".ping",
    aliases: ['.latency', '.ping']
}
./commands/help.js
module.exports.run = async (client, message, args) => {
    if (args[0]) {
        let command = args[0]
        if (client.commands.has(command)) {
            command = client.commands.get(command);
            var shEmbed = new Discord.MessageEmbed()
                .setColor("PURPLE")
                .setAuthor("______ HELP", message.guild.iconURL)
                .setDescription(`The Bot Prefix Is: .\n\n**Command:** ${command.help.name}\n**Description:** ${command.help.description || "No Description"}\n**Usage:** ${command.help.usage || "No Usage"}\n **Aliases** ${command.help.aliases || "No Aliases"}`);
            message.channel.send(shEmbed)
        }

        if (!args[0]) {
            message.delete();
            let embed = new Discord.MessageEmbed()
                .setAuthor("Help Command", message.guild.iconURL)
                .setColor("PURPLE")
                .setDescription(`${message.author.username} I've Sent You A DM! Check It Out`);

            let sembed = new Discord.MessageEmbed()
                .setColor("PURPLE")
                .setAuthor(`_______ HELP`, message.guild.iconURL)
                .setThumbnail(bot.user.displayAvatarURL)
                .setTimestamp()
                .setDescription(`These are the avaliable commands for the _______\n the bot prefix is .`)
                .addField(`commands:`, `YOUR COMMANDS_HERE`)
                .setFooter(`_______ Version: ${version}`, bot.user.displayAvatarURL)
            message.channel.send(embed).then(m => m.delete(10000));
            message.author.send(sembed);
        } 
    }
}

module.exports.help = {
    name: "help"
}