Skip to content

Commit

Permalink
🔥 fix:完善Element
Browse files Browse the repository at this point in the history
  • Loading branch information
Lain. committed Apr 20, 2024
1 parent d39a78d commit 6e350a2
Show file tree
Hide file tree
Showing 7 changed files with 734 additions and 54 deletions.
5 changes: 5 additions & 0 deletions config/defSet/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ master: []
# 管理员列表 子权限
admin: []

# 超时时间 秒
timeout:
grpc: 30
ws: 30

# 黑名单相关
black_list:
# 黑名单用户
Expand Down
5 changes: 3 additions & 2 deletions lib/adapter/kritor/api.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { EventEmitter } from 'events'
import { kritor, common } from '#Karin'
import { kritor, common, Cfg } from '#Karin'

export default class extends EventEmitter {
constructor (grpc, account) {
Expand Down Expand Up @@ -830,7 +830,8 @@ export default class extends EventEmitter {
* @param {number} time - 请求超时时间 默认10s
* @returns {Promise<protobuf>}
*/
SendApi (cmd, buf, time = 10) {
SendApi (cmd, buf, time) {
if (!time) time = Cfg.timeout.grpc
const seq = this.seq
/** 立刻自增防止并发导致重复的seq... */
this.seq++
Expand Down
130 changes: 121 additions & 9 deletions lib/adapter/kritor/converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,22 @@ import {
KarinGroupFileUploadedNotice,
KarinFriendApplyRequest,
KarinGroupApplyRequest,
KarinInvitedJoinGroupRequest
KarinInvitedJoinGroupRequest,
KarinBubbleFaceElement,
KarinRecordElement,
KarinBasketballElement,
KarinvideoElement,
KarinDiceElement,
KarinRpsElement,
KarinPokeElement,
KarinMusicElement,
KarinWeatherElement,
KarinLocationElement,
KarinShareElement,
GiftElement,
KarinForwardElement,
KarinXmlElement,
KarinJsonElement
} from '../../bot/model.js'
import { logger, Bot, kritor } from '#Karin'
import { KarinMessage } from '../../event/type.js'
Expand Down Expand Up @@ -78,33 +93,130 @@ export class MessageConverter extends Converter {

for (let i of data.elements) {
switch (i.type) {
/** 文本消息 */
case kritor.common.Element.ElementType.TEXT: {
elements.push(new KarinTextElement(i.text.text))
break
}
case kritor.common.Element.ElementType.IMAGE: {
elements.push(new KarinImageElement(i.image.file_url))
break
}
/** 艾特消息 */
case kritor.common.Element.ElementType.AT: {
elements.push(new KarinAtElement(i.at.uid, i.at.uin))
break
}
/** 表情消息 */
case kritor.common.Element.ElementType.FACE: {
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))
/** 图片消息 */
case kritor.common.Element.ElementType.IMAGE: {
const { file_url, ...args } = i.image
elements.push(new KarinImageElement(i.image.file_url, ...args))
break
}
/** 弹射表情 */
case kritor.common.Element.ElementType.BUBBLE_FACE: {
elements.push(new KarinBubbleFaceElement(i.bubble_face.id, i.bubble_face.count))
break
}
/** 回复消息 */
case kritor.common.Element.ElementType.REPLY: {
elements.push(new KarinReplyElement(i.reply.message_id))
break
}
// todo 其他类型
/** 语音消息 */
case kritor.common.Element.ElementType.VOICE: {
const { file_url, ...args } = i.voice
elements.push(new KarinRecordElement(file_url, ...args))
break
}
/** 视频消息 */
case kritor.common.Element.ElementType.VIDEO: {
const { file_url, ...args } = i.video
elements.push(new KarinvideoElement(file_url, ...args))
break
}
/** 篮球消息 */
case kritor.common.Element.ElementType.BASKETBALL: {
elements.push(new KarinBasketballElement(i.basketball.id))
break
}
/** 骰子消息 */
case kritor.common.Element.ElementType.DICE: {
elements.push(new KarinDiceElement(i.dice.id))
break
}
case kritor.common.Element.ElementType.RPS: {
elements.push(new KarinRpsElement(i.rps.id))
break
}
/** 戳一戳消息 */
case kritor.common.Element.ElementType.POKE: {
const { id = 1, type = 1, strength = 1 } = i.poke
elements.push(new KarinPokeElement(type, id, strength))
break
}
/** 音乐消息 */
case kritor.common.Element.ElementType.MUSIC: {
const { platform, id = '', custom = '' } = i.music
elements.push(new KarinMusicElement(platform, id || custom))
break
}
/** 天气消息 */
case kritor.common.Element.ElementType.WEATHER: {
const { city, code } = i.weather
elements.push(new KarinWeatherElement(city, code))
break
}
/** 位置消息 */
case kritor.common.Element.ElementType.LOCATION: {
const { title = '', address = '', lat, lon } = i.location
elements.push(new KarinLocationElement(lat, lon, title, address))
break
}
/** 链接分享 */
case kritor.common.Element.ElementType.SHARE: {
const { url, title, content, image } = i.share
elements.push(new KarinShareElement(url, title, content, image))
break
}
/** 礼物消息 只收不发 */
case kritor.common.Element.ElementType.GIFT: {
const { qq, id } = i.gift
elements.push(new GiftElement(qq, id))
break
}
/** 转发消息 */
case kritor.common.Element.ElementType.FORWARD: {
const { res_id, uniseq = '', summary = '', description = '' } = i.forward
elements.push(new KarinForwardElement(res_id, uniseq, summary, description))
break
}
/** 文件消息 只收不发 */
case kritor.common.Element.ElementType.FILE: {
elements.push(new KarinFileElement(i.file.file_url))
break
}
/** JSON消息 */
case kritor.common.Element.ElementType.JSON: {
elements.push(new KarinJsonElement(i.json.json))
break
}
/** XML消息 */
case kritor.common.Element.ElementType.XML: {
elements.push(new KarinXmlElement(i.xml.xml))
break
}
// 这都啥玩意啊...
case kritor.common.Element.ElementType.MARKET_FACE:
case kritor.common.Element.ElementType.CONTACT:
case kritor.common.Element.ElementType.MARKDOWN:
case kritor.common.Element.ElementType.KEYBOARD:
default: {
logger.info(i)
let { ...args } = i
args = JSON.stringify(args)
logger.warn(`未知消息类型 ${i.type} ${args}`)
elements.push(new KarinTextElement(args))
}
}
}
Expand Down
58 changes: 50 additions & 8 deletions lib/adapter/onebot/OneBot11.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
import {
// eslint-disable-next-line no-unused-vars
KarinElement,
KarinFileElement,
KarinAtElement,
KarinTextElement,
KarinFaceElement,
KarinImageElement,
KarinTextElement,
KarinReplyElement
KarinRecordElement,
KarinvideoElement,
KarinAtElement,
KarinPokeElement,
KarinContactElement,
KarinLocationElement,
KarinReplyElement,
KarinForwardElement,
KarinXmlElement,
KarinJsonElement,
KarinFileElement
} from '../../bot/model.js'
import { KarinMessage } from '../../event/type.js'
import { randomUUID } from 'crypto'
import Bot from '../../bot/bot.js'
import logger from '../../config/log.js'
import common from '../../common/common.js'
import Cfg from '../../config/config.js'

class OneBot11 {
socket
Expand Down Expand Up @@ -316,18 +325,50 @@ class OneBot11 {
case 'image':
elements.push(new KarinImageElement(i.data.url || i.data.file))
break
case 'record':
elements.push(new KarinRecordElement(i.data.url || i.data.file))
break
case 'video':
elements.push(new KarinvideoElement(i.data.url || i.data.file))
break
case 'at':
elements.push(new KarinAtElement(i.data.qq, i.data.name))
elements.push(new KarinAtElement(i.data.qq))
break
case 'poke':
elements.push(new KarinPokeElement(i.data.type, i.data.id))
break
case 'contact':
elements.push(new KarinContactElement(i.data.type, i.data.id))
break
case 'location':
elements.push(new KarinLocationElement(i.data.lat, i.data.lon, i.data.title, i.data.content))
break
case 'music':
// 收不到这种类型的消息,收到的都是json
break
case 'reply':
elements.push(new KarinReplyElement(i.data.id))
break
case 'forward':
elements.push(new KarinForwardElement(i.data.id))
break
case 'node':
// 收不到这种类型的消息,收到的都是forward
break
case 'file':
elements.push(new KarinFileElement(i.data.name, i.data.url))
break
// todo 其他类型
case 'json':
elements.push(new KarinJsonElement(i.data.data))
break
case 'xml':
elements.push(new KarinXmlElement(i.data.data))
break
case 'custom':
case 'button':
case 'rows':
default: {
logger.info(i)
elements.push(new KarinTextElement(JSON.stringify(i)))
}
}
}
Expand Down Expand Up @@ -965,7 +1006,8 @@ class OneBot11 {
* @param {object} params - API参数
* @returns {Promise<object>} - API返回
*/
async SendApi (action, params = {}, time = 10) {
async SendApi (action, params = {}, time) {
if (!time) time = Cfg.timeout.ws
const echo = randomUUID()
const request = JSON.stringify({ echo, action, params })
return new Promise((resolve, reject) => {
Expand Down
Loading

0 comments on commit 6e350a2

Please sign in to comment.