Home > Mobile >  Function returning "undefined" value? JS and Discord.js
Function returning "undefined" value? JS and Discord.js

Time:11-05

Please excuse how utterly "noobish" I am, I'm trying to learn as I go along but I'm very new.

I have the below code which I'm trying to use for a Discord bot. For the most part it works, however the @ "ping" simply returns "@undefined" as opposed to the values I've set in the consts.

Would anyone be so kind as to point me in the right direction on this?

   const ping = {
        roleID: function() {
            return this.role;
        }
    }
        const John = {
            role:"abc"
    }
        const Mary = {
            role:"123"
    }

    function pingSubbed() {
         let pingID = message.author.username;
            if (pingID == "John") {
                    ping.roleID.call(John);
                }
             if (pingID == "Mary") {
                    ping.roleID.call(Mary);
                }
    }

    yield hook.send(`**${message.author.username}**\n`   " "   messageContents   " "   "@" pingSubbed());

I'm expecting the function pingSubbed() to determine the username of the person who posts in Discord, for example John, reference the above ping.roleID.call(John) and then take the appropriate role (in this case John = abc) and sending this information as a message itself - @123 - as in the last line "@" pingSubbed()

CodePudding user response:

You might find a look up table simpler to author and maintain:

function pingSubbed() {
  let getId = Function.call.bind(ping.roleID);
  return {
    John: getId(John),
    Mary: getId(Mary),
  }[message.author.username] || ("Unknown User:" message.author.username);
}
   

This puts a lot less boilerplate (even no quotes) in the way of your raw logic.

Even more jedi would be to make an iterable object containing your users instead of free-floating variables (well const(s)). This drastically simplifies the already-simpler look up table method:

   const ping = {
        roleID: function() {
            return this.role;
        }
    }
    const users={  
     John : {
            role:"abc"
     },
     Mary: {
            role:"123"
     }
    }

function pingSubbed() {
  return ping.roleID.call(users[message.author.username]) || 
        ("Unknown User:" message.author.username);
}

that gets it to the point of being almost all data with minimal logic and code to worry about...

CodePudding user response:

The call inside your Object is working well you just forget to return the value that you need from your function

function pingSubbed() {
         let pingID = message.author.username;
            if (pingID == "John") {
                    ping.roleID.call(John);
                }
             if (pingID == "Mary") {
                    ping.roleID.call(Mary);
                }
         return pingID // add this line 
    }

CodePudding user response:

When you use this keyword inside an object, it refers to the object itself.

  • Related