diff --git a/src/bridge/index.ts b/src/bridge/index.ts index bfa39ea..ebbb414 100644 --- a/src/bridge/index.ts +++ b/src/bridge/index.ts @@ -2,6 +2,7 @@ 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[] = [] @@ -9,6 +10,25 @@ 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) => { @@ -38,10 +58,16 @@ 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 @@ -49,13 +75,22 @@ OneBot.eventPipe.on('request', (action, params, callback) => { 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) } }) \ No newline at end of file diff --git a/src/iirose/bot.ts b/src/iirose/bot.ts index fb2bcc3..af63fbb 100644 --- a/src/iirose/bot.ts +++ b/src/iirose/bot.ts @@ -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'); + } } \ No newline at end of file diff --git a/src/iirose/packets/encoders/DeleteMessage.ts b/src/iirose/packets/encoders/DeleteMessage.ts new file mode 100644 index 0000000..5b843b2 --- /dev/null +++ b/src/iirose/packets/encoders/DeleteMessage.ts @@ -0,0 +1,3 @@ +export default (id: string) => { + return `v0#${id}` +} \ No newline at end of file diff --git a/src/iirose/packets/index.ts b/src/iirose/packets/index.ts index a9cdeca..7879e73 100644 --- a/src/iirose/packets/index.ts +++ b/src/iirose/packets/index.ts @@ -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) => {