0

error is in title, my code is

if (args[1].startsWith('0')) return;

i'm trying not to allow people to start the first argument with zero. i couldn't find anything on argument startwiths so i'm posting it.

filistata
  • 9
  • 2
  • Can you please post more of your code to give more context? – Joseph Varilla May 26 '20 at 20:17
  • Does this answer your question? [Test if something is not undefined in JavaScript](https://stackoverflow.com/questions/7041123/test-if-something-is-not-undefined-in-javascript) – mustaccio May 26 '20 at 20:27

2 Answers2

0

First argument would be accessed as args[0]

If I'm reading your question correctly then this may be the cause of the problem. If there are less than 2 values in args then args[1] will be undefined.

Chris
  • 1,644
  • 1
  • 11
  • 15
  • the command is !rtd [number] so i'm pretty sure args 1 is right i just don't want people to be able to type !rtd 00010 or something like that – filistata May 26 '20 at 21:12
  • Have you tried args[0] - if args is passed to your code as an array then the first item will have index 0 (https://www.w3schools.com/js/js_arrays.asp) – Chris May 27 '20 at 05:28
  • Otherwise you need to give us more context in your question – Chris May 27 '20 at 05:30
  • Discord docs also suggest index 0 (https://discordjs.guide/creating-your-bot/commands-with-user-input.html#basic-arguments) – Chris May 27 '20 at 05:32
  • https://pastebin.com/aLfB2Bxz is the full command code. i just don't want a user to be able to start the number with a zero sorry i've tried everything i know how to do – filistata May 27 '20 at 06:02
  • But have you tried replacing args[1] with args[0] everywhere? Or is it the case that args[0] contains the value rtd? – Chris May 27 '20 at 06:28
  • The discord documentation suggests shifting the command off the array so that you are left with an array with just the arguments, is that what you are doing - that part isn't in your pasted code. – Chris May 27 '20 at 06:33
0

You can target the first argument by the index 0 as JavaScript does not go from 1 to 10 but from 0 to 9 for example.

Puk
  • 319
  • 1
  • 11