T
T
Toxic122020-12-06 08:02:05
Node.js
Toxic12, 2020-12-06 08:02:05

Discord bot error?

Gives this error
(node:5416) UnhandledPromiseRejectionWarning: Error: ENOENT: no such file or directory, scandir 'C:\Users\777\Desktop\discordbot\command'
(Use `node --trace-warnings ...` to show where the warning was created)
( node:5416 ) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_reje... ). (rejection id: 1)
(node:5416) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

app.js

const Discord = require(`discord.js`);
const bot = new Discord.Client();
const fs = require(`promise-fs`);

const config = require (`./config.json`);

bot.commands = new Discord.Collection();
loadCommands().then(count => console.log(`Loaded ${count} commands`));

bot.on(`message`, async (message) => {
    let arr = message.content.split(/\s+/g);
    let args = arr.slice(1);
    let name = arr[0].slice(config.prefix.length);

    if(!message.content.startsWith(config.prefix)) return;
    if(!message.guild) return;
    if(message.author.bot) return;
    
    let cmd = bot.commands.get(name);
    if(cmd) cmd.run(bot, message, args);
});

bot.on(`guildMemberAdd`, async member => {
    bot.logChannel.send(`О Боже кто же это! ${member}!`);
});

bot.on(`guildMemberRemove`, async member => {
    bot.logChannel.send(`${member} Покинул нас`);
});

bot.on(`ready`, async () => {
    console.log(`Logged as ${bot.user.tag}`);
    bot.logChannel = bot.channels.cache.get(`783928263289339926`);
});

bot.login(config.token);

async function loadCommands() {
    let dir = await fs.readdir(`command`);
    let filles = dir.filter(name => name.endsWith(`.js`));

    files.forEach(file => {
        let name = file.split(`.`)[0];
        let  cmd = require(`./commands/${name}`);
        bot.commands.set(name, cmd);
    });

    return bot.commands.size;
}

Ping.js
module.exports.run = async(bot, message, args) => {
    message.reply(`Pong!`);
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey, 2020-12-06
@Azperin

Error: ENOENT: no such file or directory, scandir 'C:\Users\777\Desktop\discordbot\command'

let dir = await fs.readdir(`command`);

A
AlmondPark33609, 2021-08-15
@AlmondPark33609

I advise you to change your code, add this code and remove async function loadcommands

fs.readdir('./название папки с командами', (err, files) => {
   if (err) throw err
   let jsfile = files.filter(f => f.split('.').pop() == 'js')
   if (jsfile.length <= 0) return console.log('Команды не найдены!')

   console.log(`Загружено ${jsfile.length} команд`)
  jsfile.forEach((f, i) => {
    let props = require(`./название папки с командами/${f}`)
   client.commands.set(props.help.name, props)
  })
})

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question