#Object value not recognized as function?

16 messages · Page 1 of 1 (latest)

fallen sparrow

hello i am quite new to javascript (im coming from python and i love d.js already)
i have this class ```javascript
class LocalSlashBuilder {
constructor() {
this.data = [];
this.interactionMap = {};
}

register(name, description, callback) {
    this.data.push({name: name, description: description});
    this.interactionMap[name] = callback;
}

dispatch_for(name, interaction) {
    this.interactionMap[name](interaction);    
}

}
``` the issue is, in dispatch_for the expression doesnt work, it says that: TypeError: this.interactionMap[name] is not a function
though upon googling i dont see anything wrong
this is also how i would write this in python
i have no idea why this happens

dense stormBOT
  • Consider reading #how-to-get-help to improve your question!
  • Explain what exactly your issue is.
  • Post the full error stack trace, not just the top part!
  • Show your code!
  • Issue solved? Press the button!
  • Marked as resolved by OP
fallen sparrow
PS C:\Users\Uzivatel\Desktop\Langs\js\discord.js> node main.cjs
Ready! Logged in as 愛psiblades - bot#3237
C:\Users\Uzivatel\Desktop\Langs\js\discord.js\main.cjs:36
        this.interactionMap[name](interaction);
                                 ^

TypeError: this.interactionMap[name] is not a function
    at LocalSlashBuilder.dispatch_for (C:\Users\Uzivatel\Desktop\Langs\js\discord.js\main.cjs:36:34)
    at Client.<anonymous> (C:\Users\Uzivatel\Desktop\Langs\js\discord.js\main.cjs:64:14)
    at Client.emit (node:events:524:28)
    at InteractionCreateAction.handle (C:\Users\Uzivatel\Desktop\Langs\js\discord.js\node_modules\discord.js\src\client\actions\InteractionCreate.js:97:12)
    at module.exports [as INTERACTION_CREATE] (C:\Users\Uzivatel\Desktop\Langs\js\discord.js\node_modules\discord.js\src\client\websocket\handlers\INTERACTION_CREATE.js:4:36)
    at WebSocketManager.handlePacket (C:\Users\Uzivatel\Desktop\Langs\js\discord.js\node_modules\discord.js\src\client\websocket\WebSocketManager.js:348:31)
    at WebSocketManager.<anonymous> (C:\Users\Uzivatel\Desktop\Langs\js\discord.js\node_modules\discord.js\src\client\websocket\WebSocketManager.js:232:12)
    at WebSocketManager.emit (C:\Users\Uzivatel\Desktop\Langs\js\discord.js\node_modules\@vladfrangu\async_event_emitter\dist\index.cjs:287:31)
    at WebSocketShard.<anonymous> (C:\Users\Uzivatel\Desktop\Langs\js\discord.js\node_modules\@discordjs\ws\dist\index.js:1190:51)
    at WebSocketShard.emit (C:\Users\Uzivatel\Desktop\Langs\js\discord.js\node_modules\@vladfrangu\async_event_emitter\dist\index.cjs:287:31)

Node.js v22.13.
``` stacktrace
patent jackal

are you sure this.interactionMap[name] isn't undefined?

could log the map and name and see if they're what you expect

fallen sparrow

thats what i suspect but how come?

patent jackal

how come what?

it's undefined?

fallen sparrow
dispatch_for(name, interaction) {
    console.log(`Requesting interaction response for ${name}`);
    const callback = this.interactionMap[name];    
    console.log(callback, typeof callback);
    callback(interaction);
}
``` ill test this
fallen sparrow
patent jackal

well discord wouldn't, it'd mean that there's some issue with how you're registering/dispatching that "builder"

fallen sparrow

i wonder if im accessing it incorrectly

patent jackal

log interaction

it's probably not an interaction

fallen sparrow

the attribute is commandName and not name