refactor(shell): logger: update

This commit is contained in:
Il Harper 2024-08-23 15:04:09 +08:00
parent 803692ffb1
commit 550f38047d
No known key found for this signature in database
GPG Key ID: 4B71FCA698E7E8EC
3 changed files with 173 additions and 12 deletions

View File

@ -5,15 +5,23 @@ import { mkdir, open, rm, writeFile } from 'node:fs/promises'
import { join } from 'node:path' import { join } from 'node:path'
import { env, platform } from 'node:process' import { env, platform } from 'node:process'
import type { Event } from '../../satori/types' import type { Event } from '../../satori/types'
import type { ColorFormatter } from '../../utils/colors'
import { grey, red, yellow } from '../../utils/colors'
import { exists } from '../../utils/fs' import { exists } from '../../utils/fs'
import { formatTimeforFilename, p2, p4 } from '../../utils/string' import { formatTimeforFilename, p2, p4 } from '../../utils/string'
import { timeout } from '../../utils/time' import { timeout } from '../../utils/time'
import { getAuthData } from '../authData' import { getAuthData } from '../authData'
import { baseDir } from '../baseDir' import { baseDir } from '../baseDir'
import { getConfig } from '../config' import { getConfig } from '../config'
import { logiriMessageCreated } from './logiri' import {
import type { ColorFormatter } from '../../utils/colors' logiriFriendRequest,
import { grey, red, yellow } from '../../utils/colors' logiriGuildMemberAdded,
logiriGuildRequest,
logiriMessageCreated,
logiriMessageDeleted,
logiriUnsafeGuildMute,
logiriUnsafeGuildUnmute,
} from './logiri'
interface LogOptions { interface LogOptions {
code?: number code?: number
@ -43,6 +51,12 @@ class ChronocatLogger {
constructor() { constructor() {
this.logiri = new Logiri() this.logiri = new Logiri()
this.logiri.register(logiriMessageCreated) this.logiri.register(logiriMessageCreated)
this.logiri.register(logiriMessageDeleted)
this.logiri.register(logiriGuildRequest)
this.logiri.register(logiriFriendRequest)
this.logiri.register(logiriGuildMemberAdded)
this.logiri.register(logiriUnsafeGuildMute)
this.logiri.register(logiriUnsafeGuildUnmute)
this.init = (async () => { this.init = (async () => {
const dir = join(baseDir, 'logs') const dir = join(baseDir, 'logs')

View File

@ -1,5 +1,6 @@
import { link } from 'logiri' import { link } from 'logiri'
import type { Event } from '../../satori/types' import type { Event } from '../../satori/types'
import { ChannelType } from '../../satori/types'
import { blue, cyan, grey } from '../../utils/colors' import { blue, cyan, grey } from '../../utils/colors'
import { send } from './messager' import { send } from './messager'
@ -10,14 +11,18 @@ export const logiriMessageCreated = async (data: object) => {
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) =>
`${blue( `${
link( d.channel?.type === ChannelType.DIRECT
d.channel?.id === d.guild?.id ? ''
? `${d.channel?.name}(${d.channel?.id})` : blue(
: `${d.guild?.name}(${d.guild?.id})/${d.channel?.name}(${d.channel?.id})`, link(
d.guild?.avatar, d.channel?.id === d.guild?.id
), ? `${d.channel?.name}(${d.channel?.id})`
)}${grey('-')}${cyan( : `${d.guild?.name}(${d.guild?.id})/${d.channel?.name}(${d.channel?.id})`,
d.guild?.avatar,
),
)
}${d.channel?.type === ChannelType.DIRECT ? '' : grey('-')}${cyan(
link( link(
`${d.user?.name || d.member?.nick}(${d.user?.id})`, `${d.user?.name || d.member?.nick}(${d.user?.id})`,
d.user?.avatar, d.user?.avatar,
@ -25,3 +30,99 @@ export const logiriMessageCreated = async (data: object) => {
)}${grey(':')} ${x}`, )}${grey(':')} ${x}`,
) )
} }
export const logiriMessageDeleted = async (data: object) => {
const d = data as Event
if (d.type !== 'message-deleted') return
const rawMessage = await send(d.message?.content)
const message = rawMessage.join('').replace(/\r/g, '').replace(/\n/g, ' ')
return [message].map(
(x) =>
`${
d.channel?.type === ChannelType.DIRECT
? ''
: blue(
link(
d.channel?.id === d.guild?.id
? `${d.channel?.name}(${d.channel?.id})`
: `${d.guild?.name}(${d.guild?.id})/${d.channel?.name}(${d.channel?.id})`,
d.guild?.avatar,
),
)
}${d.channel?.type === ChannelType.DIRECT ? '' : grey('-')}${cyan(
link(
`${d.user?.name || d.member?.nick}(${d.user?.id})`,
d.user?.avatar,
),
)}${grey(':')} ${x}`,
)
}
export const logiriGuildRequest = async (data: object) => {
const d = data as Event
if (d.type !== 'guild-request') return
return [
`${grey('用户')} ${cyan(
link(`${d.user?.name || d.member?.nick}(${d.user?.id})`, d.user?.avatar),
)} ${grey('申请加入群')} ${blue(
link(`${d.guild?.name}(${d.guild?.id})`, d.guild?.avatar),
)}`,
]
}
export const logiriFriendRequest = async (data: object) => {
const d = data as Event
if (d.type !== 'friend-request') return
return [
`${grey('用户')} ${cyan(
link(`${d.user?.name || d.member?.nick}(${d.user?.id})`, d.user?.avatar),
)} ${grey('申请添加好友')}`,
]
}
export const logiriGuildMemberAdded = async (data: object) => {
const d = data as Event
if (d.type !== 'guild-member-added') return
return [
`${grey('用户')} ${cyan(
link(`${d.user?.name || d.member?.nick}(${d.user?.id})`, d.user?.avatar),
)} ${grey('由')} ${cyan(
link(
`${d.operator?.name || d.operator?.nick}(${d.operator?.id})`,
d.operator?.avatar,
),
)} ${grey('批准/邀请加入了群')} ${blue(
link(`${d.guild?.name}(${d.guild?.id})`, d.guild?.avatar),
)}`,
]
}
export const logiriUnsafeGuildMute = async (data: object) => {
const d = data as Event
if (d.type !== 'unsafe-guild-mute') return
return [
`${grey('用户')} ${cyan(
link(`${d.user?.name || d.member?.nick}(${d.user?.id})`, d.user?.avatar),
)} ${grey('由管理')} ${cyan(
link(
`${d.operator?.name || d.operator?.nick}(${d.operator?.id})`,
d.operator?.avatar,
),
)} ${grey('禁言')}`,
]
}
export const logiriUnsafeGuildUnmute = async (data: object) => {
const d = data as Event
if (d.type !== 'unsafe-guild-unmute') return
return [
`${grey('用户')} ${cyan(
link(`${d.user?.name || d.member?.nick}(${d.user?.id})`, d.user?.avatar),
)} ${grey('由管理')} ${cyan(
link(
`${d.operator?.name || d.operator?.nick}(${d.operator?.id})`,
d.operator?.avatar,
),
)} ${grey('解除禁言')}`,
]
}

View File

@ -1,6 +1,7 @@
import h from '@satorijs/element' import h from '@satorijs/element'
import { link } from 'logiri' import { link } from 'logiri'
import { grey } from '../../utils/colors' import { grey } from '../../utils/colors'
import { PLATFORM } from '../../utils/consts'
type DisplayComponent = string type DisplayComponent = string
@ -40,6 +41,10 @@ const visit = async (element: h): Promise<DisplayComponent[] | false> => {
return [link('[语音]', attrs['src'] as string)] return [link('[语音]', attrs['src'] as string)]
} }
case 'video': {
return [link('[视频]', attrs['src'] as string)]
}
case 'file': { case 'file': {
return [link('[文件]', attrs['src'] as string)] return [link('[文件]', attrs['src'] as string)]
} }
@ -65,10 +70,51 @@ const visit = async (element: h): Promise<DisplayComponent[] | false> => {
] ]
} }
case 'chronocat:poke': { case `${PLATFORM}:poke`: {
return ['[戳一戳]'] return ['[戳一戳]']
} }
case `${PLATFORM}:pcpoke`: {
return ['[窗口抖动]']
}
case `${PLATFORM}:face`: {
let result = ''
let description = '表情'
if (attrs['unsafe-super']) description = '超级表情'
if (attrs['unsafe-market-emoticon']) description = 'Emoticon 表情'
result = link(
`[${description}]`,
h.select(children, 'img')?.[0]?.attrs['src'] as string | undefined,
)
if (attrs['unsafe-result-id'])
result += ` 掷骰结果:${attrs['unsafe-result-id']}`
if (attrs['unsafe-chain-count'])
result += ` 接龙个数:${attrs['unsafe-chain-count']}`
return [result]
}
case `${PLATFORM}:marketface`: {
return [
link(
`[商城表情]`,
h.select(children, 'img')?.[0]?.attrs['src'] as string | undefined,
),
]
}
case `${PLATFORM}:facebubble`: {
return [
link(
`[气泡表情]`,
h.select(children, 'img')?.[0]?.attrs['src'] as string | undefined,
),
]
}
case 'message': { case 'message': {
if ('forward' in attrs) { if ('forward' in attrs) {
if ('id' in attrs) { if ('id' in attrs) {