猿问

Discord.js 我可以在另一个文件中执行一个文件吗

我有我的主 Index.js 文件,里面有这段代码(想象输入是!help)


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

const client = new Discord.Client();

const fs = require('fs');

client.commands = new Discord.Collection();

const commandFiles = fs.readdirSync('./commands/').filter(file => file.endsWith('.js'));

for(const file of commandFiles){

    const command = require(`./commands/${file}`);

    client.commands.set(command.name, command);

}

client.on("error", console.error);

client.once('ready', () => {

    console.log('ProBot is online!');

});

client.on('message', message => {

    if (message.author.bot) return;

    if (message.guild === null) return;

    if (message.content.startsWith("!")){

        const prefix = "!";

        const args = message.content.slice(prefix.length).trim().split(/ +/g);

        const command = args.shift().toLowerCase();

        if (command.length === 0) return;

        let cmd = client.commands.get(command);

        if (!cmd) return message.reply(`\`${prefix + command}\` doesn't exist!`);

        cmd.execute(message, args);

    }

};

然后打开文件help.js,示例是


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

module.exports = {

    name: 'help',

    description: "!help Command",

    execute(message, args){

        if(!message.member.hasPermission("MANAGE_GUILD")){  //Regular Output

            message.react('❤️')

            const help2Embed = new Discord.MessageEmbed()

            .setColor('#ffd6d6')

            .setTitle('!Help\n')

            .setDescription('Check Your Private Messages For More Information')

            message.channel.send(help2Embed)

            const h11elpEmbed = new Discord.MessageEmbed()

            .setColor('#ffd6d6')

            .setTitle('!Help\n')

        }

}}

问题是,如果它被更改为 !warn @member [low, med, high] [reason] - 索引能否将其放入 warn.js 文件,然后从那里根据 args[2] 是否低, med,高打开并执行一个新文件?为每个运行不同的代码。[或者如果有我忽略的更简单的方法]


倚天杖
浏览 110回答 1
1回答

慕仙森

只是为了结束问题并得到答案。来自 @worthy Alpaca 的非常有用的评论(全部归功于他们) you can just create a new file, that you can name however you want, according to the schema you already have. Inside that file you can then handle whatever parameters you wish to use with that command
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答