diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 00000000..9d08a1a8 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,9 @@ +root = true + +[*] +charset = utf-8 +indent_style = space +indent_size = 2 +end_of_line = lf +insert_final_newline = true +trim_trailing_whitespace = true diff --git a/.vscode/settings.json b/.vscode/settings.json index 702f844f..7c9ea1ee 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,6 +1,12 @@ { + "cSpell.words": [ + "mamoe", + "Relogin", + "mirai" + ], + "prettier.enable": false, "editor.codeActionsOnSave": { "source.fixAll.eslint": true }, - "cSpell.words": ["mamoe", "Relogin", "mirai"] + "editor.formatOnSave": false } diff --git a/README.md b/README.md index 8bde1f2e..4a4c61f0 100644 --- a/README.md +++ b/README.md @@ -168,6 +168,9 @@ pnpm mcl # 参考 https://github.com/project-mirai/mirai-login-solver-selenium 获取 ticket -# 启动 demo +# 开发(监听文件变动并构建)(打开新终端窗口) +pnpm dev + +# 启动 demo(打开新终端窗口) pnpm demo ``` diff --git a/package.json b/package.json index 574ac6b8..b1607d43 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "mirai-ts", - "version": "2.4.1", + "version": "2.4.2", "packageManager": "pnpm@7.12.2", "description": "Mirai TypeScript SDK", "author": { diff --git a/src/mirai-api-http/index.ts b/src/mirai-api-http/index.ts index 11382d11..cd4e41c2 100644 --- a/src/mirai-api-http/index.ts +++ b/src/mirai-api-http/index.ts @@ -10,6 +10,7 @@ import FormData from 'form-data' import WebSocket from 'ws' import { Logger } from '@yunyoujun/logger' import chalk from 'chalk' +import { isChatMessage } from '../utils/check' import type { Api, Config, @@ -52,6 +53,8 @@ interface BaseVerifyParams { verifyKey: string } +type RecallParams = Pick + export class MiraiApiHttp { setting: MiraiApiHttpSetting sessionKey = '' @@ -148,8 +151,8 @@ export class MiraiApiHttp { */ async about() { const { data } = await this.axios.get< - null, - AxiosResponse + null, + AxiosResponse >('/about') return data } @@ -161,8 +164,8 @@ export class MiraiApiHttp { this.logger.info(`[http] Address: ${this.http.address}`) const { data } = await this.axios.post< - BaseVerifyParams, - AxiosResponse + BaseVerifyParams, + AxiosResponse >('/verify', { verifyKey, }) @@ -183,8 +186,8 @@ export class MiraiApiHttp { async bind(qq: number) { this.qq = qq const { data } = await this.axios.post< - Api.Params.RequestParams<{ qq: number }>, - AxiosResponse + Api.Params.RequestParams<{ qq: number }>, + AxiosResponse >('/bind', { sessionKey: this.sessionKey, qq, @@ -199,8 +202,8 @@ export class MiraiApiHttp { */ async release(qq = this.qq) { const { data } = await this.axios.post< - Api.Params.RequestParams<{ qq: number }>, - AxiosResponse + Api.Params.RequestParams<{ qq: number }>, + AxiosResponse >('/release', { sessionKey: this.sessionKey, qq, @@ -219,8 +222,8 @@ export class MiraiApiHttp { */ async fetchMessage(count = 10) { const { data } = await this.axios.get< - Api.Params.RequestParams<{ count: number }>, - AxiosResponse + Api.Params.RequestParams<{ count: number }>, + AxiosResponse >('/fetchMessage', { params: { sessionKey: this.sessionKey, @@ -236,8 +239,8 @@ export class MiraiApiHttp { */ async fetchLatestMessage(count = 10) { const { data } = await this.axios.get< - Api.Params.RequestParams<{ count: number }>, - AxiosResponse + Api.Params.RequestParams<{ count: number }>, + AxiosResponse >('/fetchLatestMessage', { params: { sessionKey: this.sessionKey, @@ -253,8 +256,8 @@ export class MiraiApiHttp { */ async peekMessage(count = 10) { const { data } = await this.axios.get< - Api.Params.RequestParams<{ count: number }>, - AxiosResponse + Api.Params.RequestParams<{ count: number }>, + AxiosResponse >('/peekMessage', { params: { sessionKey: this.sessionKey, @@ -270,8 +273,8 @@ export class MiraiApiHttp { */ async peekLatestMessage(count = 10) { const { data } = await this.axios.get< - Api.Params.RequestParams<{ count: number }>, - AxiosResponse + Api.Params.RequestParams<{ count: number }>, + AxiosResponse >('/peekLatestMessage', { params: { sessionKey: this.sessionKey, @@ -288,8 +291,8 @@ export class MiraiApiHttp { */ async messageFromId(id: number, target: number) { const { data } = await this.axios.get< - Api.Params.RequestParams<{ id: number; target: number }>, - AxiosResponse + Api.Params.RequestParams<{ id: number; target: number }>, + AxiosResponse >('/messageFromId', { params: { sessionKey: this.sessionKey, @@ -324,8 +327,8 @@ export class MiraiApiHttp { payload.quote = quote const { data } = await this.axios.post< - Api.Params.SendFriendMessage, - AxiosResponse + Api.Params.SendFriendMessage, + AxiosResponse >('/sendFriendMessage', payload) return data } @@ -352,8 +355,8 @@ export class MiraiApiHttp { payload.quote = quote const { data } = await this.axios.post< - Api.Params.SendGroupMessage, - AxiosResponse + Api.Params.SendGroupMessage, + AxiosResponse >('/sendGroupMessage', payload) return data } @@ -382,8 +385,8 @@ export class MiraiApiHttp { payload.quote = quote const { data } = await this.axios.post< - Api.Params.SendTempMessage, - AxiosResponse + Api.Params.SendTempMessage, + AxiosResponse >('/sendTempMessage', payload) return data } @@ -402,8 +405,8 @@ export class MiraiApiHttp { group?: number, ) { const { data } = await this.axios.post< - Api.Params.SendImageMessage, - AxiosResponse + Api.Params.SendImageMessage, + AxiosResponse >('/sendImageMessage', { sessionKey: this.sessionKey, target, @@ -425,8 +428,8 @@ export class MiraiApiHttp { form.append('type', type) form.append('img', img) const { data } = await this.axios.post< - FormData, - AxiosResponse + FormData, + AxiosResponse >('/uploadImage', form, { headers: form.getHeaders(), // same as post: { 'Content-Type': 'multipart/form-data' } }) @@ -444,8 +447,8 @@ export class MiraiApiHttp { form.append('type', type) form.append('voice', voice) const { data } = await this.axios.post< - FormData, - AxiosResponse + FormData, + AxiosResponse >('/uploadVoice', form, { headers: form.getHeaders(), }) @@ -480,19 +483,36 @@ export class MiraiApiHttp { /** * 撤回消息 * 使用此方法撤回指定消息。对于bot发送的消息,有2分钟时间限制。对于撤回群聊中群员的消息,需要有相应权限 - * @param target 需要撤回的消息的messageId + * @desc mirai 更新强制需要 messageId + target + * @param params { messageId, target } */ - async recall(target: Api.Params.Recall['target']) { - let messageId = target - if (typeof target !== 'number' && target.messageChain[0].id) - messageId = target.messageChain[0].id + async recall( + params: + | RecallParams + | MessageType.ChatMessage, + ) { + if ( + typeof params !== 'object' + || !(params as RecallParams).messageId + || !(params as RecallParams).target + ) + throw new Error('请同时传入包含有 messageId 与 target 的对象') + + let messageId = (params as RecallParams).messageId + let target = (params as RecallParams).target + // 传入 ChatMessage + if (isChatMessage(params) && params.messageChain[0].id) { + messageId = params.messageChain[0].id + target = params.sender.id + } const { data } = await this.axios.post< - Api.Params.Recall, - AxiosResponse + Api.Params.Recall, + AxiosResponse >('/recall', { sessionKey: this.sessionKey, - target: messageId, + messageId, + target, }) return data } @@ -502,8 +522,8 @@ export class MiraiApiHttp { */ async friendList() { const { data } = await this.axios.get< - Api.Params.BaseRequestParams, - AxiosResponse + Api.Params.BaseRequestParams, + AxiosResponse >('/friendList', { params: { sessionKey: this.sessionKey, @@ -517,8 +537,8 @@ export class MiraiApiHttp { */ async groupList() { const { data } = await this.axios.get< - Api.Params.BaseRequestParams, - AxiosResponse + Api.Params.BaseRequestParams, + AxiosResponse >('/groupList', { params: { sessionKey: this.sessionKey, @@ -533,8 +553,8 @@ export class MiraiApiHttp { */ async memberList(target: number) { const { data } = await this.axios.get< - Api.Params.RequestParams<{ target: number }>, - AxiosResponse + Api.Params.RequestParams<{ target: number }>, + AxiosResponse >('/memberList', { params: { sessionKey: this.sessionKey, @@ -577,8 +597,8 @@ export class MiraiApiHttp { */ async muteAll(target: Api.Params.MuteAll['target']) { const { data } = await this.axios.post< - Api.Params.MuteAll, - AxiosResponse + Api.Params.MuteAll, + AxiosResponse >('/muteAll', { sessionKey: this.sessionKey, target, @@ -592,8 +612,8 @@ export class MiraiApiHttp { */ async unmuteAll(target: Api.Params.UnmuteAll['target']) { const { data } = await this.axios.post< - Api.Params.UnmuteAll, - AxiosResponse + Api.Params.UnmuteAll, + AxiosResponse >('/unmuteAll', { sessionKey: this.sessionKey, target, @@ -609,8 +629,8 @@ export class MiraiApiHttp { */ async mute(target: number, memberId: number, time = 60) { const { data } = await this.axios.post< - Api.Params.Mute, - AxiosResponse + Api.Params.Mute, + AxiosResponse >('/mute', { sessionKey: this.sessionKey, target, @@ -627,8 +647,8 @@ export class MiraiApiHttp { */ async unmute(target: number, memberId: number) { const { data } = await this.axios.post< - Api.Params.Unmute, - AxiosResponse + Api.Params.Unmute, + AxiosResponse >('/unmute', { sessionKey: this.sessionKey, target, @@ -645,8 +665,8 @@ export class MiraiApiHttp { */ async kick(target: number, memberId: number, msg = '您已被移出群聊') { const { data } = await this.axios.post< - Api.Params.Kick, - AxiosResponse + Api.Params.Kick, + AxiosResponse >('/kick', { sessionKey: this.sessionKey, target, @@ -663,8 +683,8 @@ export class MiraiApiHttp { */ async quit(target: number) { const { data } = await this.axios.post< - Api.Params.Quit, - AxiosResponse + Api.Params.Quit, + AxiosResponse >('/quit', { sessionKey: this.sessionKey, target, @@ -681,8 +701,8 @@ export class MiraiApiHttp { async groupConfig(target: number, config?: Config.GroupConfig) { if (config) { const { data } = await this.axios.post< - Api.Params.GroupConfig, - AxiosResponse + Api.Params.GroupConfig, + AxiosResponse >('/groupConfig', { sessionKey: this.sessionKey, target, @@ -692,8 +712,8 @@ export class MiraiApiHttp { } else { const { data } = await this.axios.get< - Api.Params.GroupConfig, - AxiosResponse + Api.Params.GroupConfig, + AxiosResponse >('/groupConfig', { params: { sessionKey: this.sessionKey, @@ -718,8 +738,8 @@ export class MiraiApiHttp { ) { if (info) { const { data } = await this.axios.post< - Api.Params.MemberInfo, - AxiosResponse + Api.Params.MemberInfo, + AxiosResponse >('/memberInfo', { sessionKey: this.sessionKey, target, @@ -730,8 +750,8 @@ export class MiraiApiHttp { } else { const { data } = await this.axios.get< - Api.Params.MemberInfo, - AxiosResponse + Api.Params.MemberInfo, + AxiosResponse >('/memberInfo', { params: { sessionKey: this.sessionKey, @@ -842,8 +862,8 @@ export class MiraiApiHttp { */ async setEssence(target: number) { const { data } = await this.axios.post< - Api.Params.SetEssence, - AxiosResponse + Api.Params.SetEssence, + AxiosResponse >('/setEssence', { sessionKey: this.sessionKey, target, @@ -863,8 +883,8 @@ export class MiraiApiHttp { kind: Api.Params.SendNudge['kind'] = 'Group', ) { const { data } = await this.axios.post< - Api.Params.SendNudge, - AxiosResponse + Api.Params.SendNudge, + AxiosResponse >('/sendNudge', { sessionKey: this.sessionKey, target, diff --git a/src/types/api/params.ts b/src/types/api/params.ts index 8c9635af..bd22dcf0 100644 --- a/src/types/api/params.ts +++ b/src/types/api/params.ts @@ -41,7 +41,11 @@ export type SendImageMessage = RequestParams<{ }> export type Recall = RequestParams<{ - target: number | MessageType.ChatMessage + /** + * 好友id或群id + */ + target: number + messageId: number }> export type Unmute = RequestParams<{ diff --git a/src/utils/check.ts b/src/utils/check.ts index 0bf3771d..b4c05af2 100644 --- a/src/utils/check.ts +++ b/src/utils/check.ts @@ -106,8 +106,10 @@ export function match( * @param msg 消息链 */ export function isChatMessage( - msg: MessageType.ChatMessage | EventType.Event, + msg: MessageType.ChatMessage | EventType.Event | any, ): msg is MessageType.ChatMessage { + if (!msg || !msg.type) + return false const msgType = ['FriendMessage', 'GroupMessage', 'TempMessage'] return msgType.includes(msg.type) }