Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

179
Visualizações
JavaS. Cannot read property 'filter' of undefined

I have such a question because I no longer know what to do. I have a BOT on Suggestion to a certain channel. When I run the bot via Visual Studio Code, the bot runs and everything works. But as soon as I load a boot on heroku.com, the program suddenly writes an error:

files = files.filter(f => f.endsWith(".js"))

enter image description here

Someone could help me, I would be very happy. Have a nice day

Index.js is here:

client.once('ready', () => {
    console.log('Tutorial Bot is online!');
});

client.commands = new Discord.Collection()
const fs = require("fs")
fs.readdir("./commands/", (error, files) => {
    files = files.filter(f => f.endsWith(".js"))
    files.forEach(f => {
        const command = require(`./commands/${f}`)
        client.commands.set(command.name, command)
        console.log(`Command ${command.name} was loaded!`)
    });
});

client.on("message", message => {
    if (message.author.bot) return;
    if (message.channel.type === "dm") return;
    if (!message.content.startsWith(config.prefix)) return;
    const args = message.content.slice(config.prefix.length).split(" ")
    const command = args.shift()
    const cmd = client.commands.get(command)
    if (cmd) {
        cmd.run(client, message, args)
    } else return;
});


bot.login(config.token);

Suggest.js is here

const { MessageEmbed } = require("discord.js");

module.exports = {
    name: "suggest",
    aliases: [],
    description: "Make a suggestion and have the community vote",
    category: "utility",
    usage: "suggest <suggestion>",
    run: async (client, message, args) => {
        let suggestion = args.slice(0).join(" ");
        let SuggestionChannel = message.guild.channels.cache.find(channel => channel.name === "suggestions");
        if (!SuggestionChannel) return message.channel.send("Please create a channel named suggestions before using this command!");
        const embed = new MessageEmbed()
            .setTitle("New Suggestion")
            .setDescription(suggestion)
            .setColor("RANDOM")
            .setFooter(`${message.author.tag} | ID: ${message.author.id}`)
            .setTimestamp()
        SuggestionChannel.send(embed).then(msg => {
            msg.react("👍🏼")
            msg.react("👎🏼")
        message.channel.send("Your suggestion has been sent!");
        });
    }
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You forgot to check for errors in your callback. In case of an error files will be undefined, thus resulting in the error you've observed. So you should do something like:

fs.readdir("./commands/", (error, files) => {
    if(error) {
      // handle error          
    } else {
      files = files.filter(f => f.endsWith(".js"))
        files.forEach(f => {
          const command = require(`./commands/${f}`)
          client.commands.set(command.name, command)
          console.log(`Command ${command.name} was loaded!`)
       });
    }
});
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda