簡體   English   中英

如何制作某些命令 arguments?

[英]How do I make certain command arguments?

所以我想做的是制作像“.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);

  }
}

你可以這樣做。 幾乎,我正在創建一個集合並加載命令(位於 /commands/),獲取他們的 module.exports.help 並將其設置為命令處理程序。 在一條消息上,我抓住他們的消息並嘗試運行 module.exports.run(位於 /commands/${command} 中)。 對於幫助命令,我檢查他們是否有 arguments,如果有,我會嘗試從集合中獲取命令並返回有關該命令的信息。

// 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"
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM