Discord.js是否可以在另一个文件中执行一个文件

时间:2020-11-07 22:21:33

标签: javascript discord 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')
            .setDescription('This Bot Has Lots Of Special Features\n \n \nGeneral Commands Are;\n \n!blacklist   = Shows The BlackListed Words\n!safe          = Disables The Filter For That Message\n!server      = Displays Server Name, Total Members And Amount On/Offline')
            message.author.send(h11elpEmbed)
            return;
        }
}}

问题是,如果将其更改为!warn @member [低,中,高] [原因]-能否将索引带入warn.js文件,然后从那里开始,具体取决于args [2]低,中,高打开并执行新文件?为每个代码运行不同的代码。[或者如果有一种更简单的方法我忽略了]

1 个答案:

答案 0 :(得分:0)

只需结束问题并获得答案即可。

@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

相关问题