mirror of
https://github.com/chrononeko/chronocat.git
synced 2024-11-25 18:03:40 +00:00
refactor(shell): logger: functional messager
This commit is contained in:
parent
14729260de
commit
803692ffb1
@ -1,12 +1,12 @@
|
|||||||
import { link } from 'logiri'
|
import { link } from 'logiri'
|
||||||
import { LogiriMessager } from './messager'
|
|
||||||
import type { Event } from '../../satori/types'
|
import type { Event } from '../../satori/types'
|
||||||
import { blue, cyan, grey } from '../../utils/colors'
|
import { blue, cyan, grey } from '../../utils/colors'
|
||||||
|
import { send } from './messager'
|
||||||
|
|
||||||
export const logiriMessageCreated = async (data: object) => {
|
export const logiriMessageCreated = async (data: object) => {
|
||||||
const d = data as Event
|
const d = data as Event
|
||||||
if (d.type !== 'message-created') return
|
if (d.type !== 'message-created') return
|
||||||
const rawMessage = await new LogiriMessager().send(d.message?.content)
|
const rawMessage = await send(d.message?.content)
|
||||||
const message = rawMessage.join('').replace(/\r/g, '').replace(/\n/g, ' ')
|
const message = rawMessage.join('').replace(/\r/g, '').replace(/\n/g, ' ')
|
||||||
return [message].map(
|
return [message].map(
|
||||||
(x) =>
|
(x) =>
|
||||||
|
@ -4,93 +4,91 @@ import { grey } from '../../utils/colors'
|
|||||||
|
|
||||||
type DisplayComponent = string
|
type DisplayComponent = string
|
||||||
|
|
||||||
export class LogiriMessager {
|
// const prepare = async () => {}
|
||||||
prepare = async () => {}
|
|
||||||
|
|
||||||
render = async (elements: h[]): Promise<DisplayComponent[] | false> => {
|
const render = async (elements: h[]): Promise<DisplayComponent[] | false> => {
|
||||||
if (!elements.length) return ['空消息']
|
if (!elements.length) return ['空消息']
|
||||||
const result = await Promise.all(elements.map(this.visit))
|
const result = await Promise.all(elements.map(visit))
|
||||||
if (result.every((x) => x === false)) return false
|
if (result.every((x) => x === false)) return false
|
||||||
return result.flatMap((x) => (x === false ? ['[不支持的消息]'] : x))
|
return result.flatMap((x) => (x === false ? ['[不支持的消息]'] : x))
|
||||||
}
|
}
|
||||||
|
|
||||||
send = async (
|
export const send = async (
|
||||||
content: string | null | undefined,
|
content: string | null | undefined,
|
||||||
): Promise<DisplayComponent[]> => {
|
): Promise<DisplayComponent[]> => {
|
||||||
if (!content) return ['空消息']
|
if (!content) return ['空消息']
|
||||||
await this.prepare()
|
// await prepare()
|
||||||
const elements = h.normalize(content)
|
const elements = h.normalize(content)
|
||||||
let result = await this.render(elements)
|
let result = await render(elements)
|
||||||
if (result === false) result = ['[不支持的消息]']
|
if (result === false) result = ['[不支持的消息]']
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
visit = async (element: h): Promise<DisplayComponent[] | false> => {
|
const visit = async (element: h): Promise<DisplayComponent[] | false> => {
|
||||||
const { type, attrs, children } = element
|
const { type, attrs, children } = element
|
||||||
|
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case 'text': {
|
case 'text': {
|
||||||
return [attrs['content'] as string]
|
return [attrs['content'] as string]
|
||||||
}
|
}
|
||||||
|
|
||||||
case 'img': {
|
case 'img': {
|
||||||
return [link('[图片]', attrs['src'] as string)]
|
return [link('[图片]', attrs['src'] as string)]
|
||||||
}
|
}
|
||||||
|
|
||||||
case 'audio': {
|
case 'audio': {
|
||||||
return [link('[语音]', attrs['src'] as string)]
|
return [link('[语音]', attrs['src'] as string)]
|
||||||
}
|
}
|
||||||
|
|
||||||
case 'file': {
|
case 'file': {
|
||||||
return [link('[文件]', attrs['src'] as string)]
|
return [link('[文件]', attrs['src'] as string)]
|
||||||
}
|
}
|
||||||
|
|
||||||
case 'at': {
|
case 'at': {
|
||||||
if (attrs['type'] === 'all') return ['@全体成员 ']
|
if (attrs['type'] === 'all') return ['@全体成员 ']
|
||||||
else return [`@${attrs['name'] as string}(${attrs['id'] as string}) `]
|
else return [`@${attrs['name'] as string}(${attrs['id'] as string}) `]
|
||||||
}
|
}
|
||||||
|
|
||||||
case 'quote': {
|
case 'quote': {
|
||||||
const [author] = h.select(children, 'author')
|
const [author] = h.select(children, 'author')
|
||||||
const id = author?.attrs['id'] as string | undefined
|
const id = author?.attrs['id'] as string | undefined
|
||||||
|
|
||||||
return [
|
return [
|
||||||
grey(
|
grey(
|
||||||
id
|
id
|
||||||
? `${link(
|
? `${link(
|
||||||
`[回复${id}]`,
|
`[回复${id}]`,
|
||||||
`http://thirdqq.qlogo.cn/headimg_dl?dst_uin=${id}&spec=640`,
|
`http://thirdqq.qlogo.cn/headimg_dl?dst_uin=${id}&spec=640`,
|
||||||
)} `
|
)} `
|
||||||
: `[回复] `,
|
: `[回复] `,
|
||||||
),
|
),
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
case 'chronocat:poke': {
|
case 'chronocat:poke': {
|
||||||
return ['[戳一戳]']
|
return ['[戳一戳]']
|
||||||
}
|
}
|
||||||
|
|
||||||
case 'message': {
|
case 'message': {
|
||||||
if ('forward' in attrs) {
|
if ('forward' in attrs) {
|
||||||
if ('id' in attrs) {
|
if ('id' in attrs) {
|
||||||
return ['[单条转发消息]']
|
return ['[单条转发消息]']
|
||||||
} else if (children.every((x) => 'id' in x)) {
|
} else if (children.every((x) => 'id' in x)) {
|
||||||
return ['[普通合并转发消息]']
|
return ['[普通合并转发消息]']
|
||||||
} else {
|
|
||||||
return ['[伪造合并转发消息]']
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
// 普通切割消息
|
return ['[伪造合并转发消息]']
|
||||||
const result = await this.render(children)
|
|
||||||
if (result) return ['[切割消息]', ...result]
|
|
||||||
else return ['[切割消息]']
|
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
// 普通切割消息
|
||||||
|
const result = await render(children)
|
||||||
|
if (result) return ['[切割消息]', ...result]
|
||||||
|
else return ['[切割消息]']
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
default: {
|
default: {
|
||||||
// 兜底
|
// 兜底
|
||||||
return await this.render(children)
|
return await render(children)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user