From c27bb27ec386ffb4208dc60d87a3f6aafb7ea25b Mon Sep 17 00:00:00 2001 From: Kirigaya <1193466151@qq.com> Date: Sat, 1 Jun 2024 02:14:46 +0800 Subject: [PATCH] finish pipe --- bot/api/onebot.ts | 82 ++++----- bot/event.ts | 79 ++++---- bot/impl.ts | 10 +- bot/lagrange-mapping.ts | 32 ++-- bot/main.ts | 35 +--- bot/plugins.ts | 3 +- bot/type.ts | 389 +++++++++++++++++++++++++++++++++++++--- scripts/make_ts_api.py | 4 +- 8 files changed, 487 insertions(+), 147 deletions(-) diff --git a/bot/api/onebot.ts b/bot/api/onebot.ts index c8adba9..e90babe 100644 --- a/bot/api/onebot.ts +++ b/bot/api/onebot.ts @@ -9,12 +9,12 @@ import * as Lagrange from '../type'; /** - * @description 发送私聊消息 + * @description 发送私聊消息 * @param user_id 对方 QQ 号 * @param message 要发送的内容 * @param auto_escape 消息内容是否作为纯文本发送(即不解析 CQ 码),只在 message 字段是字符串时有效 */ -export function sendPrivateMsg(user_id: number, message: Lagrange.Message, auto_escape: boolean = false) { +export function sendPrivateMsg(user_id: number, message: string | Lagrange.Send.Default[], auto_escape: boolean = false) { return { action: 'send_private_msg', params: { user_id, message, auto_escape } @@ -22,12 +22,12 @@ export function sendPrivateMsg(user_id: number, message: Lagrange.Message, auto_ } /** - * @description 发送群消息 + * @description 发送群消息 * @param group_id 群号 * @param message 要发送的内容 * @param auto_escape 消息内容是否作为纯文本发送(即不解析 CQ 码),只在 message 字段是字符串时有效 */ -export function sendGroupMsg(group_id: number, message: Lagrange.Message, auto_escape: boolean = false) { +export function sendGroupMsg(group_id: number, message: string | Lagrange.Send.Default[], auto_escape: boolean = false) { return { action: 'send_group_msg', params: { group_id, message, auto_escape } @@ -35,14 +35,14 @@ export function sendGroupMsg(group_id: number, message: Lagrange.Message, auto_e } /** - * @description 发送消息 + * @description 发送消息 * @param message_type 消息类型,支持 private、group,分别对应私聊、群组,如不传入,则根据传入的 *_id 参数判断 * @param user_id 对方 QQ 号(消息类型为 private 时需要) * @param group_id 群号(消息类型为 group 时需要) * @param message 要发送的内容 * @param auto_escape 消息内容是否作为纯文本发送(即不解析 CQ 码),只在 message 字段是字符串时有效 */ -export function sendMsg(message_type: string, user_id: number, group_id: number, message: Lagrange.Message, auto_escape: boolean = false) { +export function sendMsg(message_type: string, user_id: number, group_id: number, message: string | Lagrange.Send.Default[], auto_escape: boolean = false) { return { action: 'send_msg', params: { message_type, user_id, group_id, message, auto_escape } @@ -50,7 +50,7 @@ export function sendMsg(message_type: string, user_id: number, group_id: number, } /** - * @description 撤回消息 + * @description 撤回消息 * @param message_id 消息 ID */ export function deleteMsg(message_id: number) { @@ -61,7 +61,7 @@ export function deleteMsg(message_id: number) { } /** - * @description 获取消息 + * @description 获取消息 * @param message_id 消息 ID */ export function getMsg(message_id: number) { @@ -72,7 +72,7 @@ export function getMsg(message_id: number) { } /** - * @description 获取合并转发消息 + * @description 获取合并转发消息 * @param id 合并转发 ID */ export function getForwardMsg(id: string) { @@ -83,7 +83,7 @@ export function getForwardMsg(id: string) { } /** - * @description 发送好友赞 + * @description 发送好友赞 * @param user_id 对方 QQ 号 * @param times 赞的次数,每个好友每天最多 10 次 */ @@ -95,7 +95,7 @@ export function sendLike(user_id: number, times: number = 1) { } /** - * @description 群组踢人 + * @description 群组踢人 * @param group_id 群号 * @param user_id 要踢的 QQ 号 * @param reject_add_request 拒绝此人的加群请求 @@ -108,7 +108,7 @@ export function setGroupKick(group_id: number, user_id: number, reject_add_reque } /** - * @description 群组单人禁言 + * @description 群组单人禁言 * @param group_id 群号 * @param user_id 要禁言的 QQ 号 * @param duration 禁言时长,单位秒,0 表示取消禁言 @@ -121,7 +121,7 @@ export function setGroupBan(group_id: number, user_id: number, duration: number } /** - * @description 群组匿名用户禁言 + * @description 群组匿名用户禁言 * @param group_id 群号 * @param anonymous 可选,要禁言的匿名用户对象(群消息上报的 anonymous 字段) * @param anonymous_flag 可选,要禁言的匿名用户的 flag(需从群消息上报的数据中获得) @@ -135,7 +135,7 @@ export function setGroupAnonymousBan(group_id: number, anonymous: object, anonym } /** - * @description 群组全员禁言 + * @description 群组全员禁言 * @param group_id 群号 * @param enable 是否禁言 */ @@ -147,7 +147,7 @@ export function setGroupWholeBan(group_id: number, enable: boolean = true) { } /** - * @description 群组设置管理员 + * @description 群组设置管理员 * @param group_id 群号 * @param user_id 要设置管理员的 QQ 号 * @param enable true 为设置,false 为取消 @@ -160,7 +160,7 @@ export function setGroupAdmin(group_id: number, user_id: number, enable: boolean } /** - * @description 群组匿名 + * @description 群组匿名 * @param group_id 群号 * @param enable 是否允许匿名聊天 */ @@ -172,7 +172,7 @@ export function setGroupAnonymous(group_id: number, enable: boolean = true) { } /** - * @description 设置群名片(群备注) + * @description 设置群名片(群备注) * @param group_id 群号 * @param user_id 要设置的 QQ 号 * @param card 群名片内容,不填或空字符串表示删除群名片 @@ -185,7 +185,7 @@ export function setGroupCard(group_id: number, user_id: number, card: string = " } /** - * @description 设置群名 + * @description 设置群名 * @param group_id 群号 * @param group_name 新群名 */ @@ -197,7 +197,7 @@ export function setGroupName(group_id: number, group_name: string) { } /** - * @description 退出群组 + * @description 退出群组 * @param group_id 群号 * @param is_dismiss 是否解散,如果登录号是群主,则仅在此项为 true 时能够解散 */ @@ -209,7 +209,7 @@ export function setGroupLeave(group_id: number, is_dismiss: boolean = false) { } /** - * @description 设置群组专属头衔 + * @description 设置群组专属头衔 * @param group_id 群号 * @param user_id 要设置的 QQ 号 * @param special_title 专属头衔,不填或空字符串表示删除专属头衔 @@ -223,7 +223,7 @@ export function setGroupSpecialTitle(group_id: number, user_id: number, special_ } /** - * @description 处理加好友请求 + * @description 处理加好友请求 * @param flag 加好友请求的 flag(需从上报的数据中获得) * @param approve 是否同意请求 * @param remark 添加后的好友备注(仅在同意时有效) @@ -236,7 +236,7 @@ export function setFriendAddRequest(flag: string, approve: boolean = true, remar } /** - * @description 处理加群请求/邀请 + * @description 处理加群请求/邀请 * @param flag 加群请求的 flag(需从上报的数据中获得) * @param sub_type add 或 invite,请求类型(需要和上报消息中的 sub_type 字段相符) * @param approve 是否同意请求/邀请 @@ -250,7 +250,7 @@ export function setGroupAddRequest(flag: string, sub_type: string, approve: bool } /** - * @description 获取登录号信息 + * @description 获取登录号信息 */ export function getLoginInfo() { return { @@ -260,7 +260,7 @@ export function getLoginInfo() { } /** - * @description 获取陌生人信息 + * @description 获取陌生人信息 * @param user_id QQ 号 * @param no_cache 是否不使用缓存(使用缓存可能更新不及时,但响应更快) */ @@ -272,7 +272,7 @@ export function getStrangerInfo(user_id: number, no_cache: boolean = false) { } /** - * @description 获取好友列表 + * @description 获取好友列表 */ export function getFriendList() { return { @@ -282,7 +282,7 @@ export function getFriendList() { } /** - * @description 获取群信息 + * @description 获取群信息 * @param group_id 群号 * @param no_cache 是否不使用缓存(使用缓存可能更新不及时,但响应更快) */ @@ -294,7 +294,7 @@ export function getGroupInfo(group_id: number, no_cache: boolean = false) { } /** - * @description 获取群列表 + * @description 获取群列表 */ export function getGroupList() { return { @@ -304,7 +304,7 @@ export function getGroupList() { } /** - * @description 获取群成员信息 + * @description 获取群成员信息 * @param group_id 群号 * @param user_id QQ 号 * @param no_cache 是否不使用缓存(使用缓存可能更新不及时,但响应更快) @@ -317,7 +317,7 @@ export function getGroupMemberInfo(group_id: number, user_id: number, no_cache: } /** - * @description 获取群成员列表 + * @description 获取群成员列表 * @param group_id 群号 */ export function getGroupMemberList(group_id: number) { @@ -328,7 +328,7 @@ export function getGroupMemberList(group_id: number) { } /** - * @description 获取群荣誉信息 + * @description 获取群荣誉信息 * @param group_id 群号 * @param type 要获取的群荣誉类型,可传入 talkative performer legend strong_newbie emotion 以分别获取单个类型的群荣誉数据,或传入 all 获取所有数据 */ @@ -340,7 +340,7 @@ export function getGroupHonorInfo(group_id: number, type: string) { } /** - * @description 获取 Cookies + * @description 获取 Cookies * @param domain 需要获取 cookies 的域名 */ export function getCookies(domain: string = "") { @@ -351,7 +351,7 @@ export function getCookies(domain: string = "") { } /** - * @description 获取 CSRF Token + * @description 获取 CSRF Token */ export function getCsrfToken() { return { @@ -361,7 +361,7 @@ export function getCsrfToken() { } /** - * @description 获取 QQ 相关接口凭证 + * @description 获取 QQ 相关接口凭证 */ export function getCredentials() { return { @@ -371,7 +371,7 @@ export function getCredentials() { } /** - * @description 获取语音 + * @description 获取语音 */ export function getRecord() { return { @@ -381,7 +381,7 @@ export function getRecord() { } /** - * @description 获取图片 + * @description 获取图片 * @param file 收到的图片文件名(消息段的 file 参数),如 6B4DE3DFD1BD271E3297859D41C530F5.jpg */ export function getImage(file: string) { @@ -392,7 +392,7 @@ export function getImage(file: string) { } /** - * @description 检查是否可以发送图片 + * @description 检查是否可以发送图片 */ export function canSendImage() { return { @@ -402,7 +402,7 @@ export function canSendImage() { } /** - * @description 检查是否可以发送语音 + * @description 检查是否可以发送语音 */ export function canSendRecord() { return { @@ -412,7 +412,7 @@ export function canSendRecord() { } /** - * @description 获取运行状态 + * @description 获取运行状态 */ export function getStatus() { return { @@ -422,7 +422,7 @@ export function getStatus() { } /** - * @description 获取版本信息 + * @description 获取版本信息 */ export function getVersionInfo() { return { @@ -432,7 +432,7 @@ export function getVersionInfo() { } /** - * @description 重启 OneBot 实现 + * @description 重启 OneBot 实现 */ export function setRestart() { return { @@ -442,7 +442,7 @@ export function setRestart() { } /** - * @description 清理缓存 + * @description 清理缓存 */ export function cleanCache() { return { diff --git a/bot/event.ts b/bot/event.ts index e5cc4ff..334fe8d 100644 --- a/bot/event.ts +++ b/bot/event.ts @@ -1,50 +1,63 @@ -import type * as Lagrange from './type'; - import lagrangeMapper from './lagrange-mapping'; -function runPipe(message: Lagrange.Message) { - switch (message.post_type) { - case 'message': messagePipe(message); break; - case 'notice': noticePipe(message); break; - case 'request': requestPipe(message); break; - default: break; +import type * as Lagrange from './type'; +import type { LagrangeContext } from './context'; + +class Pipe { + context: LagrangeContext | undefined; + send: Lagrange.SendApi | undefined; + public injectContext(context: LagrangeContext) { + this.context = context; + this.send = context.send.bind(context); + } + + public run(message: Lagrange.Message) { + switch (message.post_type) { + case 'message': this.messagePipe(message); break; + case 'notice': this.noticePipe(message); break; + case 'request':this.requestPipe(message); break; + default: break; + } + } + + // 处理 message 类型的 post_type 消息 + public messagePipe(message: Lagrange.MessagePostType) { + switch (message.message_type) { + case 'private': + lagrangeMapper.resolvePrivateUser(message, this.send); + break; + case 'group': + lagrangeMapper.resolveGroup(message, this.send); + break; + default: + break; + } + } + + // 处理 notice 类型的 post_type 消息 + public noticePipe(message: Lagrange.NoticePostType) { + + } + + // 处理 request 类型的 post_type 消息 + public requestPipe(message: Lagrange.RequestPostType) { + } } -// 处理 message 类型的 post_type 的 -function messagePipe(message: Lagrange.MessagePostType) { - switch (message.message_type) { - case 'private': - - break; - case 'group': - - break; - default: - break; - } -} - -// 处理 notice 类型的 post_type 消息 -function noticePipe(message: Lagrange.NoticePostType) { - -} - -// 处理 request 类型的 post_type 消息 -function requestPipe(message: Lagrange.RequestPostType) { - -} +export const pipe = new Pipe(); export function onMessage(event: Buffer) { const messageBuffer = event.toString('utf-8'); const messageJson = JSON.parse(messageBuffer) as Lagrange.Message; // 忽略系统 message if (messageJson.post_type !== 'meta_event') { - runPipe(messageJson); + console.log('进入 runPipe'); + pipe.run(messageJson); } } export function onClose() { console.log('服务器连接关闭'); -} \ No newline at end of file +} diff --git a/bot/impl.ts b/bot/impl.ts index 7edd461..0fab7ad 100644 --- a/bot/impl.ts +++ b/bot/impl.ts @@ -1,13 +1,15 @@ import lagrangeMapper from './lagrange-mapping'; +import { apiQueryVecdb } from './api/vecdb'; + import type * as Lagrange from './type'; -class Impl { +export class Impl { - @lagrangeMapper.onPrivateUser({ user_id: 1193466151 }) - async handleJinhui(message: Lagrange.PrivateMessage) { + @lagrangeMapper.onPrivateUser(1193466151) + async handleJinhui(c: Lagrange.PrivateUserInvokeContext) { + console.log('raw message:' + c.message.raw_message); } - } \ No newline at end of file diff --git a/bot/lagrange-mapping.ts b/bot/lagrange-mapping.ts index a65380a..790040b 100644 --- a/bot/lagrange-mapping.ts +++ b/bot/lagrange-mapping.ts @@ -1,8 +1,9 @@ import assert from 'assert'; + import type * as Lagrange from './type'; -type PrivateUserInvoker = (message: Lagrange.PrivateMessage) => void; -type GroupUserInvoker = (message: Lagrange.GroupMessage) => void; +type PrivateUserInvoker = (context: Lagrange.PrivateUserInvokeContext) => Lagrange.Thenable; +type GroupUserInvoker = (context: Lagrange.GroupUserInvokeContext) => Lagrange.Thenable; type MessageInvoker = PrivateUserInvoker | GroupUserInvoker; @@ -17,7 +18,7 @@ interface CustomDescriptor { interface MessageInvokerStorage { invoker: T; - config: Partial + config?: Partial } class LagrangeMapper { @@ -37,24 +38,33 @@ class LagrangeMapper { return this._groupStorage; } - public matchPrivateUser(message: Lagrange.PrivateMessage) { + public resolvePrivateUser(message: Lagrange.PrivateMessage, send: Lagrange.SendApi) { const user_id = message.user_id; const userStorage = this._privateUserStorage.get(user_id); + console.log(user_id); + console.log(userStorage); + if (userStorage) { - userStorage.invoker(message); + userStorage.invoker({ message, send }); + } + } + + public resolveGroup(message: Lagrange.GroupMessage, send: Lagrange.SendApi) { + const group_id = message.group_id; + const groupStorage = this._groupStorage.get(group_id); + if (groupStorage) { + groupStorage.invoker({ message, send }); } } - public onPrivateUser(config: Partial) { - assert(config.user_id, 'onPrivateUser 中 user_id 不能为空'); - const _this = this; + public onPrivateUser(user_id: number) { + const _this = this; return function(target: any, propertyKey: string, descriptor: CustomDescriptor) { - const user_id = config.user_id; - if (_this.privateUserStorage.has(user_id)) { + if (_this._privateUserStorage.has(user_id)) { console.warn(`${propertyKey} -> 用户 ${user_id} 已经被注册过了,该操作将覆盖原本的!`); } const invoker = descriptor.value; - _this.privateUserStorage.set(user_id, { invoker, config }); + _this._privateUserStorage.set(user_id, { invoker }); } } diff --git a/bot/main.ts b/bot/main.ts index 44ac7a9..b8f27a7 100644 --- a/bot/main.ts +++ b/bot/main.ts @@ -1,35 +1,14 @@ import * as fs from 'fs'; -import WebSocket from 'ws'; +import lagServer from './context'; +import './impl'; -import { onMessage, onClose } from './event'; -import { apiQueryVecdb } from './api/vecdb'; +const buffer = fs.readFileSync('./app/publish/appsettings.json', 'utf-8'); +const config = JSON.parse(buffer); +const impl = config.Implementations[0]; -const lagrangeBuffer = fs.readFileSync('./app/publish/appsettings.json', 'utf-8'); -const lagrangeConfig = JSON.parse(lagrangeBuffer); -const impl = lagrangeConfig.Implementations[0]; -const connectionParam = { +lagServer.run({ host: impl.Host, port: impl.Port, path: impl.Suffix -}; - -const socket = new WebSocket.Server(connectionParam); - -socket.on('connection', (ws: WebSocket) => { - console.log('完成 ws 连接,启动参数如下'); - console.table(connectionParam); - - ws.on('message', onMessage); - ws.on('close', onClose); - - const testMsg = { - action: 'send_private_msg', - params: { - user_id: 1193466151, - message: '你好' - } - } - - ws.send(JSON.stringify(testMsg)); -}); +}); \ No newline at end of file diff --git a/bot/plugins.ts b/bot/plugins.ts index 0c99393..18a9dd5 100644 --- a/bot/plugins.ts +++ b/bot/plugins.ts @@ -1,4 +1,5 @@ -import type * as Lagrange from './type'; +import * as Lagrange from './type'; + class Plugins { registeredPlugins: Map; diff --git a/bot/type.ts b/bot/type.ts index ed3b917..c5eb4b5 100644 --- a/bot/type.ts +++ b/bot/type.ts @@ -38,36 +38,357 @@ export interface Sender { title?: string } -export interface MsgText { - type: 'text', - data: { - text: string +// 参考文档: https://github.com/botuniverse/onebot-11/blob/master/message/segment.md +export namespace Receive { + export interface Text { + type: 'text', + data: { + text: string + } } + + export interface Face { + type: 'face', + data: { + id: string + } + } + + export interface Image { + type: 'image', + data: { + file: string, + url: string, + // 在简略窗口可以看到的信息,对于图片来说,这就是 [图片] + summary: string + } + } + + export interface Audio { + type: 'record', + data: { + file: string, + magic: 0 | 1, + url: string + } + } + + export interface Video { + type: 'video', + data: { + file: string, + url: string + } + } + + export interface At { + type: 'at', + data: { + qq: string + } + } + + // 猜拳魔法表情 + export interface FingerGuess { + type: 'rps', + data: {} + } + + // 掷骰子魔法表情 + export interface Dice { + type: 'dice', + data: {} + } + + // 窗口抖动(戳一戳) + export interface WindowJitter { + type: 'shake', + data: {} + } + + // 戳一戳 + export interface Poke { + type: 'poke', + data: { + type: string, + id: string, + name: string + } + } + + export interface Link { + type: 'share', + data: { + // URL + url: string, + // 标题 + title: string, + // 发送时可选,内容描述 + content?: string, + // 发送时可选,图片 URL + image?: string + } + } + + export interface RecommendFriend { + type: 'contact', + data: { + type: 'qq', + // 被推荐人的 QQ 号 + id: string + } + } + + export interface RecommendGroup { + type: 'contact', + data: { + type: 'group', + // 被推荐群的群号 + id: string + } + } + + export interface Location { + type: 'location', + data: { + // 纬度 + lat: string, + // 经度 + lon: string, + // 发送时可选,标题 + title?: string, + // 发送时可选,内容描述 + content?: string + } + } + + export interface Reply { + type: 'reply', + data: { + id: string + } + } + + export interface Forward { + type: 'forward', + data: { + id: string + } + } + + export interface XML { + type: 'xml', + data: { + // XML 内容 + data: string + } + } + + export interface JSON { + type: 'json', + data: { + data: string + } + } + + export type Default = Text | Face | Image | Audio | Video | At | FingerGuess | Dice | WindowJitter | Poke | Link | RecommendFriend | RecommendGroup | Location | Reply | Forward | XML | JSON; } -export interface MsgImage { - type: 'image', - data: { - file: string, - url: string, - // 在简略窗口可以看到的信息,对于图片来说,这就是 [图片] - summary: string +export namespace Send { + export interface Text { + type: 'text', + data: { + text: string + } } + + export interface Face { + type: 'face', + data: { + id: string + } + } + + export interface Image { + type: 'image', + data: { + /** + * 发送时 file 可行的三种取值 + * 1. 绝对路径,例如 file:///C:\\Users\Richard\Pictures\1.png + * 2. 网络 URL,例如 http://i1.piimg.com/567571/fdd6e7b6d93f1ef0.jpg + * Base64 编码,例如 base64://iVBORw0KGgoAAAANSUhEUgAAABQAAAAVCAIAAADJt1n/AAAAKElEQVQ4EWPk5+RmIBcwkasRpG9UM4mhNxpgowFGMARGEwnBIEJVAAAdBgBNAZf+QAAAAABJRU5ErkJggg== + */ + file: string, + + // 只在通过网络 URL 发送时有效,表示是否使用已缓存的文件,默认 1 + cache: 0 | 1, + + // 只在通过网络 URL 发送时有效,表示是否通过代理下载文件(需通过环境变量或配置文件配置代理),默认 1 + proxy: 0 | 1, + + // 只在通过网络 URL 发送时有效,单位秒,表示下载网络文件的超时时间,默认不超时 + timeout: number + } + } + + export interface Audio { + type: 'record', + data: { + file: string, + magic: 0 | 1, + cache: 0 | 1, + proxy: 0 | 1, + timeout: number + } + } + + export interface Video { + type: 'video', + data: { + file: string, + cache: 0 | 1, + proxy: 0 | 1, + timeout: number + } + } + + export interface At { + type: 'at', + data: { + qq: string + } + } + + export interface FingerGuess { + type: 'rps', + data: {} + } + + export interface Dice { + type: 'dice', + data: {} + } + + export interface WindowJitter { + type: 'shake', + data: {} + } + + // 戳一戳 + export interface Poke { + type: 'poke', + data: { + type: string, + id: string, + } + } + + export interface Anonymous { + type: 'anonymous', + data: {} + } + + export interface Link { + type: 'share', + data: { + // URL + url: string, + // 标题 + title: string, + // 发送时可选,内容描述 + content?: string, + // 发送时可选,图片 URL + image?: string + } + } + + export interface RecommendFriend { + type: 'contact', + data: { + type: 'qq', + // 被推荐人的 QQ 号 + id: string + } + } + + export interface RecommendGroup { + type: 'contact', + data: { + type: 'group', + // 被推荐群的群号 + id: string + } + } + + export interface Location { + type: 'location', + data: { + // 纬度 + lat: string, + // 经度 + lon: string, + // 发送时可选,标题 + title?: string, + // 发送时可选,内容描述 + content?: string + } + } + + export interface MusicShare { + type: 'music', + data: { + // 分别表示使用 QQ 音乐、网易云音乐、虾米音乐 + type: 'qq' | '163' | 'xm', + // 歌曲 ID + id: string + } + } + + export interface CustomMusicShare { + type: 'music', + data: { + type: 'custom', + url: string, + audio: string, + title: string, + content: string, + image: string + } + } + + export interface Reply { + type: 'reply', + data: { + id: string + } + } + + export interface ForwardNode { + type: 'node', + data: { + id: string + } + } + + export interface XML { + type: 'xml', + data: { + // XML 内容 + data: string + } + } + + export interface JSON { + type: 'json', + data: { + data: string + } + } + + export type Default = Text | Face | Image | Audio | Video | At | FingerGuess | Dice | WindowJitter | Poke | Anonymous | Link | RecommendFriend | RecommendGroup | Location | MusicShare | CustomMusicShare | Reply | ForwardNode | XML | JSON; } -export interface MsgFace { - type: 'face', - data: { - id: string - } -} -export interface MsgAt { - type: 'at', - data: { - qq: string - } -} export interface MsgFile { // 一般是 '' @@ -103,7 +424,7 @@ export interface CommonMessage { // 是否为匿名发言,一般都是 null anonymous?: null | boolean, // 消息内容(结构化) - message?: MsgText | MsgImage | MsgFace | MsgAt, + message?: Receive.Default, // 消息内容(纯文本) raw_message?: string, // 发送的时间戳 @@ -125,7 +446,7 @@ export interface PrivateMessage { // 发消息的人的 QQ 号 user_id: number, // 消息内容(结构化) - message: MsgText | MsgImage | MsgFace | MsgAt, + message: Receive.Default, // 消息内容(纯文本) raw_message: string, // 发送的时间戳 @@ -136,6 +457,7 @@ export interface PrivateMessage { font?: number } + export interface GroupMessage { // 事件类型 post_type: 'message', @@ -152,7 +474,7 @@ export interface GroupMessage { // 是否为匿名发言,一般都是 null anonymous: null | boolean, // 消息内容(结构化) - message: MsgText | MsgImage | MsgFace | MsgAt, + message: Receive.Default, // 消息内容(纯文本) raw_message: string, // 发送的时间戳 @@ -204,4 +526,17 @@ export interface ApproveMessage { export type Message = MetaMessage | PrivateMessage | GroupMessage | FileMessage | AddMessage | ApproveMessage; export type MessagePostType = PrivateMessage | GroupMessage; export type NoticePostType = FileMessage | ApproveMessage; -export type RequestPostType = AddMessage; \ No newline at end of file +export type RequestPostType = AddMessage; + + +export type Thenable = T | Promise; + +export type SendApi = (msg: string | Send.Default[]) => Thenable; + +export interface InvokerContext { + message: M, + send: SendApi +} + +export type PrivateUserInvokeContext = InvokerContext; +export type GroupUserInvokeContext = InvokerContext; diff --git a/scripts/make_ts_api.py b/scripts/make_ts_api.py index e9e4a13..56ab477 100644 --- a/scripts/make_ts_api.py +++ b/scripts/make_ts_api.py @@ -96,11 +96,11 @@ for el in soup.find_all('h2'): } if param['type'] == 'message': - param['type'] = 'Lagrange.Message' + param['type'] = 'string | Lagrange.Send.Default[]' params.append(param) - t1 = function_desc + t1 = function_desc.strip() t2 = '\n' for param in params: t2 += ' * @param {} {}\n'.format(param['name'], param['desc'])