mirror of
https://github.com/LLOneBot/LLOneBot.git
synced 2024-11-22 01:56:33 +00:00
feat: 上报支持CQCode
This commit is contained in:
parent
4941f0071a
commit
cfb066971f
@ -29,13 +29,13 @@ export class ConfigUtil {
|
||||
enableHttp: true,
|
||||
enableHttpPost: true,
|
||||
enableWs: true,
|
||||
enableWsReverse: false
|
||||
enableWsReverse: false,
|
||||
messagePostFormat: "array",
|
||||
}
|
||||
let defaultConfig: Config = {
|
||||
ob11: ob11Default,
|
||||
heartInterval: 60000,
|
||||
token: "",
|
||||
messagePostFormat: "array",
|
||||
enableLocalFile2Url: false,
|
||||
debug: false,
|
||||
log: false,
|
||||
|
@ -7,13 +7,13 @@ export interface OB11Config {
|
||||
enableHttpPost?: boolean
|
||||
enableWs?: boolean
|
||||
enableWsReverse?: boolean
|
||||
messagePostFormat?: 'array' | 'string'
|
||||
}
|
||||
|
||||
export interface Config {
|
||||
ob11: OB11Config
|
||||
token?: string
|
||||
heartInterval?: number // ms
|
||||
messagePostFormat?: 'array' | 'string'
|
||||
enableLocalFile2Url?: boolean // 开启后,本地文件路径图片会转成http链接, 语音会转成base64
|
||||
debug?: boolean
|
||||
reportSelfMessage?: boolean
|
||||
|
9
src/global.d.ts
vendored
9
src/global.d.ts
vendored
@ -4,14 +4,7 @@ import {LLOneBot} from "./preload";
|
||||
|
||||
declare global {
|
||||
interface Window {
|
||||
llonebot: typeof llonebot;
|
||||
llonebot: LLOneBot;
|
||||
LiteLoader: any;
|
||||
}
|
||||
|
||||
interface Event {
|
||||
detail?: {
|
||||
name: string;
|
||||
value: string;
|
||||
}
|
||||
}
|
||||
}
|
@ -9,7 +9,7 @@ import {ActionName, BaseCheckResult} from "./types";
|
||||
import * as fs from "fs";
|
||||
import {log} from "../../common/utils";
|
||||
import {v4 as uuidv4} from "uuid"
|
||||
import {parseCQCode} from "../cqcode";
|
||||
import {decodeCQCode} from "../cqcode";
|
||||
|
||||
function checkSendMessage(sendMsgList: OB11MessageData[]) {
|
||||
function checkUri(uri: string): boolean {
|
||||
@ -124,7 +124,7 @@ export class SendMsg extends BaseAction<OB11PostSendMsg, ReturnDataType> {
|
||||
// text: message
|
||||
// }
|
||||
// }] as OB11MessageData[]
|
||||
message = parseCQCode(message.toString())
|
||||
message = decodeCQCode(message.toString())
|
||||
} else if (!Array.isArray(message)) {
|
||||
message = [message]
|
||||
}
|
||||
|
@ -7,27 +7,18 @@ import {
|
||||
OB11MessageDataType,
|
||||
OB11User
|
||||
} from "./types";
|
||||
import {
|
||||
AtType,
|
||||
ChatType,
|
||||
Friend,
|
||||
Group,
|
||||
GroupMember,
|
||||
IMAGE_HTTP_HOST,
|
||||
RawMessage,
|
||||
SelfInfo,
|
||||
User
|
||||
} from '../ntqqapi/types';
|
||||
import {AtType, ChatType, Group, GroupMember, IMAGE_HTTP_HOST, RawMessage, SelfInfo, User} from '../ntqqapi/types';
|
||||
import {getFriend, getGroupMember, getHistoryMsgBySeq, selfInfo} from '../common/data';
|
||||
import {file2base64, getConfigUtil, log} from "../common/utils";
|
||||
import {NTQQApi} from "../ntqqapi/ntcall";
|
||||
import {EventType} from "./event/OB11BaseEvent";
|
||||
import {encodeCQCode} from "./cqcode";
|
||||
|
||||
|
||||
export class OB11Constructor {
|
||||
static async message(msg: RawMessage): Promise<OB11Message> {
|
||||
|
||||
const {enableLocalFile2Url, messagePostFormat} = getConfigUtil().getConfig()
|
||||
const {enableLocalFile2Url, ob11: {messagePostFormat}} = getConfigUtil().getConfig()
|
||||
const message_type = msg.chatType == ChatType.group ? "group" : "private";
|
||||
const resMsg: OB11Message = {
|
||||
self_id: parseInt(selfInfo.uin),
|
||||
@ -92,14 +83,11 @@ export class OB11Constructor {
|
||||
}
|
||||
} else if (element.textElement) {
|
||||
message_data["type"] = "text"
|
||||
let text= element.textElement.content
|
||||
if (!text.trim()){
|
||||
let text = element.textElement.content
|
||||
if (!text.trim()) {
|
||||
continue;
|
||||
}
|
||||
message_data["data"]["text"] = text
|
||||
if (text){
|
||||
resMsg.raw_message += text
|
||||
}
|
||||
} else if (element.picElement) {
|
||||
message_data["type"] = "image"
|
||||
message_data["data"]["file_id"] = element.picElement.fileUuid
|
||||
@ -156,8 +144,11 @@ export class OB11Constructor {
|
||||
}
|
||||
|
||||
if (message_data.type !== "unknown" && message_data.data) {
|
||||
if (messagePostFormat === 'string') (resMsg.message as string) += CQCodeBuilder(message_data);
|
||||
else (resMsg.message as OB11MessageData[]).push(message_data);
|
||||
if (messagePostFormat === 'string') {
|
||||
const cqCode = encodeCQCode(message_data);
|
||||
(resMsg.message as string) += cqCode;
|
||||
resMsg.raw_message += cqCode;
|
||||
} else (resMsg.message as OB11MessageData[]).push(message_data);
|
||||
}
|
||||
}
|
||||
resMsg.raw_message = resMsg.raw_message.trim();
|
||||
@ -220,24 +211,3 @@ export class OB11Constructor {
|
||||
return groups.map(OB11Constructor.group)
|
||||
}
|
||||
}
|
||||
|
||||
function CQCodeBuilder(data: OB11MessageData) {
|
||||
const CQCodeEscape = (text: string) => {
|
||||
return text.replace(/\[/g, '[')
|
||||
.replace(/\]/g, ']')
|
||||
.replace(/\&/g, '&')
|
||||
.replace(/,/g, ',');
|
||||
};
|
||||
|
||||
if (data.type === 'text') {
|
||||
return CQCodeEscape(data.data.text);
|
||||
}
|
||||
|
||||
let result = '[CQ:' + data.type;
|
||||
for (const name in data.data) {
|
||||
const value = data.data[name];
|
||||
result += `,${name}=${CQCodeEscape(value)}`;
|
||||
}
|
||||
result += ']';
|
||||
return result;
|
||||
}
|
||||
|
@ -29,7 +29,7 @@ function h(type: string, data: any) {
|
||||
}
|
||||
}
|
||||
|
||||
export function parseCQCode(source: string): OB11MessageData[] {
|
||||
export function decodeCQCode(source: string): OB11MessageData[] {
|
||||
const elements: any[] = []
|
||||
let result: ReturnType<typeof from>
|
||||
while ((result = from(source))) {
|
||||
@ -44,6 +44,28 @@ export function parseCQCode(source: string): OB11MessageData[] {
|
||||
return elements
|
||||
}
|
||||
|
||||
|
||||
export function encodeCQCode(data: OB11MessageData) {
|
||||
const CQCodeEscape = (text: string) => {
|
||||
return text.replace(/\[/g, '[')
|
||||
.replace(/\]/g, ']')
|
||||
.replace(/\&/g, '&')
|
||||
.replace(/,/g, ',');
|
||||
};
|
||||
|
||||
if (data.type === 'text') {
|
||||
return CQCodeEscape(data.data.text);
|
||||
}
|
||||
|
||||
let result = '[CQ:' + data.type;
|
||||
for (const name in data.data) {
|
||||
const value = data.data[name];
|
||||
result += `,${name}=${CQCodeEscape(value)}`;
|
||||
}
|
||||
result += ']';
|
||||
return result;
|
||||
}
|
||||
|
||||
// const result = parseCQCode("[CQ:at,qq=114514]早上好啊[CQ:image,file=http://baidu.com/1.jpg,type=show,id=40004]")
|
||||
// const result = parseCQCode("好好好")
|
||||
// console.log(JSON.stringify(result))
|
@ -113,8 +113,8 @@ async function onSettingWindowCreated(view: Element) {
|
||||
<setting-text data-type="secondary">如客户端无特殊需求推荐保持默认设置,两者的详细差异可参考 <a href="javascript:LiteLoader.api.openExternal('https://github.com/botuniverse/onebot-11/tree/master/message#readme');">OneBot v11 文档</a></setting-text>
|
||||
</div>
|
||||
<setting-select id="messagePostFormat">
|
||||
<setting-option data-value="array" ${config.messagePostFormat !== "string" ? "is-selected" : ""}>Array</setting-option>
|
||||
<setting-option data-value="string" ${config.messagePostFormat === "string" ? "is-selected" : ""}>String</setting-option>
|
||||
<setting-option data-value="array" ${config.ob11.messagePostFormat !== "string" ? "is-selected" : ""}>消息段</setting-option>
|
||||
<setting-option data-value="string" ${config.ob11.messagePostFormat === "string" ? "is-selected" : ""}>CQ码</setting-option>
|
||||
</setting-select>
|
||||
</setting-item>
|
||||
<setting-item data-direction="row" class="hostItem vertical-list-item">
|
||||
@ -185,10 +185,9 @@ async function onSettingWindowCreated(view: Element) {
|
||||
|
||||
doc.getElementById("addHttpHost").addEventListener("click", () => addHostEle("http"))
|
||||
doc.getElementById("addWsHost").addEventListener("click", () => addHostEle("ws"))
|
||||
doc.getElementById("messagePostFormat").addEventListener("selected", (e) => {
|
||||
const _config = config || {};
|
||||
_config.messagePostFormat = e.detail && !isEmpty(e.detail.value) ? e.detail.value : 'array';
|
||||
window.llonebot.setConfig(_config);
|
||||
doc.getElementById("messagePostFormat").addEventListener("selected", (e: CustomEvent) => {
|
||||
config.ob11.messagePostFormat = e.detail && !isEmpty(e.detail.value) ? e.detail.value : 'array';
|
||||
window.llonebot.setConfig(config);
|
||||
})
|
||||
|
||||
function switchClick(eleId: string, configKey: string, _config=null) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user