如何从index.js导出客户端

问题描述 投票:0回答:1

我正在制作一个“utils.js”文件,它有一些有趣的功能,我想知道是否有办法将客户端从我的index.js拉到这个文件以及如何做到这一点。我想这样做,以便在使用“嵌入”功能时我不需要将客户端作为参数传递,并且我总是必须通知客户端。

javascript discord discord.js client
1个回答
0
投票

我不确定你想要实现什么,但我想你可以从index.js文件中导出客户端,然后在utils.js中定义它,然后导出嵌入函数,然后在index.js中再次调用它像这样的文件:

index.js

import { Client } from "discord.js";

const client = new Client({ intents: }) // Add your intents here

export default client;

utils.js

import client from "./index.js";

function embed(text) {
    // Add your logic here
}

现在我不知道上面的代码有多大用处,因为要让您的嵌入函数执行任何操作,您必须将其调用到您的主文件(即您的 index.js 文件),因为这是您验证机器人的地方。

现在拥有多个文件的最佳方法是让主文件 (index.js) 定义客户端,执行基本的必要操作,例如验证机器人、定义事件处理程序,然后从其他文件导出函数/逻辑,然后在你的index.js文件中调用它,如下所示:

utils.js

export function embed(client, test) {
    // Add your logic here
}

index.js

import { Client } from "discord.js";
import { embed } from "./utils.js"; // Import your embed function

const client = new Client({ intents:  }) // Add your intents here

embed(client);
© www.soinside.com 2019 - 2024. All rights reserved.