Skip to content

Commit

Permalink
refactor(shell): logger: update
Browse files Browse the repository at this point in the history
  • Loading branch information
ilharp committed Aug 23, 2024
1 parent 803692f commit 550f380
Show file tree
Hide file tree
Showing 3 changed files with 171 additions and 10 deletions.
20 changes: 17 additions & 3 deletions packages/shell/src/services/logger/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,23 @@ import { mkdir, open, rm, writeFile } from 'node:fs/promises'
import { join } from 'node:path'
import { env, platform } from 'node:process'
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 { formatTimeforFilename, p2, p4 } from '../../utils/string'
import { timeout } from '../../utils/time'
import { getAuthData } from '../authData'
import { baseDir } from '../baseDir'
import { getConfig } from '../config'
import { logiriMessageCreated } from './logiri'
import type { ColorFormatter } from '../../utils/colors'
import { grey, red, yellow } from '../../utils/colors'
import {
logiriFriendRequest,
logiriGuildMemberAdded,
logiriGuildRequest,
logiriMessageCreated,
logiriMessageDeleted,
logiriUnsafeGuildMute,
logiriUnsafeGuildUnmute,
} from './logiri'

interface LogOptions {
code?: number
Expand Down Expand Up @@ -43,6 +51,12 @@ class ChronocatLogger {
constructor() {
this.logiri = new Logiri()
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 () => {
const dir = join(baseDir, 'logs')
Expand Down
113 changes: 107 additions & 6 deletions packages/shell/src/services/logger/logiri.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { link } from 'logiri'
import type { Event } from '../../satori/types'
import { ChannelType } from '../../satori/types'
import { blue, cyan, grey } from '../../utils/colors'
import { send } from './messager'

Expand All @@ -10,18 +11,118 @@ export const logiriMessageCreated = async (data: object) => {
const message = rawMessage.join('').replace(/\r/g, '').replace(/\n/g, ' ')
return [message].map(
(x) =>
`${blue(
`${
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.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.user?.name || d.member?.nick}(${d.user?.id})`,
d.user?.avatar,
),
)}${grey('-')}${cyan(
)}${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('解除禁言')}`,
]
}
48 changes: 47 additions & 1 deletion packages/shell/src/services/logger/messager.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import h from '@satorijs/element'
import { link } from 'logiri'
import { grey } from '../../utils/colors'
import { PLATFORM } from '../../utils/consts'

type DisplayComponent = string

Expand Down Expand Up @@ -40,6 +41,10 @@ const visit = async (element: h): Promise<DisplayComponent[] | false> => {
return [link('[语音]', attrs['src'] as string)]
}

case 'video': {
return [link('[视频]', attrs['src'] as string)]
}

case 'file': {
return [link('[文件]', attrs['src'] as string)]
}
Expand All @@ -65,10 +70,51 @@ const visit = async (element: h): Promise<DisplayComponent[] | false> => {
]
}

case 'chronocat:poke': {
case `${PLATFORM}:poke`: {
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': {
if ('forward' in attrs) {
if ('id' in attrs) {
Expand Down

0 comments on commit 550f380

Please sign in to comment.