Estoy tratando de hacer que mi bot diga un mensaje simple cada vez que alguien le diga "hola". Actualmente, el código del comando en sí se ve así:
const { SlashCommandBuilder } = require('@discordjs/builders'); const greeter = "default"; const greetOptions = [ `Hello, ${greeter}. It's very nice to hear from you today.` ] module.exports = { data: new SlashCommandBuilder() .setName('hello') .setDescription('say hi to Hal!'), execute(message, args) { let greeter = message.user.username; msg.channel.send(greetOptions[Math.floor(Math.random() * greetOptions.length)]); }, };El código que estoy usando para administrar cuando se escriben los comandos tiene el siguiente aspecto:
let prefix = "hal, "; client.on('messageCreate', message => { if (!message.content.startsWith(prefix)) return; const args = message.content.slice(prefix.length); const command = args.toLowerCase(); console.log(`${message.author.tag} called ${command}`); if (!client.commands.has(command)) return; try { client.commands.get(command).execute(message, args); } catch (error) { console.error(error); message.channel.send({ content: 'There was an error while executing this command!', ephemeral: true }); } });Cuando lo ejecuto, arroja el error "No se pueden leer las propiedades de undefined (leyendo 'nombre de usuario').
Si desea mencionar a un usuario, puede hacerlo ${message.author} . Pero si quieres decir por ej. Hello, Brandon entonces debes hacer ${message.author.username} . El mensaje ${message.author.tag} no siempre funciona y también te recomiendo const user = message.mentions.users.first() || message.author o simplemente const user = message.author para abreviar, entonces puedes hacer ${user.username} . Tal vez esto podría arreglar que el bot no responda de otra manera si no me lo dice. Corrija el primer código y cambie msg.channel.send a message.channel.send .