Acabo de intentar crear un controlador de comandos en discord.js y cada vez que ejecuto el bot, arroja
TypeError: bot.commands.get(...).run no es una función
Aquí está la parte inferior del código sobre dónde está ocurriendo el error.
bot.on("message", async message => { if(message.author.bot) return; if(message.channel.type === 'dm') return; if(message.content.startsWith(prefix)) { const args = message.content.slice(prefix.length).trim().split(/ +/); const command = args.shift().toLowerCase(); if(!bot.commands.has(command)) return; try { bot.commands.get(command).run(bot, message, args); } catch (error){ console.error(error); } } }) bot.login(token);Parece que su controlador de comandos está un poco incompleto. Así es como tengo la configuración mía, puedes probarlo y decirme si funciona (lo explico todo con los comentarios:
// your bot.js or main.js file const bot = new Discord.Client(); // I setup the bot client bot.commands = new Discord.Collection(); // I create a collection with all the commands const commandsFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js')); // In my project tree, I define all the files with the .js extension in this folder as command files. I will add a file (.js) to this folder for each command I want to create. For example, there will be './commands/ping.js' inside. for(const file of commandsFiles){ const command = require(`./commands/${file}`); client.commands.set(command.name, command); } // For each command file detected, I add it's name and it's path to my client.commands collection client.on('message', message => { if(message.author.bot) return; if(message.channel.type === 'dm') return; if(message.content.startsWith(prefix)) { const args = message.content.slice(prefix.length).trim().split(/ +/); const command = args.shift().toLowerCase(); if(!bot.commands.has(command)) return; // if the collection doesn't have a command with this name, return try { bot.commands.get(command).run(bot, message, args); // I get the command object defined previously in the 'for' loop, and I execute the run function with bot, message and args as arguments } catch (error){ console.error(error); } } } Ahora que ha configurado su controlador de comandos, para cada comando que desee crear, agregue un archivo dentro de la carpeta ./commands/ (por ejemplo, ./commands/ping.js , no olvide la parte .js) . En este archivo recién creado, escriba:
module.exports = { name : 'ping', description : 'Ping command', run(bot, message, args) { // the code to be executed }Aquí puede ver que definimos un objeto, que será utilizado por el controlador de comandos.
Cuando hacemos client.commands.set(command.name, command) (en bot.js o main.js), usamos el nombre definido aquí.
Cuando hacemos client.commands.get(command).run(bot, message, args) , ejecutamos la función de run definida dentro del objeto module.export del archivo de comando de destino.
No dudes en decirme si hay algo que no entiendas.