【问题标题】:How to read Class Objects / Functions when passed as a param?作为参数传递时如何读取类对象/函数?
【发布时间】:2022-06-11 05:20:55
【问题描述】:

我目前正在使用 Discord 的网关和套接字来创建类似于 discord.js 的东西,并让新用户更容易使用我正在制作的包我希望他们查看 Message 类对象和作为参数传递时的函数。我在这方面的所有尝试都没有成功。

来自Discord.js的示例:

来自My Package的示例:

我的.on()代码:

/**
 * 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
                    }
            }
        }
        
        })

}

我的Message.init()代码:

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

}

我查看了类似的内容,但找不到任何特定于我要查找的内容的内容。

【问题讨论】:

  • Discord 可能是用 Typescript 编写的。如果你想要这样的 Intellisense 支持,你应该用 typescript 编写
  • 所以您问的是如何在您在对象后键入点时弹出的窗口中首先显示有用的内容? (此评论应该说明如何在不知道“智能感知”一词的情况下提出这个问题)
  • @ChrisG message 属性是 message: any 类型,我希望它是 message: Message 类型
  • 是的,很明显。你知道怎么写 Typescript 吗?
  • 没有尝试太多,似乎 discord.js 确实使用 typescript 但不用于他们的客户端类。安装后,打字稿是否仍会显示 vanilla js 项目中的参数类型?

标签: javascript node.js discord discord.js


【解决方案1】:

你需要告诉 jsdoc 你的函数接受什么参数。虽然FUNCTION 可以通用,但您需要更具体:

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

这表示第二个参数是一个不返回任何内容的函数 (void),并有一个 Message 对象作为其参数。大概然后你会在你的项目中的某个地方有一个Message 类/构造函数。

【讨论】:

  • 注意:此示例仅适用于 message 事件。如果 OP 需要使用不同的回调参数来实现不同的事件,那么他将无法使用 JSDoc 来实现。
  • @koloml Typescript 类型(jsdoc 支持的)支持联合类型。您可以简单地指定(Message|Other|AnotherOne) => void
  • 联合类型工作正常,是的。但不是像keyof 和泛型这样的复杂功能。在 DiscordJS 中有一个 type ,其中包含所有可用的事件名称(键)和发送到回调的参数列表(值)。因此,当您输入特定的事件名称时,IDE 将获取发送回的内容。
  • 如果没有thisthis OP 将无法为不同的事件进行这项工作。
  • 这在将其更改为 @param {(message:Message) => void} func 时有效,谢谢@slebetman
猜你喜欢
  • 2019-07-23
  • 1970-01-01
  • 1970-01-01
  • 2012-03-30
  • 2022-07-10
  • 2015-11-22
  • 1970-01-01
  • 2014-04-30
  • 1970-01-01
相关资源
最近更新 更多