[英]|Discord Bot Error: Cannot find module './commands/ban.js'
我的代码
require('dotenv').config();
const fs = require('fs');
const Discord = require('discord.js')
const { error } = require('console');
const client = new Discord.Client();
client.commands = new Discord.Collection();
const PREFIX = "zm.";
const commandFiles = fs.readdirSync(`./commands/`).filter(file => file.endsWith('.js'));
for(const file of commandFiles){
const command = require(`./commands/${file}`);
client.commands.set(command.name, command);
}
client.login(process.env.BOT_TOKEN);
client.on('ready', () =>{
console.log('ZeKuS Modification bot is now online!');
client.user.setActivity('ZeKuS Modification | ZM', {type: 'PLAYING'}).catch(console.error)});
client.on('message', message =>{
if (message.author.bot) return;
if (!message.content.startsWith(PREFIX)) return;
const args = message.content.slice(PREFIX.length).trim().split(/\s+/);
const cmd = args.shift();
const commandFiles = fs.readdirSync(`./commands/`).filter(file => file.endsWith('.js'));
for(const file of commandFiles){
const command = require(`./commands/${file}`);
client.commands.set(command.name, command);
}
if (!client.commands.has(command)) return;
try{
client.commands.get(cmd).execute(message, args);
} catch (error){
console.error(error);
message.reply('there was an error trying to execute that command!');
}
})
//verify
//ban
client.on('guildMemberAdd', member => {
console.log(member.user.tag);
});
错误:
Error: Cannot find module './commands/ban.js'
Require stack:
- c:\Users\uporabnik\Documents\GitHub\zm-bot\src\bot.js
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:980:15)
at Function.Module._load (internal/modules/cjs/loader.js:862:27)
at Module.require (internal/modules/cjs/loader.js:1042:19)
at require (internal/modules/cjs/helpers.js:77:18)
at Object.<anonymous> (c:\Users\uporabnik\Documents\GitHub\zm-bot\src\bot.js:13:21)
at Module._compile (internal/modules/cjs/loader.js:1156:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1176:10)
at Module.load (internal/modules/cjs/loader.js:1000:32)
at Function.Module._load (internal/modules/cjs/loader.js:899:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12) {
code: 'MODULE_NOT_FOUND',
requireStack: [ 'c:\\Users\\uporabnik\\Documents\\GitHub\\zm-bot\\src\\bot.js' ]
}
我的文件夹:在此处输入图像描述
错误:找不到模块 './commands/ban.js' 需要堆栈:
基本上它会引发错误,我不知道如何解决它,请帮忙,因为我只是停留在当下
您的文件夹结构如下所示:
ZM-Bot/
|
Commands/
| |
| ban.js
|
src/
|
app.js
如果你想从app.js
中要求ban.js
,你需要先从src/
中导航出来。
所以路径是../commands/ban.js
。
const commandFiles = fs.readdirSync(`../commands/`).filter(file => file.endsWith('.js'));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.