Skip to content

Commit

Permalink
refactor: (*MsgContext).Notice;初步支持跨平台发送消息 (sealdice#164)
Browse files Browse the repository at this point in the history
  • Loading branch information
oissevalt authored Sep 6, 2023
1 parent 1a1b1b4 commit e53e334
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 8 deletions.
33 changes: 33 additions & 0 deletions dice/im_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,39 @@ func ReplyPersonRaw(ctx *MsgContext, msg *Message, text string, flag string) {
}
}

// CrossMsgBySearch
// 在 se 中找到第一个平台等于 p 且启用的 EndPointInfo, 并向目标 t 发送消息,
// pr 判断是否为私聊消息
func CrossMsgBySearch(se *IMSession, p, t, txt string, pr bool) bool {
ep := se.GetEpByPlatform(p)
if ep == nil {
return false
}
mctx := &MsgContext{
EndPoint: ep,
Session: ep.Session,
Dice: ep.Session.Parent,
}

if g, ok := mctx.Session.ServiceAtNew[t]; ok {
mctx.IsCurGroupBotOn = g.Active
mctx.Group = g
}

if !pr {
mctx.MessageType = "group"
ReplyGroup(mctx, &Message{GroupId: t}, txt)
} else {
mctx.IsPrivate = true
mctx.MessageType = "private"
ReplyPerson(mctx, &Message{Sender: SenderBase{UserId: t}}, txt)
}

return true
}

// TODO: CrossMsgById 用指定 Id 的 EndPoint 发送跨平台消息,现在似乎没有这个需求

func ReplyPerson(ctx *MsgContext, msg *Message, text string) {
ReplyPersonRaw(ctx, msg, text, "")
}
Expand Down
40 changes: 32 additions & 8 deletions dice/im_session.go
Original file line number Diff line number Diff line change
Expand Up @@ -999,6 +999,17 @@ func (s *IMSession) OnMessageSend(ctx *MsgContext, msg *Message, flag string) {
}
}

// GetEpByPlatform
// 在 EndPoints 中找到第一个符合平台 p 且启用的
func (s *IMSession) GetEpByPlatform(p string) *EndPointInfo {
for _, ep := range s.EndPoints {
if ep.Enable && ep.Platform == p {
return ep
}
}
return nil
}

// SetEnable
/* 如果已连接,将断开连接,如果开着GCQ将自动结束。如果启用的话,则反过来 */
func (ep *EndPointInfo) SetEnable(d *Dice, enable bool) {
Expand Down Expand Up @@ -1080,19 +1091,32 @@ func (ctx *MsgContext) Notice(txt string) {

for _, i := range ctx.Dice.NoticeIds {
n := strings.Split(i, ":")
if len(n) >= 2 {
seg := strings.Split(n[0], "-")[0]
if ctx.EndPoint.Platform != seg {
ctx.Dice.Logger.Infof("发给 %s,但由于平台不同而未发送通知:%s", i, txt)
continue
}
if strings.HasSuffix(n[0], "-Group") {
if len(n) < 2 {
continue
}

seg := strings.Split(n[0], "-")[0]

messageType := "private"
if strings.HasSuffix(n[0], "-Group") {
messageType = "group"
}

if ctx.EndPoint.Platform == seg {
if messageType == "group" {
ReplyGroup(ctx, &Message{GroupId: i}, txt)
} else {
ReplyPerson(ctx, &Message{Sender: SenderBase{UserId: i}}, txt)
}
time.Sleep(1 * time.Second)
continue
}

if done := CrossMsgBySearch(ctx.Session, seg, i, txt, messageType == "private"); !done {
ctx.Dice.Logger.Errorf("未能向 %s 发送通知:%s", i, txt)
} else {
time.Sleep(1 * time.Second)
}
time.Sleep(1 * time.Second)
}
}
go foo()
Expand Down

0 comments on commit e53e334

Please sign in to comment.