7

Discord.js Problem I should point out I am not experienced at discord.js. I have the following code which is supposed to change the users requested sum or expression into the actual answer and message back. My other command is working but not the other here is the code for that:

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 )


    }
})

I basically remove the command part via slice method then compute the value of it using the Number function and subsequently return it but I get no response from the bot. Any help appreciated

7
  • 1
    once will only listen for the first event, maybe use on instead
    – Wendelin
    Feb 19, 2021 at 11:25
  • 1
    And I don't think msg.content is an array so msg.content.splice will throw an error. Feb 19, 2021 at 11:53
  • there isn't a splice method? I used slice?
    – user14335301
    Feb 19, 2021 at 12:08
  • i think the best way to use message.content is if (message.content == "!simple") { Feb 19, 2021 at 12:57
  • normally I would agree but in this case i need the user to include the input after the !simple command
    – user14335301
    Feb 19, 2021 at 13:04

4 Answers 4

21

In the mean while Discord changed their APIs. client.on("message") is deprecated now. The working example at 2021 would look like this:

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);

The boot needs explicit permission to read messages. If the bot doesn't have that permission then the on messageCreate event will not be fired.

1
  • 1
    You saved me! This is not even documented anywhere. Internet is full of deprecated approach, sadly. Thanks a lot!
    – LubWn
    Oct 30, 2021 at 18:22
1

i'm not to sure about what you mean with "requested sum or expression", something like this?

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
0

this is how i would go about it

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]
  });
0

if (message.author.bot) return false;

1
  • 2
    As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Mar 30 at 13:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.