Skip to content

Commit

Permalink
fix(Notice): Notice增加跨平台开关参数,修复存活消息复读问题 (sealdice#189)
Browse files Browse the repository at this point in the history
* docs(Notice): 如果一个Notice到最后也没有发出,记录日志
  • Loading branch information
Xiangze-Li authored Sep 8, 2023
1 parent b6596fd commit e91da4b
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 30 deletions.
5 changes: 3 additions & 2 deletions api/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package api

import (
"fmt"
"github.com/labstack/echo/v4"
"net/http"
"sealdice-core/dice"
"sealdice-core/dice/model"
"strings"
"time"

"github.com/labstack/echo/v4"
)

func groupList(c echo.Context) error {
Expand Down Expand Up @@ -100,7 +101,7 @@ func groupQuit(c echo.Context) error {
myDice.Logger.Info(_txt)

ctx := &dice.MsgContext{Dice: myDice, EndPoint: ep, Session: myDice.ImSession}
ctx.Notice(_txt)
ctx.Notice(_txt, true)
//dice.SetBotOffAtGroup(ctx, group.GroupId)

if !v.Silence {
Expand Down
2 changes: 1 addition & 1 deletion dice/builtin_commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ func (d *Dice) registerCoreCommands() {
userName := ctx.Dice.Parent.TryGetUserName(msg.Sender.UserId)
_txt := fmt.Sprintf("指令退群: 于群组<%s>(%s)中告别,操作者:<%s>(%s)", ctx.Group.GroupName, msg.GroupId, userName, msg.Sender.UserId)
d.Logger.Info(_txt)
ctx.Notice(_txt)
ctx.Notice(_txt, true)
SetBotOffAtGroup(ctx, ctx.Group.GroupId)
time.Sleep(6 * time.Second)
ctx.Group.DiceIdExistsMap.Delete(ctx.EndPoint.UserId)
Expand Down
3 changes: 2 additions & 1 deletion dice/dice.go
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,8 @@ func (d *Dice) ApplyAliveNotice() {
entry, err := d.Cron.AddFunc(d.AliveNoticeValue, func() {
for _, ep := range d.ImSession.EndPoints {
ctx := &MsgContext{Dice: d, EndPoint: ep, Session: d.ImSession}
ctx.Notice(fmt.Sprintf("存活, D100=%d", DiceRoll64(100)))
// FIXME: 如果开启了邮件通知,每个存在的EP都会发一封邮件
ctx.Notice(fmt.Sprintf("存活, D100=%d", DiceRoll64(100)), false)
}
})
if err == nil {
Expand Down
5 changes: 3 additions & 2 deletions dice/dice_ban.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package dice
import (
"encoding/json"
"fmt"
"github.com/robfig/cron/v3"
"sealdice-core/dice/model"
"strings"
"time"

"github.com/robfig/cron/v3"
)

type BanRankType int
Expand Down Expand Up @@ -223,7 +224,7 @@ func (i *BanListInfo) NoticeCheck(uid string, place string, oldRank BanRankType,

if ctx != nil {
// 做出通知
ctx.Notice(txt)
ctx.Notice(txt, true)

// 发给当事人
ReplyPersonRaw(ctx, &Message{Sender: SenderBase{UserId: uid}}, "提醒:你引发了黑名单事件:\n"+txt, "")
Expand Down
39 changes: 27 additions & 12 deletions dice/im_session.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@ package dice

import (
"fmt"
"github.com/dop251/goja"
"github.com/fy0/lockfree"
"github.com/jmoiron/sqlx"
"golang.org/x/time/rate"
"gopkg.in/yaml.v3"
"runtime/debug"
"sealdice-core/dice/model"
"sort"
"strings"
"time"

"github.com/dop251/goja"
"github.com/fy0/lockfree"
"github.com/jmoiron/sqlx"
"golang.org/x/time/rate"
"gopkg.in/yaml.v3"
)

type SenderBase struct {
Expand Down Expand Up @@ -399,7 +400,7 @@ func (s *IMSession) Execute(ep *EndPointInfo, msg *Message, runInSync bool) {
txt := fmt.Sprintf("自动激活: 发现无记录群组%s(%s),因为已是群成员,所以自动激活,开启状态: %t", groupName, group.GroupId, autoOn)
ep.Adapter.GetGroupInfoAsync(msg.GroupId)
log.Info(txt)
mctx.Notice(txt)
mctx.Notice(txt, true)

if msg.Platform == "QQ" || msg.Platform == "TG" {
if mctx.Session.ServiceAtNew[msg.GroupId] != nil {
Expand Down Expand Up @@ -1076,7 +1077,10 @@ func (ep *EndPointInfo) RefreshGroupNum() {
}
}

func (ctx *MsgContext) Notice(txt string) {
// Notice 向通知列表发送通知
// - txt 通知内容
// - allowCrossPlatform 是否允许跨平台发送
func (ctx *MsgContext) Notice(txt string, allowCrossPlatform bool) {
foo := func() {
defer func() {
if r := recover(); r != nil {
Expand All @@ -1089,6 +1093,8 @@ func (ctx *MsgContext) Notice(txt string) {
return
}

sent := false

for _, i := range ctx.Dice.NoticeIds {
n := strings.Split(i, ":")
if len(n) < 2 {
Expand All @@ -1109,15 +1115,24 @@ func (ctx *MsgContext) Notice(txt string) {
ReplyPerson(ctx, &Message{Sender: SenderBase{UserId: i}}, txt)
}
time.Sleep(1 * time.Second)
continue
sent = true
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)
// 如果走到这里,一定是没有找到对应的平台
if allowCrossPlatform {
if done := CrossMsgBySearch(ctx.Session, seg, i, txt, messageType == "private"); !done {
ctx.Dice.Logger.Errorf("尝试跨平台后仍未能向 %s 发送通知:%s", i, txt)
} else {
sent = true
time.Sleep(1 * time.Second)
}
}
}

if !sent {
ctx.Dice.Logger.Errorf("未能发送来自%s的通知:%s", ctx.EndPoint.Platform, txt)
}
}
go foo()
}
Expand Down
12 changes: 6 additions & 6 deletions dice/platform_adapter_qq.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ func (pa *PlatformAdapterGocq) Serve() int {
userName := dm.TryGetUserName(uid)
txt := fmt.Sprintf("收到QQ加群邀请: 群组<%s>(%d) 邀请人:<%s>(%d)", groupName, msgQQ.GroupId, userName, msgQQ.UserId)
log.Info(txt)
ctx.Notice(txt)
ctx.Notice(txt, true)
tempInviteMap[msg.GroupId] = time.Now().Unix()
tempInviteMap2[msg.GroupId] = uid

Expand Down Expand Up @@ -523,7 +523,7 @@ func (pa *PlatformAdapterGocq) Serve() int {

txt := fmt.Sprintf("收到QQ好友邀请: 邀请人:%d, 验证信息: %s, 是否自动同意: %t%s", msgQQ.UserId, comment, willAccept, extra)
log.Info(txt)
ctx.Notice(txt)
ctx.Notice(txt, true)

// 忽略邀请
if pa.IgnoreFriendRequest {
Expand Down Expand Up @@ -634,7 +634,7 @@ func (pa *PlatformAdapterGocq) Serve() int {
}()
txt := fmt.Sprintf("加入QQ群组: <%s>(%d)", groupName, msgQQ.GroupId)
log.Info(txt)
ctx.Notice(txt)
ctx.Notice(txt, true)
if ctx.Session.ServiceAtNew[msg.GroupId] != nil {
for _, i := range ctx.Session.ServiceAtNew[msg.GroupId].ActivatedExtList {
if i.OnGroupJoined != nil {
Expand Down Expand Up @@ -739,7 +739,7 @@ func (pa *PlatformAdapterGocq) Serve() int {

txt := fmt.Sprintf("被踢出群: 在QQ群组<%s>(%d)中被踢出,操作者:<%s>(%d)%s", groupName, msgQQ.GroupId, userName, msgQQ.OperatorId, extra)
log.Info(txt)
ctx.Notice(txt)
ctx.Notice(txt, true)
}
return
}
Expand All @@ -750,7 +750,7 @@ func (pa *PlatformAdapterGocq) Serve() int {
groupName := dm.TryGetGroupName(msg.GroupId)
txt := fmt.Sprintf("离开群组或群解散: <%s>(%d)", groupName, msgQQ.GroupId)
log.Info(txt)
ctx.Notice(txt)
ctx.Notice(txt, true)
return
}

Expand All @@ -765,7 +765,7 @@ func (pa *PlatformAdapterGocq) Serve() int {
ctx.Dice.BanList.AddScoreByGroupMuted(opUid, msg.GroupId, ctx)
txt := fmt.Sprintf("被禁言: 在群组<%s>(%d)中被禁言,时长%d秒,操作者:<%s>(%d)", groupName, msgQQ.GroupId, msgQQ.Duration, userName, msgQQ.OperatorId)
log.Info(txt)
ctx.Notice(txt)
ctx.Notice(txt, true)
}
return
}
Expand Down
13 changes: 7 additions & 6 deletions dice/platform_adapter_walleq.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package dice
import (
"encoding/json"
"fmt"
"github.com/sacOO7/gowebsocket"
"math/rand"
"os"
"os/signal"
Expand All @@ -16,6 +15,8 @@ import (
"strings"
"syscall"
"time"

"github.com/sacOO7/gowebsocket"
)

/* 定义结构体 */
Expand Down Expand Up @@ -275,7 +276,7 @@ func (pa *PlatformAdapterWalleQ) Serve() int {
}()
txt := fmt.Sprintf("加入QQ群组: <%s>(%s)", groupName, event.GroupId)
log.Info(txt)
ctx.Notice(txt)
ctx.Notice(txt, true)
}

// 入群的另一种情况: 管理员审核
Expand Down Expand Up @@ -425,14 +426,14 @@ func (pa *PlatformAdapterWalleQ) Serve() int {

txt := fmt.Sprintf("被踢出群: 在QQ群组<%s>(%s)中被踢出,操作者:<%s>(%s)%s", groupName, event.GroupId, userName, n.OperatorId, extra)
log.Info(txt)
ctx.Notice(txt)
ctx.Notice(txt, true)
}
case "group_member_ban": // 被禁言
if event.UserId == event.Self.UserId {
ctx.Dice.BanList.AddScoreByGroupMuted(opUid, msg.GroupId, ctx)
txt := fmt.Sprintf("被禁言: 在群组<%s>(%s)中被禁言,时长%d秒,操作者:<%s>(%s)", groupName, msg.GroupId, n.Duration, userName, n.OperatorId)
log.Info(txt)
ctx.Notice(txt)
ctx.Notice(txt, true)
}
return
case "group_message_delete": // 消息撤回
Expand Down Expand Up @@ -528,7 +529,7 @@ func (pa *PlatformAdapterWalleQ) Serve() int {

txt := fmt.Sprintf("收到QQ好友邀请: 邀请人:%s, 验证信息: %s, 是否自动同意: %t%s", event.UserId, comment, willAccept, extra)
log.Info(txt)
ctx.Notice(txt)
ctx.Notice(txt, true)

// 忽略邀请
if pa.IgnoreFriendRequest {
Expand Down Expand Up @@ -559,7 +560,7 @@ func (pa *PlatformAdapterWalleQ) Serve() int {
userName := dm.TryGetUserName(uid)
txt := fmt.Sprintf("收到QQ加群邀请: 群组<%s>(%s) 邀请人:<%s>(%s)", groupName, event.GroupId, userName, event.UserId)
log.Info(txt)
ctx.Notice(txt)
ctx.Notice(txt, true)
//tempInviteMap[msg.GroupId] = time.Now().Unix()
//tempInviteMap2[msg.GroupId] = uid

Expand Down

0 comments on commit e91da4b

Please sign in to comment.