refactor: 重构消息发送模型

This commit is contained in:
lixiangwuxian
2024-10-28 02:58:08 +08:00
parent fe863a9ac8
commit 7e28953a56
10 changed files with 196 additions and 16 deletions

53
action/action.go Normal file
View File

@@ -0,0 +1,53 @@
package action
import (
"encoding/json"
"fmt"
"git.lxtend.com/qqbot/model"
"github.com/gorilla/websocket"
)
func init() {
}
var ActionManager = actionManager{}
type actionManager struct {
botConn *websocket.Conn
}
func (am *actionManager) SetConn(conn *websocket.Conn) {
am.botConn = conn
}
func (am *actionManager) SendAction(action string) error {
return am.botConn.WriteMessage(websocket.TextMessage, []byte(action))
}
func (am *actionManager) SendMsg(reply model.Reply) error {
if reply.ReferOriginMsg {
reply.ReplyMsg = fmt.Sprintf("%s%s", GenReply(reply.FromMsg.OriginMsgId, "", 0, 0, 0), reply.ReplyMsg)
}
sendPkg := model.GenSendPkg(reply.FromMsg.UserId, reply.FromMsg.GroupInfo.GroupId, reply.ReplyMsg, false)
sendPkgJson, err := json.Marshal(sendPkg)
if err != nil {
return err
}
if err = am.botConn.WriteMessage(websocket.TextMessage, sendPkgJson); err != nil {
return err
}
return nil
}
func (am *actionManager) DrawbackMsg(msgId int32) error {
withDrawPkg := model.GenDrawbackPkg(msgId)
withDrawPkgJson, err := json.Marshal(withDrawPkg)
if err != nil {
return err
}
if err = am.botConn.WriteMessage(websocket.TextMessage, withDrawPkgJson); err != nil {
return err
}
return nil
}