Search code examples
discord.jsargs

Discordjs v12 args


Can anyone help me with args? i have tested .bet (number under 99) but its wont reply me the message and same as .bet (number more than 100)

//My code
if (message.content.startsWith(".bet")) {
let bet = args.join(" ");
 // let bet = args[1]
  if (!args[0]) {
        return message.channel.send(`Woops, looks like you forgot to specify an amount to bet`)
    }

and this is the problems

//the problems

if (args[1] <= 99) {
  return
  message.channel.send("Sorry, You cannot bet under 99$")
  
}
if (args[2] >= 100) {
  return
  await db.add(`user.money_${message.author.id}`, -bet)

  message.channel.send("test")
}  

//
}

Solution

  • The code snippet you added in your post doesn't show the part where you define the args variable, so I created one here.

    const args = message.content.slice(".".length).trim().split(/ +/g).shift(); // split the message content on space and return the arguments
    
    if (message.content.startsWith(".bet")) {
        if (!args[0]) { // if there's no argument
            return message.reply("Woops, looks like you forgot to specify an amount to bet!");
        }
    
        if (args[0] <= 99) { // if bet is equal/less than 99
            return message.reply("Sorry, you cannot bet under $99.");
        }
    
        if (args[0] >= 100) { // if bet is equal/greater than 100
            await db.add(`user.money_${message.author.id}`, -args[0]);
            return message.reply("Successfully added a bet.");
        }
    }