• Empleos
  • Sobre nosotros
  • profesionales
    • Inicio
    • Empleos
    • Cursos y retos
  • empresas
    • Inicio
    • Publicar vacante
    • Nuestro proceso
    • Precios
    • Evaluaciones
    • Nómina
    • Blog
    • Comercial
    • Calculadora de salario

0

205
Vistas
How to read Class Objects / Functions when passed as a param?

I'm current working with Discord's Gateway and sockets to create something similar to discord.js and to make it easier for a new user to use the package i'm making i would like them to view the Message class objects and functions when passed as a param. I have been unsuccessful in all of my attempts at this.

Example from Discord.js:

Discord.js's Example

Example from My Package:

My Example

My .on() Code:

/**
 * client.on() [ when received certain operation from discord's gateway ]
 * @param {OPERATION} operation 
 * @param {FUNCTION} func 
 */
on(operation, func) {

    ws.on('message', (data) => {
        let payload = JSON.parse(data)
        const {t, event, op, d} = payload

        // OPERATION => operation
        operation = operation.toLowerCase();

        // if op => message
        switch(operation) {
            case "message": {
                switch(t) {
                    case "MESSAGE_CREATE":
                        // message build
                        let message = new Message().init(d, this.token)
                        return func(message)
                        //          ^^^^^^^ this is where message is passed
                    }
            }
        }
        
        })

}

My Message.init() Code:

init(d, token) {

    this.content = d.content
    this.channel = {
        id: d.channel_id,
        send: (content) => {
            sendMessage(d.channel_id, token, content)
        }
    }
    this.author = {
        bot: d.author.bot ? d.author.bot : false,
        username: d.author.username,
        tag: `${d.author.username}#${d.author.discriminator}`,
        identifier: d.author.discriminator,
        id: d.author.id,
        avatar: `https://cdn.discordapp.com/avatars/${d.author.id}/${d.author.avatar}.gif` // avatar image
    }
    this.guild = {
        id: d.guild_id
    }
    this.timestamp = d.timestamp

    return this

}

I've looked something like this up and couldn't find anything specific to what I'm looking for.

about 3 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You need to tell jsdoc what arguments your function accept. While FUNCTION works generically you need to be more specific:

/**
 * client.on() [ when received certain operation from discord's gateway ]
 * @param {OPERATION} operation 
 * @param {(message:Message) => void} func 
 */

This says that the second argument is a function that returns nothing (void) and has a Message object as its argument. Presumably then you will have a Message class/constructor somewhere in your project.

about 3 years ago · Juan Pablo Isaza Denunciar

0

Document your code using jsdoc. you'll probably want to read more about it at https://jsdoc.app/

Once you'll have that vscode (and other supported IDEs) will do the rest of the "magic"

about 3 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recomiéndame algunas ofertas
Necesito ayuda