Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

565
Views
Discord.js El comando de mensaje no funciona

Discord.js Problema Debo señalar que no tengo experiencia en discord.js. Tengo el siguiente código que se supone que cambia la suma o expresión solicitada por los usuarios en la respuesta real y el mensaje de vuelta. Mi otro comando funciona pero no el otro aquí está el código para eso:

 client.once("message", msg => { if(msg.content.includes("!simple")){ math = Number(msg.content.slice(msg.content.search("e"))) msg.reply(guild.username + "The answer is " + math ) } })

Básicamente, elimino la parte del comando a través del método de corte, luego calculo el valor usando la función Número y luego lo devuelvo, pero no obtengo respuesta del bot. Cualquier ayuda apreciada

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Mientras tanto, Discord cambió sus API. client.on("message") está obsoleto ahora. El ejemplo de trabajo en 2021 se vería así:

 const { Client, Intents } = require('discord.js'); const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] }); client.on("messageCreate", (message) => { if (message.author.bot) return false; console.log(`Message from ${message.author.username}: ${message.content}`); }); client.login(process.env.BOT_TOKEN);

El arranque necesita permiso explícito para leer mensajes. Si el bot no tiene ese permiso, el evento on messageCreate no se activará.

over 4 years ago · Santiago Trujillo Report

0

No estoy seguro de lo que quiere decir con "suma o expresión solicitada", ¿algo como esto?

 client.on('message', msg => { if (msg.content.startsWith('!simple')) { var tocalc = msg.content.substr(8); if (!tocalc.match(/^[\d\(\)\+\-\/\*e\.%=!\s]+$/)) { // allowed = ['number', '(', ')', '+', '-', '*', '/', 'e', '.', '%', '=', '!', ' '] msg.reply('no valid expression or calculation'); return; } var result; try { result = Function('return ' + tocalc)(); } catch(e) { console.error(`tried to exploit! ban user ${msg.author.id}!`); return; } if (isNaN(result)) { console.error(`tried to exploit! ban user ${msg.author.id}!`); return; } msg.reply(result); } }); !simple 1 + 8 - (4 * .25) // 8 !simple 1 == 1 // true !simple 9 % 2 != 8 % 2 // true
over 4 years ago · Santiago Trujillo Report

0

así es como lo haría

 client.on("message",message=>{ if(!message.content.startsWith("!simple") return ;//if the message does not start with simple return const args=message.content.slice(6).trim().split(/ +/);//splits the command into an array separated by spaces const command=args.shift().toLowerCase();//removes the command (!simple in this case) //now you can acces your arguments args[0]..args[1]..args[n] });
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!