Company logo
  • Jobs
  • Bootcamp
  • About Us
  • For professionals
    • Home
    • Jobs
    • Courses
    • Questions
    • Teachers
    • Bootcamp
  • For business
    • Home
    • Our process
    • Plans
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Calculator

0

51
Views
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.

7 months ago · Juan Pablo Isaza
2 answers
Answer question

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.

7 months ago · Juan Pablo Isaza Report

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"

7 months ago · Juan Pablo Isaza Report
Answer question
Find remote jobs