UNPKG

xinc

Version:

基于napcat,node-napcat-ts的bot框架

63 lines 2.02 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CQ_TAG_REGEXP = exports.SPLIT = exports.logger = void 0; exports.convertCQCodeToJSON = convertCQCodeToJSON; exports.convertJSONToCQCode = convertJSONToCQCode; exports.CQCodeDecode = CQCodeDecode; exports.CQCodeEncode = CQCodeEncode; exports.logger = { warn: (...args) => { console.warn(`[${getTime()}]`, ...args); }, debug: (...args) => { console.debug(`[${getTime()}]`, ...args); }, dir: (json) => { console.dir(json, { depth: null }); }, }; const getTime = () => new Date().toLocaleString(); exports.SPLIT = /(?=\[CQ:)|(?<=])/; exports.CQ_TAG_REGEXP = /^\[CQ:([a-z]+)(?:,([^\]]+))?]$/; /** * CQ码转JSON */ function convertCQCodeToJSON(msg) { return CQCodeDecode(msg) .split(exports.SPLIT) .map((tagStr) => { const match = exports.CQ_TAG_REGEXP.exec(tagStr); if (match === null) return { type: 'text', data: { text: tagStr } }; const [, tagName, value] = match; if (value === undefined) return { type: tagName, data: {} }; const data = Object.fromEntries(value.split(',').map((item) => item.split('='))); return { type: tagName, data }; }); } const _conver = (json) => { if (json.type === 'text') return json.data.text; return `[CQ:${json.type}${Object.entries(json.data) .map(([k, v]) => (v ? `,${k}=${v}` : '')) .join('')}]`; }; /** * JSON转CQ码 */ function convertJSONToCQCode(json) { if (Array.isArray(json)) { return json.map((item) => _conver(item)).join(''); } else { return _conver(json); } } function CQCodeDecode(str) { return str.replace(/&#44;/g, ',').replace(/&#91;/g, '[').replace(/&#93;/g, ']').replace(/&amp;/g, '&'); } function CQCodeEncode(str) { return str.replace(/,/g, '&#44;').replace(/\[/g, '&#91;').replace(/]/g, '&#93;').replace(/&/g, '&amp;'); } //# sourceMappingURL=Utils.js.map