0
let args = message.content.slice(config.prefix.length).split(' ');
let command = args.shift().toLowerCase();
let tellJoke = randomNumber(4);

if (debounce == true){
    return
  }
debounce = true

switch (command) {

  case 'gameid':
    console.log(args.length)
    if(args.length == 1){
      let placeId = args[1]
      console.log(placeId)
      api = await fetch("https://api.roblox.com/universes/get-universe-containing-place?placeid=" + placeId)
      let gameId = await api.json()
      try{
        if(api){
          reply = await message.reply('Game Id: ' + gameId.UniverseId)
          debounce = false
          break;
        }
      }catch(error){
        reply = await message.reply('Something went wrong. ERROR: ' + error);
        debounce = false
        break;
      }

      }
    reply = await message.reply('Something went wrong. Did you write the command correctly?')
    debounce = false
    return

It says placeId/arg 1 is undefined when I clearly put the place id there. Does anyone know why this is happening? I've printed/logged the args and length of it and I can say that it should work.

2
  • 1
    If args.length == 1 then args[1] is undefined. args[0] is the first element in an array (and the only element in an array of length 1). Commented Aug 9, 2021 at 14:47
  • 1
    It now works. Thank you! :) Commented Aug 9, 2021 at 15:13

1 Answer 1

1

Array index starts from 0. so if the length of the array is 1 you can access the first element with args[0].

let args = message.content.slice(config.prefix.length).split(' ');
let command = args.shift().toLowerCase();
let tellJoke = randomNumber(4);

if (debounce == true){
    return
  }
debounce = true

switch (command) {

  case 'gameid':
    console.log(args.length)
    if(args.length == 1){
      let placeId = args[0]
      console.log(placeId)
      api = await fetch("https://api.roblox.com/universes/get-universe-containing-place?placeid=" + placeId)
      let gameId = await api.json()
      try{
        if(api){
          reply = await message.reply('Game Id: ' + gameId.UniverseId)
          debounce = false
          break;
        }
      }catch(error){
        reply = await message.reply('Something went wrong. ERROR: ' + error);
        debounce = false
        break;
      }

      }
    reply = await message.reply('Something went wrong. Did you right the command correctly?')
    debounce = false
    return
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! Makes more sense now why it would say undefined.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.