Skip to content

Commit

Permalink
feat: 群消息事件和撤回消息
Browse files Browse the repository at this point in the history
  • Loading branch information
TheresaQWQ committed Jul 26, 2023
1 parent b55a991 commit 3378eff
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 6 deletions.
47 changes: 41 additions & 6 deletions src/bridge/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,33 @@ import Bot from "../iirose/bot";
import * as OneBot from '../onebot'
import { getConfig } from "../core/config";
import * as MessageProcessor from './message'
import id from "../core/id";

const bots: Bot[] = []

getConfig('accounts').forEach((account: any, index: number) => {
const bot = new Bot(account)
bots[index] = bot
bots[index].index = index

bot.on('PublicMessage', msg => {
OneBot.boardcast(JSON.stringify({
"id": id(),
"self": getConfig('compatibility.events.self'),
"time": Date.now() / 1000,
"type": "message",
"detail_type": "group",
"sub_type": "",
"message_id": msg.messageId,
"message": [
...MessageProcessor.str2msg(msg.message)
// 处理回复消息
],
"alt_message": msg.message,
"group_id": index.toString(),
"user_id": msg.uid
}))
})
})

OneBot.eventPipe.on('request', (action, params, callback) => {
Expand Down Expand Up @@ -38,24 +58,39 @@ OneBot.eventPipe.on('request', (action, params, callback) => {

const msgStr = MessageProcessor.msg2str(params.message)
bot.sendPublicMessage(msgStr, msgId)

callback('ok', 0, {
time: Date.now() / 1000,
message_id: `${id}:${msgId}`
})
} else if (detail_type === 'private') {
// 发送私聊消息
const uid = params.user_id as string
const bot = bots.sort(() => Math.random() - 0.5)[0]
const botId = bot.index
if (!bot) {
callback('failed', 35001, null, 'Logic Error: bot not found')
return
}

const msgStr = MessageProcessor.msg2str(params.message)
bot.sendPrivateMessage(uid, msgStr, msgId)
}

callback('ok', 0, {
time: Date.now() / 1000,
message_id: msgId
})
callback('ok', 0, {
time: Date.now() / 1000,
message_id: `${botId}:${msgId}`
})
}
} else if (action === 'delete_message') {
// TODO: 撤回消息
// 撤回消息
const id = params.message_id as string
const index = id.split(':')[0]
const bot = bots[parseInt(index)]

const msgId = id.split(':')[1]

bot.deleteMessage(msgId)

callback('ok', 0, null)
}
})
6 changes: 6 additions & 0 deletions src/iirose/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,10 @@ export default class Bot extends EventEmitter {
const succ = this.ws.send(packets.encode.PublicMessage(message, this.account.color, id));
if (!succ) throw new Error('Failed to send public message');
}

public deleteMessage (id: string) {
if (!this.ws) return
const succ = this.ws.send(packets.encode.DeleteMessage(id));
if (!succ) throw new Error('Failed to delete message');
}
}
3 changes: 3 additions & 0 deletions src/iirose/packets/encoders/DeleteMessage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default (id: string) => {
return `v0#${id}`
}
2 changes: 2 additions & 0 deletions src/iirose/packets/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ import DecodePrivateMessage from './decoders/PrivateMessage'
import EncodePrivateMessage from './encoders/PrivateMessage'
import EncodePublicMessage from './encoders/PublicMessage'
import Login from './encoders/Login'
import DeleteMessage from './encoders/DeleteMessage'

export default {
encode: {
PrivateMessage: EncodePrivateMessage,
PublicMessage: EncodePublicMessage,
DeleteMessage: DeleteMessage,
Login: Login
},
decode: (packet: string) => {
Expand Down

0 comments on commit 3378eff

Please sign in to comment.