Skip to content

Commit

Permalink
🎨 refactor: adapter and forward
Browse files Browse the repository at this point in the history
  • Loading branch information
ikechan8370 committed Apr 27, 2024
1 parent 4832733 commit efb86fd
Show file tree
Hide file tree
Showing 9 changed files with 772 additions and 75 deletions.
534 changes: 534 additions & 0 deletions lib/adapter/adapter.js

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion lib/adapter/kritor/api.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { kritor, common, Cfg, Bot } from '#Karin'
import { KarinAdapter } from '../adapter.js'

export default class {
export default class Kritor extends KarinAdapter {
constructor (grpc, account) {
super()
/** grpc */
this.grpc = grpc
/** 自增 */
Expand Down
41 changes: 22 additions & 19 deletions lib/adapter/onebot/OneBot11.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import Bot from '../../bot/bot.js'
import logger from '../../config/log.js'
import common from '../../common/common.js'
import Cfg from '../../config/config.js'
import { KarinAdapter } from '../adapter.js'

/**
* @typedef OneBotSegmentNode
Expand All @@ -30,7 +31,11 @@ import Cfg from '../../config/config.js'
* @property {Array<object>} data.content - 节点内容
*/

class OneBot11 {
/**
* @class OneBot11
* @extends KarinAdapter
*/
class OneBot11 extends KarinAdapter {
socket
request
/** 机器人QQ */
Expand All @@ -46,6 +51,7 @@ class OneBot11 {
/** 版本信息 */
version
constructor (socket, request) {
super()
this.socket = socket
this.request = request
this.#start()
Expand Down Expand Up @@ -188,7 +194,7 @@ class OneBot11 {
}

const e = new KarinMessage(message)

e.bot = this
/**
* 快速撤回,可键入指定message_id
* @param {number} message_id - 消息ID
Expand All @@ -203,14 +209,6 @@ class OneBot11 {
if (!Array.isArray(message)) {
message = [message]
}

if (message.some(val => val.type === 'node')) {
// 应该全是node
let nodes = message.filter(val => val.type === 'node').map(this.formatNode)
let res_id = await this.UploadForwardMessage(e.contact, nodes)
return await this.SendMessageByResId(e.contact, res_id)
}

if (data.message_type === 'private') {
return this.send_private_msg(data.user_id, message)
} else {
Expand Down Expand Up @@ -503,6 +501,7 @@ class OneBot11 {

/**
* 发送消息
*
* @param {Contact} contact
* @param {Array<KarinElement>} elements
* @returns {Promise<message_id>} - 消息ID
Expand All @@ -519,7 +518,7 @@ class OneBot11 {
/**
* 上传合并转发消息
* @param {object} contact - 联系人信息
* @param {OneBotSegmentNode[] | OneBotSegmentNode} elements - ob的node
* @param {KarinNodeElement[] | KarinNodeElement} elements - nodes
* @returns {Promise<string>} - 资源id
* */
async UploadForwardMessage (contact, elements) {
Expand All @@ -533,18 +532,22 @@ class OneBot11 {
const message_type = scene === 'group' ? 'group_id' : 'user_id'
let selfUin = this.account.uin
let selfNick = this.account.name
elements.forEach(element => {
element.data.content = this.Elements_OneBot_V11(element.data.content)
if (!element.data.uin) {
element.data.uin = selfUin
}
if (!element.data.name) {
element.data.name = selfNick
let obElements = elements.map(element => {
/**
* @type {OneBotSegmentNode}
*/
let obElement = {
type: 'node',
data: {}
}
obElement.data.content = this.Elements_OneBot_V11(element.content)
obElement.data.uin = element.user_id || selfUin
obElement.data.name = element.nickname || selfNick
return obElement
})
const params = {
[message_type]: String(peer),
messages: elements
messages: obElements
}
return await this.SendApi('send_forward_msg', params)
}
Expand Down
Loading

0 comments on commit efb86fd

Please sign in to comment.