Skip to content

Commit

Permalink
fix: 修正c2c下没去掉at
Browse files Browse the repository at this point in the history
  • Loading branch information
sj817 committed Aug 17, 2024
1 parent f72b468 commit 117f223
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 24 deletions.
10 changes: 5 additions & 5 deletions src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,11 +213,11 @@ export class AdapterQQBot implements KarinAdapter {
const Match = v.match(/faceId="(\d+)"/) as RegExpMatchArray
elements.push(segment.face(Number(Match[1])))
} else {
const BotConfig = Config.getBotConfig(this.account.uid) || { regex: [] }
for (const reg of BotConfig.regex) {
if (typeof v === 'string') {
v = v.trim().replace(new RegExp(reg.reg), reg.rep)
}
const { regex } = Config.getBotConfig(this.account.uid) || { regex: [] }
for (const r of regex) {
const reg = r.reg
if (typeof reg !== 'string') continue
v = v.replace(reg, r.rep)
}
elements.push(segment.text(v))
}
Expand Down
17 changes: 15 additions & 2 deletions src/core/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
common as karinCommon,
ButtonType,
TplMarkdownElement,
segment,
} from 'node-karin'

/**
Expand Down Expand Up @@ -52,8 +53,9 @@ export async function markdownTemplate (Opt: {
const textFn = async () => {
text.sort((a, b) => a.index - b.index)
const data = Common.getQQBotButton(text.map(v => v.data.text).join(''))
const val = type === PathType.Friends ? Common.formatText(data.text, true) : Common.formatText(data.text)
/** 去除at全体 */
params.push({ key: bot.markdown.oldTemplate.textStartKey, values: [data.text.replace(/everyone/g, '').replace(/\n/g, '\r')] })
params.push({ key: bot.markdown.oldTemplate.textStartKey, values: [val] })
/** 按钮 */
buttons.push(...data.data)
}
Expand All @@ -79,6 +81,11 @@ export async function markdownTemplate (Opt: {
case 'image':
image.push({ index, data })
break
case 'at': {
if (type === PathType.Friends) break
text.push({ index, data: segment.text(`<qqbot-at-user id="${data.uid || data.uin}" />`) })
break
}
case 'record': {
const file = await common.voiceToSilk(data.file)
list.push(mediaFn(file, type, '0.silk', FileType.Record))
Expand Down Expand Up @@ -222,12 +229,18 @@ export async function markdownRaw (Opt: {
/** 先提取掉url 随后放出按钮中 */
const res = Common.getQQBotButton(data.text)
buttons.push(...res.data)
message.push({ index, data: res.text.replace(/everyone/g, '').replace(/\n/g, '\r') })
const text = type === PathType.Friends ? Common.formatText(res.text, true) : Common.formatText(res.text)
message.push({ index, data: text })
break
}
case 'image':
imageFn(index, data)
break
case 'at': {
if (type === PathType.Friends) break
message.push({ index, data: `<qqbot-at-user id="${data.uid || data.uin}" />` })
break
}
case 'record': {
const file = await common.voiceToSilk(data.file)
list.push(mediaFn(file, type, '0.silk', FileType.Record))
Expand Down
12 changes: 12 additions & 0 deletions src/utils/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,18 @@ class Common {
data,
}
}

/**
* 传入文本 将文本一些特殊字符转义去除 用于处理markdown
* @param text - 文本
* @param isC2C - 是否是C2C
* @returns 处理后的文本
*/
formatText (text: string, isC2C = false) {
text = text.replace(/everyone/g, '').replace(/\n/g, '\r')
if (isC2C) text = text.replace(/<qqbot-at-user id=".+" \/>/gm, '').replace(/<@.+>/gm, '')
return text
}
}

export const common = new Common()
34 changes: 17 additions & 17 deletions src/utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export interface GlobalCfgType {
/** 接受到消息后对文本进行表达式处理,优先级高于全局配置 */
regex: Array<{
/** 匹配的正则表达式 */
reg: string
reg: RegExp

/** 替换为的内容 */
rep: string
Expand Down Expand Up @@ -217,12 +217,26 @@ class Cfg {
/** 全局默认配置 */
const defCfg = allCfg.default

return {
/** 对regex进行处理 */
const regex = Array.isArray(botCfg.regex) ? botCfg.regex : defCfg.regex
/** 对表达式进行new RegExp处理 */
const regexData = regex.map(({ reg, rep }) => {
return {
reg: reg instanceof RegExp ? reg : new RegExp(reg),
rep,
}
})

const data = {
...defCfg,
...botCfg,
sendMode: Number(botCfg.sendMode) as SendMode || defCfg.sendMode,
regex: Array.isArray(botCfg.regex) ? botCfg.regex : defCfg.regex,
regex: regexData,
}

/** 缓存 */
this.change.set(key, data)
return data
}

/**
Expand Down Expand Up @@ -264,20 +278,6 @@ class Cfg {
watcher.on('change', () => {
this.change.delete(key)
logger.mark(`[修改配置文件][${type}][${name}]`)

/** 文件修改后调用对应的方法 请自行使用 */

// switch (`change_${name}`) {
// case 'change_App':
// this.change_App()
// break
// case 'change_config':
// this.change_config()
// break
// case 'change_group':
// this.change_group()
// break
// }
})

/** 缓存 防止重复监听 */
Expand Down

0 comments on commit 117f223

Please sign in to comment.