【发布时间】: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