-
Notifications
You must be signed in to change notification settings - Fork 201
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
移除 cq-websocket 依赖;添加 cq-http 支持;实现QQ机器人推送、签到;一些优化
- Loading branch information
Showing
17 changed files
with
1,007 additions
and
1,242 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
export default class CQ { | ||
#ws_url: string; | ||
#ws_instance?: WebSocket; | ||
#target_type: string; | ||
#target_id: number; | ||
#cache: Map<string, any>; | ||
|
||
constructor(ws_url: string); | ||
constructor(ws_url: string, target_type: string, target_id: number); | ||
constructor(arg1: string, arg2?: string, arg3?: number) { | ||
this.#ws_url = arg1; | ||
this.#target_type = arg2 || 'private'; | ||
this.#target_id = arg3 || 1001; | ||
this.#cache = new Map(); | ||
} | ||
|
||
static hasImage = (msg: string) => { | ||
return msg.includes('[CQ:image'); | ||
}; | ||
|
||
connect() { | ||
if (!this.#ws_instance) this.#ws_instance = new WebSocket(this.#ws_url); | ||
// 连接失败处理 | ||
this.#ws_instance.onerror = function (e) { | ||
console.log('[连接异常]', e); | ||
}; | ||
return this; | ||
} | ||
|
||
send(text: string, target_id: number): void; | ||
send(text: string, target_id: number, action: string, payload: any): void; | ||
send(arg1: string, arg2: number, arg3?: string, arg4?: any): void { | ||
if (arg3) { this.#ws_instance?.send(JSON.stringify({ action: arg3, params: arg4 })); return; }; | ||
const payload: any = { | ||
action: arg3 || 'send_msg', | ||
params: { | ||
message_type: this.#target_type, | ||
message: arg1, | ||
auto_escape: true | ||
} | ||
}; | ||
this.#target_type === 'private' ? payload.params.user_id = arg2 : payload.params.group_id = arg2; | ||
this.#ws_instance?.send(JSON.stringify(payload)); | ||
} | ||
|
||
close() { | ||
this.#ws_instance?.close(); | ||
} | ||
|
||
getTargetID() { | ||
return this.#target_id; | ||
} | ||
|
||
getCache(key: string) { | ||
return this.#cache.get(key); | ||
} | ||
|
||
setCache(key: string, object: any) { | ||
this.#cache.set(key, object); | ||
} | ||
|
||
clearCache() { | ||
this.#cache.clear(); | ||
} | ||
|
||
onMessage(handler: (this: CQ, data: string) => void) { | ||
if (this.#ws_instance) { | ||
const context = this; | ||
this.#ws_instance.onmessage = function (e) { | ||
const data = JSON.parse(e.data); | ||
const isTarget = !!(data.group_id === context.#target_id || data.user_id === context.#target_id); | ||
// 仅处理消息类型,且来自目标用户或群 | ||
if (data.post_type === 'message' && isTarget) { | ||
handler.call(context, data.raw_message); | ||
} | ||
}; | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,17 @@ | ||
import { PPTSIGN } from '../configs/api'; | ||
import { cookieSerialize, request } from '../utils/request'; | ||
|
||
export const QRCodeSign = async (args: BasicCookie & { enc: string; name: string; fid: string; activeId: string }) => { | ||
export const QRCodeSign = async (args: BasicCookie & { enc: string; name: string; fid: string; activeId: string; }) => { | ||
const { enc, name, fid, activeId, ...cookies } = args; | ||
const url = `${PPTSIGN.URL}?enc=${enc}&name=${encodeURI(name)}&activeId=${activeId}&uid=${ | ||
cookies._uid | ||
}&clientip=&useragent=&latitude=-1&longitude=-1&fid=${fid}&appType=15`; | ||
const url = `${PPTSIGN.URL}?enc=${enc}&name=${encodeURI(name)}&activeId=${activeId}&uid=${cookies._uid | ||
}&clientip=&useragent=&latitude=-1&longitude=-1&fid=${fid}&appType=15`; | ||
const result = await request(url, { | ||
secure: true, | ||
headers: { | ||
Cookie: cookieSerialize(cookies), | ||
}, | ||
}); | ||
if (result.data === 'success') { | ||
console.log(`[二维码]签到成功`); | ||
return 'success'; | ||
} else { | ||
console.log(result.data); | ||
return result.data; | ||
} | ||
const msg = result.data === 'success' ? '[二维码]签到成功' : `[二维码]${result.data}`; | ||
console.log(msg); | ||
return msg; | ||
}; |
Oops, something went wrong.