【问题标题】:How can I make this work - client already declared [closed]我怎样才能使这项工作 - 客户已经宣布[关闭]
【发布时间】:2023-01-02 20:16:57
【问题描述】:

代码:

require("dotenv").config();
const { token } = process.env;
const {client, collection, GatewayIntentBits } = require("discord.js");
const fs = require("fs");

const client = new Client({ intents: GatewayIntentBits.Guilds });
client.commands = new collection();
client.commandArray = [];

const functionFolders = fs.readdirSync(`./src/functions`);
for (const folder of functionFolders) {
  const functionFiles = fs
    .readdirSync('./src/functions/${folder}')
    .filter((file) => file.endsWith(".js"));
  for (const file of functionFiles)
    require(`./functions/${folder}/${file}`)(client);
}

client.handleEvents();
client.handleCommands();
client.login(token);

运行 npm run test 后出错:

**const client = new Client({ intents: GatewayIntentBits.Guilds });
      ^

SyntaxError: Identifier 'client' has already been declared**
    at Object.compileFunction (node:vm:360:18)
    at Module._compile (node:internal/modules/cjs/loader:1123:27)
    at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)
    at Module.load (node:internal/modules/cjs/loader:1037:32)
    at Module._load (node:internal/modules/cjs/loader:878:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:23:47

我尝试删除第 3 行,因为客户端已在第 6 行处理,但这也会引发不同的错误。

有人可以建议吗?

我尝试删除第 3 行,因为客户端已在第 6 行处理,但这也会引发不同的错误。

尝试从第 3 行删除“客户端”

【问题讨论】:

  • 当您从第 3 行仅删除 client 时会出现什么错误?
  • const client = new Client({ intents: GatewayIntentBits.Guilds }); ^ ReferenceError:客户端未定义
  • 它可能应该是Client而不是client在您的导入/要求中
  • 同样适用于Collection

标签: javascript


【解决方案1】:

您需要导入 Client 而不是 client

像这样尝试

require("dotenv").config();
const { token } = process.env;
const {Client, collection, GatewayIntentBits } = require("discord.js");
const fs = require("fs");

const client = new Client({ intents: GatewayIntentBits.Guilds });
client.commands = new collection();
client.commandArray = [];

const functionFolders = fs.readdirSync(`./src/functions`);
for (const folder of functionFolders) {
  const functionFiles = fs
    .readdirSync('./src/functions/${folder}')
    .filter((file) => file.endsWith(".js"));
  for (const file of functionFiles)
    require(`./functions/${folder}/${file}`)(client);
}

client.handleEvents();
client.handleCommands();
client.login(token);


【讨论】:

  • 现在在下面尝试了这个错误。 client.commands = new Collection(); ^ ReferenceError:未定义集合
  • @HyperXSpartan 在第 3 行尝试使用 Collection 而不是 collection
【解决方案2】:

您不能重新声明用 const 声明的变量。 使用不同的名称

const discordClient = new Client({ intents: GatewayIntentBits.Guilds });
discordClient.foo = bar;

【讨论】:

    【解决方案3】:

    您已两次初始化 client 变量。

    const {client, collection, GatewayIntentBits } = require("discord.js"); // declared client here
    ...
    ...
    const client = new Client({ intents: GatewayIntentBits.Guilds }); // also here
    // const _client = new Client({ intents: GatewayIntentBits.Guilds }) # this will fix with updated references of this variable
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-09-06
      • 1970-01-01
      • 2012-08-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多