Discord.js - ReferenceError: join is not defined

时间:2021-01-13 15:38:48

标签: javascript node.js discord.js

我试图将我的命令组织到类别中,但在运行我的机器人时出现错误:

const getDirectories = source => fs.readdirSync(source).map(name => join(source, name)).filter(isDirectory);
                                                                    ^
ReferenceError: join is not defined

enter image description here

我将命令存储为如上图所示的类别,主文件的代码是:

const fs = require("fs");
const { token, prefix } = require('./config.json');
const Discord = require('discord.js');
const db = require ('quick.db');

const client = new Discord.Client
client.commands = new Discord.Collection();
// First get the category directories
const isDirectory = source => fs.lstatSync(source).isDirectory();
const getDirectories = source => fs.readdirSync(source).map(name => join(source, name)).filter(isDirectory);

// Then load the commands
getDirectories(__dirname + '/commands').forEach(category => {
  const commandFiles = fs.readdirSync(category).filter(file => file.endsWith('.js'));

  for(const file of commandFiles) {
    const command = require(`./${category}/${file}`);
    client.commands.set(command.name, command);
  }
});

client.on("ready", () => {
    console.log(`ready!.`);
    console.log(token);
    
    // Activities  
    const activities_list = [ 
        `Serving Tacos | .help`,
        `Preparing Orders | .help`
    ];
    
    setInterval(() => {
        const index = Math.floor(Math.random() * (activities_list.length - 1) + 1);
        client.user.setActivity(activities_list[index]);
    }, 10000);
});
// rest of the code

1 个答案:

答案 0 :(得分:2)

您似乎正在尝试使用 path.join。不要忘记导入 path 库:

const path = require('path')

并将其用作 path.join(source, name)。或者通过解构 join 来获取 path 方法:

const {join} = require('path')

并用作 join(source, name)