【问题标题】:How can I split my code into different files?如何将我的代码拆分为不同的文件?
【发布时间】:2021-07-26 05:12:17
【问题描述】:

index.js

const Discord = require('discord.js');
const client = new Discord.Client();

client.login('');

client.on('ready', ready => {
    console.log(`${client.user.tag} is ready!`)
});

另一个.js

client.on('message', m => {
    if (m.content = '123') {
        m.delete()
    }
})

我怎样才能让它工作?我想将某些命令拆分到其他文件中。

【问题讨论】:

标签: javascript node.js discord.js


【解决方案1】:

index.js 导出client 并在another.js 中导入:

// index.js
// the rest of the file, and...

module.exports = { client };
// another.js
const { client } = require('./index'); // or the correct path

// and the rest of the file

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-13
    • 2021-03-25
    • 1970-01-01
    • 2017-11-24
    • 2020-01-30
    • 2020-01-11
    相关资源
    最近更新 更多