Skip to content

Commit

Permalink
4.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
idranme committed Jul 23, 2024
1 parent 5fa6ea5 commit 38e2912
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 110 deletions.
4 changes: 0 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@

### 设定适配器

#### QQ

参见 Koishi 论坛的 [810](https://forum.koishi.xyz/t/topic/810) 帖。

#### Telegram

1. 添加好友 @BotFather,与其交互,按照屏幕提示进行操作,建立一个机器人账号。设定完成后,BotFather 会给一个 Token。
Expand Down
11 changes: 3 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@myrtus/koishi-plugin-forward",
"description": "Forward messages",
"version": "4.0.1",
"version": "4.1.0",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
"files": [
Expand All @@ -19,16 +19,11 @@
"transfer"
],
"peerDependencies": {
"koishi": "^4.16.4"
"koishi": "^4.17.7"
},
"repository": {
"type": "git",
"url": "https://github.com/bot-myrtus/forward.git"
},
"author": {
"email": "[email protected]",
"name": "idanran",
"url": "https://github.com/idanran"
"url": "git+https://github.com/bot-myrtus/forward.git"
},
"koishi": {
"description": {
Expand Down
4 changes: 2 additions & 2 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Schema, Dict, Time } from 'koishi'

interface Source {
channelId: string
name: string
name?: string
platform: string
blockingWords: string[]
selfId: string
Expand Down Expand Up @@ -83,7 +83,7 @@ export const Config: Schema<Config> = Schema.intersect([
targetConst,
fullConst
])
]))
])).collapse(true)
}),
Schema.object({
rules: Schema.array(Schema.object({
Expand Down
78 changes: 45 additions & 33 deletions src/core.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Context, h, sleep, Keys, Universal, isNullable } from 'koishi'
import { MessageParse } from './parse'
import { Context, h, Universal } from 'koishi'
import { RuleSource, RuleTarget, Config } from './config'

declare module 'koishi' {
Expand All @@ -19,6 +18,22 @@ interface Sent {
id?: number
}

function transform(source: h[]): h[] {
return h.transform(source, {
at(attrs) {
const name = attrs.name || attrs.id
return h.text(`@${name}`)
},
face(attrs) {
const name = attrs.name || '表情'
return h.text(`[${name}]`)
},
audio(attrs) {
return h.text('[语音]')
}
})
}

export function apply(ctx: Context, config: Config) {
ctx.model.extend('myrtus_forward_sent', {
id: 'unsigned',
Expand All @@ -33,12 +48,12 @@ export function apply(ctx: Context, config: Config) {
autoInc: true
})

const logger = ctx.logger('forward')
const { logger } = ctx

for (const rule of config.rules) {
const sConfig = config.constants[rule.source] as RuleSource
if (!sConfig) continue
const targetConfigs: Array<RuleTarget> = []
const targetConfigs: RuleTarget[] = []
for (const target of rule.targets) {
const targetConfig = config.constants[target] as RuleTarget
if (targetConfig && !targetConfig.disabled) {
Expand All @@ -57,17 +72,17 @@ export function apply(ctx: Context, config: Config) {
const { event, sid } = session

for (const regexpStr of sConfig.blockingWords) {
const hit = event.message.elements.some(value => {
if (value.type === 'text') {
return new RegExp(regexpStr).test(value.attrs.content)
const reg = new RegExp(regexpStr)
const hit = session.elements.some(v => {
if (v.type === 'text') {
return reg.test(v.attrs.content)
}
return false
})

if (hit) return
}

let rows: Pick<Sent, Keys<Sent>>[] = []
let rows: Sent[] = []
const { quote } = event.message
if (quote) {
if (event.selfId === quote.user.id) {
Expand All @@ -85,46 +100,44 @@ export function apply(ctx: Context, config: Config) {
}
logger.debug('%C', '=== inspect quote ===')
logger.debug(`from sid: ${sid}`)
logger.debug(rows)
}

const filtered: h[] = new MessageParse(event.message.elements).face().record().at().output()

const filtered = transform(event.message.elements)
const sent: Sent[] = []

for (let index = 0; index < targetConfigs.length; index++) {
const target = targetConfigs[index]
const targetSid = `${target.platform}:${target.selfId}`
const bot = ctx.bots[targetSid]

const name = event.member?.nick || event.user.nick || event.user.name
if (!bot) {
logger.warn(`暂时找不到机器人实例 %c, 等待一会儿说不定就有了呢!`, targetSid)
continue
}
if (bot.status !== Universal.Status.ONLINE) {
logger.warn(`机器人实例 %c 处于非在线状态,可能与网络环境有关。`, targetSid)
continue
}

let prefix: h
if (target.simulateOriginal && target.platform === 'discord') {
let avatar = event.user.avatar
if (event.platform === 'telegram') {
avatar = 'https://discord.com/assets/5d6a5e9d7d77ac29116e.png'
}
prefix = h('author', {
name: `[${sConfig.name}] ${name}`,
name: `[${sConfig.name ?? ''}] ${session.username}`,
avatar
})
} else {
const altName = isNullable(sConfig.name) ? '' : `${sConfig.name} - `
prefix = h.text(`[${altName}${name}]\n`)
}
const payload: h[] = [prefix, ...filtered]

if (!bot) {
logger.warn(`暂时找不到机器人实例 %c, 等待一会儿说不定就有了呢!`, targetSid)
continue
}
if (bot.status !== Universal.Status.ONLINE) {
logger.warn(`机器人实例 %c 处于非在线状态,可能与网络环境有关。`, targetSid)
continue
const altName = sConfig.name ? `${sConfig.name} - ` : ''
prefix = h.text(`[${altName}${session.username}]\n`)
}

const delay = config.delay[target.platform] ?? 200
if (index) await sleep(delay)
if (index) await ctx.sleep(delay)

const payload: h[] = [prefix, ...filtered]
if (event.message.quote) {
let quoteId: string | undefined
if (event.selfId === event.message.quote.user.id) {
Expand All @@ -149,18 +162,17 @@ export function apply(ctx: Context, config: Config) {
payload.unshift(h.quote(quoteId))
}
logger.debug(`msgId: ${quoteId}`)
logger.debug(`added`)
logger.debug(`quote added`)
} else {
const { user, elements } = event.message.quote
const re: h[] = [h.text(`Re ${user.nick || user.name} ⌈`), ...(elements || []), h.text('⌋\n')]
payload.unshift(...new MessageParse(re).face().record().at().output())
logger.debug('not added')
const { user, elements = [], member } = event.message.quote
const name = member?.nick || user.nick || user.name
payload.unshift(h.text(`Re ${name} ⌈`), ...transform(elements), h.text('⌋\n'))
logger.debug('quote not added')
}
logger.debug(`to sid: ${targetSid}`)
}

try {
logger.debug(payload)
const messageIds = await bot.sendMessage(target.channelId, payload)
for (const msgId of messageIds) {
sent.push({
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
export * from './config'

export const name = 'forward'

export const inject = ['database']
Expand All @@ -10,4 +8,6 @@ export const usage = `
入门教程<a href="https://github.com/bot-myrtus/forward?tab=readme-ov-file#%E8%AE%BE%E5%AE%9A%E6%B6%88%E6%81%AF%E8%BD%AC%E5%8F%91%E8%A7%84%E5%88%99" target="_blank">点此查阅</a>,如有疑问可<a href="https://github.com/bot-myrtus/forward/issues/new" target="_blank">提交 issue</a>。
`

export * from './config'

export * from './core'
6 changes: 3 additions & 3 deletions src/locales/zh-CN.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
- type:
$description: 常量类型
$value:
- 仅用于「来源」
- 仅用于「目标」
- 用于「来源」或「目标」(通常用以双向转发)
- 仅适用「来源」
- 仅适用「目标」
- 适用「来源」或「目标」(通常用以双向转发)
- - name: 来源代称 (可随意填写)
blockingWords:
$description: 屏蔽词 (消息包含屏蔽词时不转发, 支持正则表达式)
Expand Down
54 changes: 0 additions & 54 deletions src/parse.ts

This file was deleted.

13 changes: 9 additions & 4 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,22 @@
"rootDir": "src",
"outDir": "lib",
"target": "es2022",
"module": "commonjs",
"module": "esnext",
"declaration": true,
"emitDeclarationOnly": true,
"composite": true,
"incremental": true,
"skipLibCheck": true,
"esModuleInterop": true,
"moduleResolution": "node",
"moduleResolution": "bundler",
"jsx": "react-jsx",
"jsxImportSource": "@satorijs/element",
"types": [
"node",
"yml-register/types"
]
},
"include": [
"src",
],
"src"
]
}

0 comments on commit 38e2912

Please sign in to comment.