Skip to content

Commit

Permalink
fix:优化消息事件结构
Browse files Browse the repository at this point in the history
  • Loading branch information
sj817 committed Apr 10, 2024
1 parent 36915e1 commit 59afb57
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 18 deletions.
20 changes: 10 additions & 10 deletions lib/event/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,26 +87,26 @@ export default class Event {
e.replyNew = e.reply

/**
* @param {string|object|Array} msg - 发送的消息
* @param {object} data - 回复数据
* @param {string|object|Array} message - 发送的消息
* @param {object} options - 回复数据
* @param {boolean} data.at - 是否at用户
* @param {boolean} data.reply - 是否引用回复
* @param {number} data.recallMsg - 群聊是否撤回消息,0-120秒,0不撤回
* @param {boolean} data.button - 是否使用按钮
* @param {number} data.retry_count - 重试次数
*/
e.reply = async (msg = '', data = { reply: false, recallMsg: 0, at: '', button: false, retry_count: 1 }) => {
e.reply = async (message = '', options = { reply: false, recallMsg: 0, at: '', button: false, retry_count: 1 }) => {
/** 将msg格式化为数组 */
if (!Array.isArray(msg)) msg = typeof msg == 'string' ? [segment.text(msg)] : [msg]
if (!Array.isArray(message)) message = typeof message == 'string' ? [segment.text(message)] : [message]

let { reply, recallMsg, at, button, retry_count } = data
let { reply, recallMsg, at, button, retry_count } = options
logger.debug(button) // 后续处理
/** 加入at */
if (at && e.isGroup) msg.unshift(segment.at(e.user_id))
if (at && e.isGroup) message.unshift(segment.at(e.user_id))
/** 加入引用回复 */
if (reply && e.message_id) msg.unshift(segment.reply(e.message_id))
if (reply && e.message_id) message.unshift(segment.reply(e.message_id))

const reply_log = this.reply_log(msg)
const reply_log = this.reply_log(message)

if (e.isGroup) {
logger.info(common.logger(e.self_id, `${logger.green(`Send Group ${e.group_id}: `)}${reply_log}`))
Expand All @@ -116,8 +116,8 @@ export default class Event {

let msgRes
try {
msgRes = await e.replyNew(msg, retry_count)
logger.debug(common.logger(e.self_id, `回复消息结果:${JSON.stringify(msg, null, 2)}`))
msgRes = await e.replyNew(message, retry_count)
logger.debug(common.logger(e.self_id, `回复消息结果:${JSON.stringify(message, null, 2)}`))
} catch (err) {
logger.error(common.logger(e.self_id, `回复消息失败:${reply_log}`))
logger.error(common.logger(e.self_id), err)
Expand Down
8 changes: 5 additions & 3 deletions lib/event/message.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,17 +86,19 @@ class Message extends Event {
break
}
case 'image':
e.img.push(val.file)
e.image.push(val.file)
e.raw_message += `<image file=${val.file}> `
break
case 'at':
/** atBot不计入e.at */
if (val.uid == e.bot.account.uid) {
if (val.uid && val.uid == e.bot.account.uid) {
e.atBot = true
} else if (val.uin == e.bot.account.uin) {
e.atBot = true
} else if (val.uid && val.uid === 'all') {
e.atAll = true
} else {
e.at = val.uid || val.uin
e.at = val.uin || val.uid
}
e.raw_message += `<at uid=${val.uid}, uin=${val.uin}> `
break
Expand Down
10 changes: 5 additions & 5 deletions lib/event/type.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ class KarinMessage extends KarinEvent {
* elements: Array<KarinElement>,
* msg?: string,
* game?: string,
* img?: Array<string>,
* image?: Array<string>,
* at?: string,
* atBot?: boolean,
* atAll?: boolean,
Expand All @@ -174,8 +174,8 @@ class KarinMessage extends KarinEvent {
group_id = '',
msg = '',
game = '',
img = [],
at = '',
image = [],
at = [],
atBot = false,
atAll = false,
file = {},
Expand All @@ -188,7 +188,7 @@ class KarinMessage extends KarinEvent {
this.elements = elements
this.msg = msg
this.game = game
this.img = img
this.image = image
this.at = at
this.atBot = atBot
this.atAll = atAll
Expand Down Expand Up @@ -236,7 +236,7 @@ class KarinMessage extends KarinEvent {
* 图片数组
* @type {Array<string>}
*/
img
image

/**
* at
Expand Down

0 comments on commit 59afb57

Please sign in to comment.