Merge pull request from MisaLiu/feat_msg_format

增加对 `event.message_format` 和 CQ 码(仅接收)的支持
This commit is contained in:
linyuchen 2024-02-24 17:47:44 +08:00 committed by GitHub
commit 6e61621f44
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 57 additions and 7 deletions

@ -4,7 +4,7 @@
"name": "LLOneBot", "name": "LLOneBot",
"slug": "LLOneBot", "slug": "LLOneBot",
"description": "LiteLoaderQQNT的OneBotApi", "description": "LiteLoaderQQNT的OneBotApi",
"version": "3.6.0", "version": "3.7.0",
"thumbnail": "./icon.png", "thumbnail": "./icon.png",
"authors": [ "authors": [
{ {

@ -35,6 +35,7 @@ export class ConfigUtil {
ob11: ob11Default, ob11: ob11Default,
heartInterval: 60000, heartInterval: 60000,
token: "", token: "",
messagePostFormat: "array",
enableLocalFile2Url: false, enableLocalFile2Url: false,
debug: false, debug: false,
log: false, log: false,

@ -86,6 +86,6 @@ export function getUidByUin(uin: string) {
} }
} }
export const version = "3.6.0" export const version = "3.7.0"
export let groupNotifies: Map<string, GroupNotify> = new Map(); export let groupNotifies: Map<string, GroupNotify> = new Map();

@ -13,6 +13,7 @@ export interface Config {
ob11: OB11Config ob11: OB11Config
token?: string token?: string
heartInterval?: number // ms heartInterval?: number // ms
messagePostFormat?: 'array' | 'string'
enableLocalFile2Url?: boolean // 开启后本地文件路径图片会转成http链接, 语音会转成base64 enableLocalFile2Url?: boolean // 开启后本地文件路径图片会转成http链接, 语音会转成base64
debug?: boolean debug?: boolean
reportSelfMessage?: boolean reportSelfMessage?: boolean

7
src/global.d.ts vendored

@ -7,4 +7,11 @@ declare global {
llonebot: typeof llonebot; llonebot: typeof llonebot;
LiteLoader: any; LiteLoader: any;
} }
interface Event {
detail?: {
name: string;
value: string;
}
}
} }

@ -27,7 +27,7 @@ import {EventType} from "./event/OB11BaseEvent";
export class OB11Constructor { export class OB11Constructor {
static async message(msg: RawMessage): Promise<OB11Message> { static async message(msg: RawMessage): Promise<OB11Message> {
const {enableLocalFile2Url} = getConfigUtil().getConfig() const {enableLocalFile2Url, messagePostFormat} = getConfigUtil().getConfig()
const message_type = msg.chatType == ChatType.group ? "group" : "private"; const message_type = msg.chatType == ChatType.group ? "group" : "private";
const resMsg: OB11Message = { const resMsg: OB11Message = {
self_id: parseInt(selfInfo.uin), self_id: parseInt(selfInfo.uin),
@ -44,7 +44,8 @@ export class OB11Constructor {
raw_message: "", raw_message: "",
font: 14, font: 14,
sub_type: "friend", sub_type: "friend",
message: [], message: messagePostFormat === 'string' ? '' : [],
message_format: messagePostFormat === 'string' ? 'string' : 'array',
post_type: selfInfo.uin == msg.senderUin ? EventType.MESSAGE_SENT : EventType.MESSAGE, post_type: selfInfo.uin == msg.senderUin ? EventType.MESSAGE_SENT : EventType.MESSAGE,
} }
if (msg.chatType == ChatType.group) { if (msg.chatType == ChatType.group) {
@ -153,8 +154,10 @@ export class OB11Constructor {
} }
} }
} }
if (message_data.type !== "unknown" && message_data.data) { if (message_data.type !== "unknown" && message_data.data) {
resMsg.message.push(message_data); if (messagePostFormat === 'string') (resMsg.message as string) += CQCodeBuilder(message_data);
else (resMsg.message as OB11MessageData[]).push(message_data);
} }
} }
resMsg.raw_message = resMsg.raw_message.trim(); resMsg.raw_message = resMsg.raw_message.trim();
@ -217,3 +220,24 @@ export class OB11Constructor {
return groups.map(OB11Constructor.group) return groups.map(OB11Constructor.group)
} }
} }
function CQCodeBuilder(data: OB11MessageData) {
const CQCodeEscape = (text: string) => {
return text.replace(/\[/g, '&#91;')
.replace(/\]/g, '&#93;')
.replace(/\&/g, '&amp;')
.replace(/,/g, '&#44;');
};
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;
}

@ -65,7 +65,8 @@ export interface OB11Message {
message_type: "private" | "group", message_type: "private" | "group",
sub_type?: "friend" | "group" | "normal", sub_type?: "friend" | "group" | "normal",
sender: OB11Sender, sender: OB11Sender,
message: OB11MessageData[], message: OB11MessageData[] | string,
message_format: 'array' | 'string',
raw_message: string, raw_message: string,
font: number, font: number,
post_type?: EventType, post_type?: EventType,

@ -4,6 +4,7 @@
// 打开设置界面时触发 // 打开设置界面时触发
async function onSettingWindowCreated(view: Element) { async function onSettingWindowCreated(view: Element) {
window.llonebot.log("setting window created"); window.llonebot.log("setting window created");
const isEmpty = (value: any) => value === undefined || value === null || value === '';
let config = await window.llonebot.getConfig() let config = await window.llonebot.getConfig()
const httpClass = "http"; const httpClass = "http";
const httpPostClass = "http-post"; const httpPostClass = "http-post";
@ -106,6 +107,16 @@ async function onSettingWindowCreated(view: Element) {
</setting-list> </setting-list>
</setting-panel> </setting-panel>
<setting-panel> <setting-panel>
<setting-item data-direction="row" class="hostItem vertical-list-item">
<div>
<setting-text></setting-text>
<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-select>
</setting-item>
<setting-item data-direction="row" class="hostItem vertical-list-item"> <setting-item data-direction="row" class="hostItem vertical-list-item">
<div> <div>
<div></div> <div></div>
@ -174,6 +185,11 @@ async function onSettingWindowCreated(view: Element) {
doc.getElementById("addHttpHost").addEventListener("click", () => addHostEle("http")) doc.getElementById("addHttpHost").addEventListener("click", () => addHostEle("http"))
doc.getElementById("addWsHost").addEventListener("click", () => addHostEle("ws")) 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);
})
function switchClick(eleId: string, configKey: string, _config=null) { function switchClick(eleId: string, configKey: string, _config=null) {
if (!_config){ if (!_config){