将语句分解为多个模块

时间:2018-03-16 17:10:09

标签: node.js discord.js

我想将以下代码分解为多个文件进行管理,而不是让所有内容都在主文件上运行。整个代码定位为一个Node.js模式,称为index.js我想将代码分解为多个文件management.js和interantion.js,它们与node.js进行通信

    const Discord = require("discord.js");
const client = new Discord.Client();
const config = require("./config.json");
const ms = require("ms");

client.on("ready", () => {
    console.log(`Bot has started, with ${client.users.size} users, in ${client.channels.size} channels of ${client.guilds.size} guilds.`);
    client.user.setGame(config.game);
});
client.on("guildCreate", guild => {
    console.log(`New guild joined: ${guild.name} (id: ${guild.id}). This guild has ${guild.memberCount} members!`);
    client.user.setGame(`on ${client.guilds.size} servers`);
});
client.on("guildDelete", guild => {
    console.log(`I have been removed from: ${guild.name} (id: ${guild.id})`);
});

client.on("guildMemberAdd", function(member) {
    //Welcomes a user
    member.guild.channels.find("name", "general").send("Please welcome " + member.toString() + " to the server! " + member.toString() + " Make sure to read the rules and enjoy your stay :heart:." + "   https://imgur.com/a/pCcSm");
});
//managment
client.on("message", async message => {
    if (message.author.bot) return;
    if (message.content.indexOf(config.prefix) !== 0) return;
    const args = message.content.slice(config.prefix.length).trim().split(/ +/g);
    const command = args.shift().toLowerCase();
    var mutedrole = message.guild.roles.find("name", "muted");
    //Managment
    if (command === "say") {
        if (!message.member.roles.some(r => ["Management"].includes(r.name))) return message.reply("Sorry, you don't have permissions to use this!");
        const sayMessage = args.join(" ");
        message.delete().catch(O_o => {});
        message.channel.send(sayMessage);
    }
    if (command === "kick") {
        if (!message.member.roles.some(r => ["Management"].includes(r.name))) return message.reply("Sorry, you don't have permissions to use this!");
        let member = message.mentions.members.first();
        if (!member) return message.reply("Please mention a valid member of this server");
        if (!member.kickable) return message.reply("I cannot kick this user! Do they have a higher role? Do I have kick permissions?");
        let reason = args.slice(1).join(' ');
        if (!reason) return message.reply("Please indicate a reason for the kick!");
        await member.kick(reason).catch(error => message.reply(`Sorry ${message.author} I couldn't kick because of : ${error}`));
        message.reply(`${member.user.tag} has been kicked by ${message.author.tag} because: ${reason}`);
    }
    if (command === "ban") {
        if (!message.member.roles.some(r => ["Management"].includes(r.name))) return message.reply("Sorry, you don't have permissions to use this!");
        let member = message.mentions.members.first();
        if (!member) return message.reply("Please mention a valid member of this server");
        if (!member.bannable) return message.reply("I cannot ban this user! Do they have a higher role? Do I have ban permissions?");
        let reason = args.slice(1).join(' ');
        if (!reason) return message.reply("Please indicate a reason for the ban!");
        await member.ban(reason).catch(error => message.reply(`Sorry ${message.author} I couldn't ban because of : ${error}`));
        message.reply(`${member.user.tag} has been banned by ${message.author.tag} because: ${reason}`);
    }
    if (command === "purge") {
        if (!message.member.roles.some(r => ["Management"].includes(r.name))) return message.reply("Sorry, you don't have permissions to use this!");
        const deleteCount = parseInt(args[0], 10);
        if (!deleteCount || deleteCount < 2 || deleteCount > 100) return message.reply("Please provide a number between 2 and 100 for the number of messages to delete");
        const fetched = await message.channel.fetchMessages({
            count: deleteCount
        });
        message.channel.bulkDelete(fetched).catch(error => message.reply(`Couldn't delete messages because of: ${error}`));
    }

    if (command == "mute") {
        let member = message.mentions.members.first();
        if (!member) return message.reply("Please mention a member");
        let muteRole = message.guild.roles.find("name", "Muted");
        if (!muteRole) return message.reply("Role: Muted not found.");
        let params = message.content.split(" ").slice(1);
        let time = args[1];
        if (!time) return message.reply("Please specify how long the user should be muted.");

        member.addRole(muteRole.id);
        message.channel.send(`You've been muted for ${ms(ms(time), {long: true})}, ${member.user.tag}`);

        setTimeout(function() {
            member.removeRole(muteRole.id);
            message.channel.send(`${member.user.tag} You were unmuted! The mute lasted: ${ms(ms(time), {long: true})}`)
        }, ms(time));
    }
    //interaction

    if (command === "ping") {
        const m = await message.channel.send("Pong");
        m.edit(`Fine here you go >.<! Latency is ${m.createdTimestamp - message.createdTimestamp}ms. API Latency is ${Math.round(client.ping)}ms`);
    }


    if (command == "drops") {
        message.channel.send("https://docs.google.com/spreadsheets/d/1_SlTjrVRTgHgfS7sRqx4CeJMqlz687HdSlYqiW-JvQA/htmlview?sle=true#gid=0");
    }
});

client.login(config.token);

结果看起来像这样:

index.js

    const Discord = require("discord.js");
const client = new Discord.Client();
const config = require("./config.json");
const ms = require("ms");
client.on("ready", () => {
    console.log(`Bot has started, with ${client.users.size} users, in ${client.channels.size} channels of ${client.guilds.size} guilds.`);
    client.user.setGame(config.game);
});
client.on("guildCreate", guild => {
    console.log(`New guild joined: ${guild.name} (id: ${guild.id}). This guild has ${guild.memberCount} members!`);
    client.user.setGame(`on ${client.guilds.size} servers`);
});
client.on("guildDelete", guild => {
    console.log(`I have been removed from: ${guild.name} (id: ${guild.id})`);
});
client.on("guildMemberAdd", function(member) {
    //Welcomes a user
    member.guild.channels.find("name", "general").send("Please welcome " + member.toString() + " to the server! " + member.toString() + " Make sure to read the rules and enjoy your stay :heart:." + "   https://imgur.com/a/pCcSm");
});
//managment
client.on("message", async message => {
    if (message.author.bot) return;
    if (message.content.indexOf(config.prefix) !== 0) return;
    const args = message.content.slice(config.prefix.length).trim().split(/ +/g);
    const command = args.shift().toLowerCase();

});
client.login(config.token); 

managment.js

     //Managment
    if (command === "say") {
        if (!message.member.roles.some(r => ["Management"].includes(r.name))) return message.reply("Sorry, you don't have permissions to use this!");
        const sayMessage = args.join(" ");
        message.delete().catch(O_o => {});
        message.channel.send(sayMessage);
    }
    if (command === "kick") {
        if (!message.member.roles.some(r => ["Management"].includes(r.name))) return message.reply("Sorry, you don't have permissions to use this!");
        let member = message.mentions.members.first();
        if (!member) return message.reply("Please mention a valid member of this server");
        if (!member.kickable) return message.reply("I cannot kick this user! Do they have a higher role? Do I have kick permissions?");
        let reason = args.slice(1).join(' ');
        if (!reason) return message.reply("Please indicate a reason for the kick!");
        await member.kick(reason).catch(error => message.reply(`Sorry ${message.author} I couldn't kick because of : ${error}`));
        message.reply(`${member.user.tag} has been kicked by ${message.author.tag} because: ${reason}`);
    }
    if (command === "ban") {
        if (!message.member.roles.some(r => ["Management"].includes(r.name))) return message.reply("Sorry, you don't have permissions to use this!");
        let member = message.mentions.members.first();
        if (!member) return message.reply("Please mention a valid member of this server");
        if (!member.bannable) return message.reply("I cannot ban this user! Do they have a higher role? Do I have ban permissions?");
        let reason = args.slice(1).join(' ');
        if (!reason) return message.reply("Please indicate a reason for the ban!");
        await member.ban(reason).catch(error => message.reply(`Sorry ${message.author} I couldn't ban because of : ${error}`));
        message.reply(`${member.user.tag} has been banned by ${message.author.tag} because: ${reason}`);
    }
    if (command === "purge") {
        if (!message.member.roles.some(r => ["Management"].includes(r.name))) return message.reply("Sorry, you don't have permissions to use this!");
        const deleteCount = parseInt(args[0], 10);
        if (!deleteCount || deleteCount < 2 || deleteCount > 100) return message.reply("Please provide a number between 2 and 100 for the number of messages to delete");
        const fetched = await message.channel.fetchMessages({
            count: deleteCount
        });
        message.channel.bulkDelete(fetched).catch(error => message.reply(`Couldn't delete messages because of: ${error}`));
    }
    if (command == "mute") {
        let member = message.mentions.members.first();
        if (!member) return message.reply("Please mention a member");
        let muteRole = message.guild.roles.find("name", "Muted");
        if (!muteRole) return message.reply("Role: Muted not found.");
        let params = message.content.split(" ").slice(1);
        let time = args[1];
        if (!time) return message.reply("Please specify how long the user should be muted.");
        member.addRole(muteRole.id);
        message.channel.send(`You've been muted for ${ms(ms(time), {long: true})}, ${member.user.tag}`);
        setTimeout(function() {
            member.removeRole(muteRole.id);
            message.channel.send(`${member.user.tag} You were unmuted! The mute lasted: ${ms(ms(time), {long: true})}`)
        }, ms(time));
    } 

interaction.js

  //interaction
if (command === "ping") {
    const m = await message.channel.send("Pong");
    m.edit(`Fine here you go >.<! Latency is ${m.createdTimestamp - message.createdTimestamp}ms. API Latency is ${Math.round(client.ping)}ms`);
}
if (command == "drops") {
    message.channel.send("https://docs.google.com/spreadsheets/d/1_SlTjrVRTgHgfS7sRqx4CeJMqlz687HdSlYqiW-JvQA/htmlview?sle=true#gid=0");
}

我尝试过使用必需的方法,但它找不到文件错误。

1 个答案:

答案 0 :(得分:0)

我不完全确定你怎么能这样做,可能是通过使用fs或类似的东西。但是,您可以使用commando.js而不是discord.js。

Commando.js有一个用于登录的索引文件,依此类推,然后每个命令在文件夹下设置为不同的类别,每个命令都有自己的文件。这当然值得研究。