Skip to content

Commit

Permalink
fix: 沙盒新增接收at,button,poke,发送poke
Browse files Browse the repository at this point in the history
  • Loading branch information
XasYer committed Nov 1, 2024
1 parent f4b556e commit f60f139
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 12 deletions.
25 changes: 22 additions & 3 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,28 @@
# TODO List

- [x] 沙盒测试
- [ ] 渲染转发消息
- [ ] 适配更多发送的消息类型
- [ ] 适配更多接收的消息类型
- [ ] 发送类型
- [x] text
- [ ] image
- [ ] at
- [ ] reply
- [ ] button
- [x] poke
- [ ] node
- [ ] markdown
- [ ] video
- [ ] audio
- [ ] 接收类型
- [x] text
- [x] image
- [x] at
- [x] reply
- [x] button
- [x] poke
- [ ] node
- [ ] markdown
- [ ] video
- [ ] audio
- [x] 好像height有点大了
- [x] 插件库预览 安装插件 卸载插件
- [ ] 本体设置
Expand Down
74 changes: 65 additions & 9 deletions src/api/Bot/sandbox/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { version } from '@/common'
import { RouteOptions } from 'fastify'
import { WebSocket } from 'ws'
import fs from 'fs'
import { randomUUID } from 'crypto'

export default [
{
Expand All @@ -25,7 +26,10 @@ export default [
logger.mark(`${uin} 已连接`)
break
case 'message':
createMessage(data.uin, data.userId, data.groupId, data.content, data.permission)
createMessage(data.uin, data.userId, data.groupId, data.msgId, data.content, data.permission)
break
case 'poke':
createPoke(data.uin, data.operatorId, data.targetId, data.userId, data.groupId, data.permission)
break
default:
break
Expand Down Expand Up @@ -115,6 +119,9 @@ function createSendbox (id: string, nickname: string, avatar: string, ws: WebSoc
},
getInfo () {
return info
},
poke: () => {
return this.sendPrivateMsg(userId, { type: 'poke', qq: userId })
}
}
},
Expand All @@ -134,6 +141,9 @@ function createSendbox (id: string, nickname: string, avatar: string, ws: WebSoc
},
getInfo () {
return info
},
pokeMember: (userId: string) => {
return this.sendGroupMsg(groupId, { type: 'poke', qq: userId })
}
}
},
Expand All @@ -145,18 +155,21 @@ function createSendbox (id: string, nickname: string, avatar: string, ws: WebSoc
return {
info,
...info,
...this.pickFriend(userId)
...this.pickFriend(userId),
poke: () => this.sendGroupMsg(groupId, { type: 'poke', qq: userId })
}
},
sendPrivateMsg (userId: string, message: any) {
ws.send?.(JSON.stringify({ type: 'friend', id: userId, content: dealMessage(message) }))
const msgId = userId + '.' + randomUUID()
ws.send?.(JSON.stringify({ type: 'friend', id: userId, content: dealMessage(message), msgId }))
logger.info(`${logger.blue(`[${uin} => ${userId}]`)} 发送私聊消息:${JSON.stringify(message).replace(/data:image\/png;base64,.*?(,|]|")/g, 'base64://...$1')}`)
return new Promise((resolve) => resolve({ message_id: Date.now() }))
return new Promise((resolve) => resolve({ message_id: msgId }))
},
sendGroupMsg (groupId: string, message: string) {
ws.send?.(JSON.stringify({ type: 'group', id: groupId, content: dealMessage(message) }))
const msgId = groupId + '.' + randomUUID()
ws.send?.(JSON.stringify({ type: 'group', id: groupId, content: dealMessage(message), msgId }))
logger.info(`${logger.blue(`[${uin} => ${groupId}]`)} 发送群聊消息:${JSON.stringify(message).replace(/data:image\/png;base64,.*?(,|]|")/g, 'base64://...$1')}`)
return new Promise((resolve) => resolve({ message_id: Date.now() }))
return new Promise((resolve) => resolve({ message_id: msgId }))
},
getFriendList () {
return this.fl
Expand Down Expand Up @@ -212,14 +225,14 @@ function createSendbox (id: string, nickname: string, avatar: string, ws: WebSoc
}
}

function createMessage (id: string, userId: string, groupId: string, content: string, permission: 'owner' | 'admin' | 'user' | 'master' = 'user') {
function createMessage (id: string, userId: string, groupId: string, msgId: string, content: string, permission: 'owner' | 'admin' | 'user' | 'master' = 'user') {
const key = 'YePanel.sandbox.'
const uin = key + id
const bot = Bot[uin]
const e = {
bot: Bot[uin],
adapter: bot.version,
message_id: Date.now(),
message_id: msgId,
sender: {
user_id: userId,
nickname: userId,
Expand All @@ -237,7 +250,7 @@ function createMessage (id: string, userId: string, groupId: string, content: st
? {
message_type: 'group',
sub_type: 'normal',
groupId: groupId,
groupId,
group_name: groupId
}
: {
Expand Down Expand Up @@ -304,3 +317,46 @@ function dealMessage (message: any) {
}
return message
}

function createPoke (id: string, operatorId: string, targetId: string, userId: string, groupId?: string, permission: 'owner' | 'admin' | 'user' | 'master' = 'user') {
const key = 'YePanel.sandbox.'
const uin = key + id
const bot = Bot[uin]
const e = {
bot: Bot[uin],
adapter: bot.version,
user_id: groupId ? targetId : userId,
self_id: uin,
isMaster: permission === 'master',
post_type: 'notice',
operator_id: operatorId,
target_id: targetId == id ? uin : targetId,
...groupId
? {
notice_type: 'group',
sub_type: 'poke',
groupId,
group_name: groupId
}
: {
notice_type: 'friend',
sub_type: 'poke'
},
friend: bot.pickFriend(userId),
group: groupId ? bot.pickGroup(groupId) : undefined,
member: (groupId && userId) ? bot.pickMember(groupId, userId) : undefined
}
let event = `${e.post_type}.${e.notice_type}.${e.sub_type}`
logger.info(`${logger.blue(`[${e.self_id}]`)} ${groupId ? '群' : '好友'}事件:[${groupId ? groupId + ', ' : ''}${e.operator_id}] 戳了戳 [${e.target_id}]`)
if (version.BotName === 'TRSS') {
Bot.em(event, e)
} else {
// eslint-disable-next-line no-constant-condition
while (true) {
Bot.emit(event, e)
const i = event.lastIndexOf('.')
if (i == -1) break
event = event.slice(0, i)
}
}
}

0 comments on commit f60f139

Please sign in to comment.