Skip to content

Commit

Permalink
⚡ feat:node . --dev
Browse files Browse the repository at this point in the history
  • Loading branch information
sj817 committed Apr 8, 2024
1 parent 5f58783 commit f2099fa
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 17 deletions.
13 changes: 11 additions & 2 deletions lib/adapter/kritor/converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import {
KarinFaceElement,
KarinGroupRecallNotice,
KarinImageElement,
KarinTextElement
KarinTextElement,
KarinReplyElement
} from '../../bot/model.js'
import { logger, Bot, kritor } from '#Karin'
import { KarinMessage } from '../../event/type.js'
Expand Down Expand Up @@ -75,6 +76,14 @@ export class MessageConverter extends Converter {
elements.push(new KarinFaceElement(i.face.id, i.face.is_big))
break
}
case kritor.common.Element.ElementType.FILE: {
elements.push(new KarinFileElement(i.file.file_url))
break
}
case kritor.common.Element.ElementType.REPLY: {
elements.push(new KarinReplyElement(i.file.file_url))
break
}
// todo 其他类型
default: {
logger.info(i)
Expand Down Expand Up @@ -162,7 +171,7 @@ export class NoticeConverter extends Converter {
case kritor.event.NoticeEvent.NoticeType.GROUP_RECALL: {
const e = new KarinGroupRecallNotice({ self_id, time: data.time, content: data.group_recall })
e.logText = `[群撤回:${data.group_recall.group_id}] operator: ${data.group_recall.operator_uin}, target: ${data.group_recall.target_uin}, message_id: ${data.group_recall.message_id}`
e.raw_message = `<group_recall operator_uin=${e.sender.operator_uin} target_uin=${e.sender.target_uin}>`
e.raw_message = `<group_recall operator_uin=${e.sender.operator_uin} operator_uid=${e.sender.operator_uid} target_uin=${e.sender.target_uin} target_uid=${e.sender.target_uid} message_id=${e.message_id}>`
return e
}
/** 群成员增加 */
Expand Down
5 changes: 1 addition & 4 deletions lib/adapter/kritor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,7 @@ export default class Kritor {
/** 通知事件 */
case kritor.event.EventType.EVENT_TYPE_NOTICE: {
let notice = Converters.noticeConverter.convert(data.notice, uin || uid)
if (notice) {
logger.info(notice.raw_message)
Bot.emit('notice', notice)
}
Bot.emit('notice', notice)
break
}
/** 请求事件 */
Expand Down
69 changes: 65 additions & 4 deletions lib/bot/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,15 +298,76 @@ export function Contacts (scene, peer, sub_peer) {

/**
* 发送者
* @param {string} operator_uid - 操作者uid
* @param {string} operator_uin - 操作者uin
* @param {string} target_uid - 目标者uid
* @param {string} target_uin - 目标者uin
* @returns {Object}
*/
export function Senders (operator_uid = '', operator_uin = '', target_uid = '', target_uin = '') {
return { operator_uid, operator_uin, target_uid, target_uin }
}

/**
* 好友头像戳一戳
*/
export class KarinFriendPokeNotice extends KarinNotice {
type = 'friend_poke'

/**
* @param {{
* selfId: string,
* content: {
* operator_uid: string,
* operator_uin: String,
* action: String,
* suffix: String,
* action_image: String
* },
* time: Number
* }} params
*/
constructor ({ self_id, content, time }) {
const { operator_uid = '', operator_uin = '', target_uid = '', target_uin = '', message_id, message_seq, group_id = '', raw_message = '' } = content
/** 构建联系人 */
const contact = Contacts('friend', operator_uin, operator_uin)
/** 构建发送者信息 */
const sender = Senders(operator_uid, operator_uin, target_uid, target_uin)
/** 构建通知 */
const data = {
self_id,
type: 'friend_poke',
time,
contact,
sender,
content,
// user_id与peer统一使用uin
user_id: operator_uin,
message_id,
message_seq,
raw_message,
group_id
}
super(data)
}

/**
* @type {{
* group_id: Number,
* message_id: String,
* tip_text: String,
* operator_uid: String,
* operator_uin: Number,
* target_uid: String,
* target_uin: Number,
* message_seq: Number
* }}
*/
content
}

/**
* 群撤回通知
* @todo 统一化notice类型,和filterEvent适配
*/
export class KarinGroupRecallNotice extends KarinNotice {
type = 'group_recall'
Expand All @@ -328,9 +389,9 @@ export class KarinGroupRecallNotice extends KarinNotice {
* }} params
*/
constructor ({ self_id, content, time }) {
const { operator_uid, operator_uin, target_uid, target_uin, message_id, message_seq, group_id = '', raw_message = '' } = content
const { operator_uid = '', operator_uin = '', target_uid = '', target_uin = '', message_id, message_seq, group_id = '', raw_message = '' } = content
/** 构建联系人 */
const contact = Contacts('group', group_id, operator_uid)
const contact = Contacts('group', group_id, operator_uin)
/** 构建发送者信息 */
const sender = Senders(operator_uid, operator_uin, target_uid, target_uin)
/** 构建通知 */
Expand All @@ -342,7 +403,7 @@ export class KarinGroupRecallNotice extends KarinNotice {
sender,
content,
// user_id与peer统一使用uin
user_id: operator_uid,
user_id: operator_uin,
message_id,
message_seq,
raw_message,
Expand Down
15 changes: 12 additions & 3 deletions lib/config/log.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,22 @@ import cfg from './config.js'
let logsDir = './logs'
if (!fs.existsSync(logsDir)) fs.mkdirSync(logsDir)

let enableCallStack = false
let pattern = '%[[Karin][%d{hh:mm:ss.SSS}][%4.4p]%] %m'

/** 开发者模式 */
if (process.argv[2]?.includes('dev')) {
enableCallStack = true
pattern = '%[[Karin][%d{hh:mm:ss.SSS}][%4.4p]%] [%f{3}:%l] %m'
}

log4js.configure({
appenders: {
console: {
type: 'console', // 输出到控制台
layout: {
type: 'pattern',
pattern: '%[[Karin][%d{hh:mm:ss.SSS}][%4.4p]%] %m'
pattern
}
},
out: {
Expand Down Expand Up @@ -42,8 +51,8 @@ log4js.configure({
}
},
categories: {
default: { appenders: ['out', 'console'], level: cfg.Config.log_level },
error: { appenders: ['errorFile', 'console'], level: 'error' }
default: { appenders: ['out', 'console'], level: cfg.Config.log_level, enableCallStack },
error: { appenders: ['errorFile', 'console'], level: 'error', enableCallStack }
}
})

Expand Down
20 changes: 16 additions & 4 deletions lib/event/notice.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import lodash from 'lodash'
import util from 'node:util'
import Event from './event.js'
import loader from '../plugins/loader.js'
import { common, logger } from '#Karin'
import { common, logger, Cfg } from '#Karin'

/** 通知、请求事件 */
class Notice extends Event {
Expand All @@ -16,6 +16,20 @@ class Notice extends Event {
/** 处理回复 */
this.reply(e)

/** 主人 */
if (Cfg.master.includes(Number(e.user_id) || String(e.user_id))) e.isMaster = true

if (e.contact.scene === 'friend') {
e.isPrivate = true
e.logText = `[Private:${e.sender.nick || ''}(${e.user_id})]`
logger.info(common.logger(e.self_id, `私聊通知事件:[${e.user_id}(${e.sender.nick || ''})] ${e.raw_message}`))
} else if (e.contact.scene === 'group') {
e.isGroup = true
e.logText = `[Group:${e.group_id}-${e.user_id}(${e.sender.nick || ''})]`
logger.info(common.logger(e.self_id, `群通知事件:[${e.group_id}-${e.user_id}(${e.sender.nick || ''})] ${e.raw_message}`))
} else {
logger.info(common.logger(e.self_id, `未知来源通知事件:${JSON.stringify(e)}`))
}
/* eslint-disable no-labels */
a:
for (const app of loader.priority) {
Expand Down Expand Up @@ -45,9 +59,7 @@ class Notice extends Event {
if (util.types.isPromise(res)) res = await res

if (res !== false) {
if (app.log !== false) {
logger.info(common.logger(e.self_id, `${e.logFnc} ${lodash.truncate(e.msg, { length: 80 })} 处理完成 ${Date.now() - start}ms`))
}
logger.info(common.logger(e.self_id, `${e.logFnc} ${lodash.truncate(e.msg, { length: 80 })} 处理完成 ${Date.now() - start}ms`))
break a
}
} catch (error) {
Expand Down

0 comments on commit f2099fa

Please sign in to comment.