Soy nuevo en escribir discord.js y primero no sé si esta es una pregunta estúpida, pero la pregunta es que quiero hacer un bot para que la primera sección sea un saludo:
//greetings const random_greeting = () =>{ return Math.floor(Math.random() * 5); } client.on('messageCreate', msg=>{ if(msg.author == client.user) return; let greeting = ['Hi', 'Yo', 'Hello', '👋👋', 'Ok...',] if (msg.content === 'hi'){ msg.reply(greeting[random_greeting()])} if (msg.content === 'hello'){ msg.reply(greeting[random_greeting()])} if (msg.content === 'yo'){ msg.reply(greeting[random_greeting()])} if (msg.content === 'sup'){ msg.reply(greeting[random_greeting()])} if (msg.content === 'wassup'){ msg.reply(greeting[random_greeting()])} if (msg.content === 'yo'){ msg.reply(greeting[random_greeting()])} })Como puedes ver, necesito escribir:
if (msg.content === 'hello'){ msg.reply(greeting[random_greeting()])}y sigo enviándolo como 6 veces, así que mi pregunta, ¿hay alguna forma de hacerlo más corto o más rápido?
He intentado algo como agregar la variable saludorespuesta:
let greetingAns = ['hi', 'hello']pero cuando usé
const random_greeting = () =>{ return Math.floor(Math.random() * 5); } client.on('messageCreate', msg=>{ if(msg.author == client.user) return; let greeting = ['Hi', 'Yo', 'Hello', '👋👋', 'Ok...',] let greetingAns = ['hi', 'hello'] if (msg.content === greetingAns()){ msg.reply(greeting[random_greeting()])} Creo que debería funcionar pero no es así. Entonces, ¿ msg.content === es una variable?
Sí, puede hacer eso y necesitará usar una matriz de cadenas como ya lo hizo. Entonces, para verificar si el contenido del mensaje se encuentra en la matriz, se necesitará el método javascript include () .
Como esto:
if (greetingAns.includes(msg.content)) { msg.reply(greeting[random_greeting()]); }