Skip to content

Commit

Permalink
增加了ob11事件的匹配,增加了撤回消息后bot自动撤回相应的消息
Browse files Browse the repository at this point in the history
  • Loading branch information
lianhong2758 committed Nov 5, 2024
1 parent 66a22dd commit b5723f8
Show file tree
Hide file tree
Showing 13 changed files with 92 additions and 44 deletions.
4 changes: 2 additions & 2 deletions plugins/gscore/messsage.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func MakeSendCoreMessage(ctx *rosm.Ctx) []byte {
MessageReport := MessageReceive{
Bot_id: ctx.BotType,
Bot_self_id: ctx.Bot.Card().BotID,
Msg_id: ctx.Being.MsgID[0],
Msg_id: ctx.Being.MsgID,
User_type: "group",
Group_id: tool.MergePadString(ctx.Being.RoomID, ctx.Being.RoomID2),
User_id: ctx.Being.User.ID,
Expand All @@ -34,7 +34,7 @@ func MakeSendCoreMessage(ctx *rosm.Ctx) []byte {
Nickname: ctx.Being.User.Name,
},
}
cache.Set(ctx.Being.MsgID[0], ctx)
cache.Set(ctx.Being.MsgID, ctx)
marshal, err := json.Marshal(MessageReport)
if err != nil {
return nil
Expand Down
17 changes: 15 additions & 2 deletions plugins/test/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package test
import (
"github.com/lianhong2758/RosmBot-MUL/rosm"
"github.com/lianhong2758/RosmBot-MUL/server/ob11"
"github.com/lianhong2758/RosmBot-MUL/tool"
"github.com/sirupsen/logrus"
zero "github.com/wdvxdr1123/ZeroBot"
)

func init() {
Expand All @@ -13,7 +15,18 @@ func init() {
})
en.AddRex(`^\[CQ:reply,id=(-?[0-9]+)\].*`).MUL("ob11").Rule(rosm.OnlyMaster(), rosm.KeyWords("撤回")).Handle(func(ctx *rosm.Ctx) {
ob11.DeleteMessage(ctx, ctx.Being.Rex[1])
ob11.DeleteMessage(ctx, ctx.Being.MsgID[0])
logrus.Info("[delete]撤回消息",ctx.Being.Rex[1]," - ",ctx.Being.MsgID[0])
ob11.DeleteMessage(ctx, ctx.Being.MsgID)
logrus.Info("[delete]撤回消息", ctx.Being.Rex[1], " - ", ctx.Being.MsgID[0])
})
//跟随撤回
en.AddEvent(rosm.FriendRecall, rosm.GroupRecall).MUL("ob11").Handle(func(ctx *rosm.Ctx) {
if id, ok := ctx.Message.(*zero.Event).MessageID.(int64); ok {
if sids := rosm.GetMessageIDFormMapCache(tool.Int64ToString(id)); len(sids) > 0 {
for _, sid := range sids {
tool.WaitWhile()
ob11.DeleteMessage(ctx, sid)
}
}
}
})
}
8 changes: 4 additions & 4 deletions plugins/time/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ var (
// 表示每隔多长时间时,你还可以使用预定义的 @every <duration> 如每隔十分钟就可以表示为 @every 10m
func init() {
en := rosm.Register(&rosm.PluginData{
Name: "定时消息",
Help: "- /记录在* * * *的指令\n"+
"- /删除指令xxx",
Name: "定时消息",
Help: "- /记录在* * * *的指令\n" +
"- /删除指令xxx",
DataFolder: "time",
})
en.AddRex(`^/记录在(.*)的指令`).Rule(rosm.OnlyMaster()).Handle(func(ctx *rosm.Ctx) {
Expand Down Expand Up @@ -95,7 +95,7 @@ func init() {
}

func cronRun(db *model) {
tool.WaitInit()
tool.WaitWhile()
c = cron.New()
db.Range(func(i int, m *mode) bool {
timeCtx := send.CTXBuild(m.Types, m.BotID, m.String1)
Expand Down
23 changes: 16 additions & 7 deletions rosm/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,22 @@ package rosm
//可以把有相同性质的事件作为一个事件类型
//可以通过平台限制+事件类型,实现更具体的事件监听

type EventType = int
type EventType string

const (
//消息有关类型1-2,事件3+
AllMessage EventType = iota //全消息匹配
SurplusMessage //无其他插件匹配
Join //入群
Out //退群
UpFile //上传文件-ob11
//消息有关类型1-2
AllMessage = "AllMessage" //全消息匹配
SurplusMessage = "SurplusMessage" //无其他插件匹配
//ob11标准 https://github.com/botuniverse/onebot-11/blob/master/event/message.md
MemberJoin = "group_increase" //入群
MemberOut = "group_decrease" //退群
UpFile = "group_upload" //上传文件
ChangeGroupAdmin = "group_admin" //管理员变动
GroupBan = "group_ban" //群禁言
FriendAdd = "friend_add" //好友添加
GroupRecall = "group_recall" //群消息撤回
FriendRecall = "friend_recall" //好友消息撤回
Poke = "notify" //群内戳一戳
LuckyKing = "lucky_king" //群红包运气王
Notify = "notify" //群成员荣誉变更
)
8 changes: 4 additions & 4 deletions rosm/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ type Ctx struct {
BotType string
Message any //解析后的原始消息
Being *Being //常用消息解析,需实现
on bool //插件是否在本群开启
on bool //插件是否在本群开启
}

// 常用数据
Expand All @@ -16,7 +16,7 @@ type Being struct {
RoomName string //房间名称,存在上级则存放上级名称
User *UserData //触发事件者信息
ATList []string //at的id列表
MsgID []string //用于reply,存放消息id,reply的其他需要参数写在第二位
MsgID string //用于reply,存放消息id
AtMe bool //是否是at机器人触发的事件
Word string //接收的用户发送的信息,进行了首位的空格切割
Rex []string //如果有正则匹配,这里存放匹配结果
Expand All @@ -25,6 +25,6 @@ type Being struct {

// 触发者信息
type UserData struct {
Name string
ID string
Name string
ID string
}
33 changes: 29 additions & 4 deletions rosm/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,22 @@ import (
"os"
"regexp"
"sort"
"sync"
"time"

"github.com/FloatTech/floatbox/file"
"github.com/FloatTech/ttl"
"github.com/lianhong2758/RosmBot-MUL/message"
"github.com/lianhong2758/RosmBot-MUL/tool/rate"
log "github.com/sirupsen/logrus"
)

var (
//用于映射发送的消息id到触发id
MessagesMapCache = ttl.NewCache[string, []string](time.Minute * 5)
MessagesMu = sync.Mutex{}
)

type (
// Rule filter the event
Rule = func(ctx *Ctx) bool
Expand Down Expand Up @@ -49,7 +58,7 @@ var (
RegexpMatch = []*Matcher{} //Rex

//事件触发
EventMatch = map[int][]*Matcher{} //事件触发
EventMatch = map[EventType][]*Matcher{} //事件触发
)

// 注册插件
Expand Down Expand Up @@ -94,10 +103,12 @@ func (p *PluginData) AddRex(rex string) *Matcher {
}

// 其他事件匹配器
func (p *PluginData) AddEvent(types int) *Matcher {
func (p *PluginData) AddEvent(types ...EventType) *Matcher {
m := new(Matcher)
m.block = false
EventMatch[types] = append(EventMatch[types], m)
for _, v := range types {
EventMatch[v] = append(EventMatch[v], m)
}
p.Matchers = append(p.Matchers, m)
m.PluginNode = p
return m
Expand Down Expand Up @@ -166,8 +177,22 @@ func (m *Matcher) Limit(limiterfn func(*Ctx) *rate.Limiter, postfn ...func(*Ctx)

// 快捷发送消息
func (ctx *Ctx) Send(m ...message.MessageSegment) H {
return ctx.Bot.BotSend(ctx, m...)
MessagesMu.Lock()
defer MessagesMu.Unlock()
h := ctx.Bot.BotSend(ctx, m...)
if h["id"] != "" {
MessagesMapCache.Set(ctx.Being.MsgID, append([]string{h["id"]}, MessagesMapCache.Get(ctx.Being.MsgID)...))
}
return h
}

// 通过记录的回复id查找触发id
func GetMessageIDFormMapCache(id string) []string {
MessagesMu.Lock()
defer MessagesMu.Unlock()
return MessagesMapCache.Get(id)
}

func Display() {
log.Println(WordMatch)
log.Println(RegexpMatch)
Expand Down
4 changes: 2 additions & 2 deletions rosm/plugin_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

// 匹配事件
func (ctx *Ctx) RunEvent(types int) (block bool) {
func (ctx *Ctx) RunEvent(types EventType) (block bool) {
defer func() {
if pa := recover(); pa != nil {
log.Errorf("[rosm] RunEvent Err: %v\n%v", pa, tool.BytesToString(debug.Stack()))
Expand All @@ -23,7 +23,7 @@ func (ctx *Ctx) RunEvent(types int) (block bool) {
for _, m := range EventMatch[types] {
if m.RulePass(ctx) {
m.handler(ctx)
log.Debugf("调用插件: %s - 类型: %d", m.PluginNode.Name, types)
log.Debugf("调用插件: %s - 类型: %s", m.PluginNode.Name, types)
return m.block
}
}
Expand Down
8 changes: 4 additions & 4 deletions server/ob11/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@ import (
// https://github.com/botuniverse/onebot-11/blob/master/api/public.md#delete_msg-%E6%92%A4%E5%9B%9E%E6%B6%88%E6%81%AF
//
//nolint:interfacer
func DeleteMessage(ctx *rosm.Ctx, messageID ...string) {
func DeleteMessage(ctx *rosm.Ctx, messageID string) {
CallAction(ctx, "delete_msg", zero.Params{
"message_id": messageID[0],
"message_id": messageID,
})
}

// GetMessage 获取消息
// https://github.com/botuniverse/onebot-11/blob/master/api/public.md#get_msg-%E8%8E%B7%E5%8F%96%E6%B6%88%E6%81%AF
//
//nolint:interfacer
func GetMessage(ctx *rosm.Ctx, messageID ...string) zero.Message {
func GetMessage(ctx *rosm.Ctx, messageID string) zero.Message {
rsp := CallAction(ctx, "get_msg", zero.Params{
"message_id": messageID[0],
"message_id": messageID,
}).Data
m := zero.Message{
Elements: message.ParseMessage(tool.StringToBytes(rsp.Get("message").Raw)),
Expand Down
2 changes: 1 addition & 1 deletion server/ob11/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func MakeMsgContent(ctx *rosm.Ctx, msg ...message.MessageSegment) message.Messag
msg[k].Data = rosm.H{"file": ImageAnalysis(message.Data["file"])}
case "replyuser":
msg[k].Type = "reply"
msg[k].Data = rosm.H{"id": ctx.Being.MsgID[0]}
msg[k].Data = rosm.H{"id": ctx.Being.MsgID}
case "link":
msg[k].Type = "text"
msg[k].Data = rosm.H{"text": fmt.Sprintf("%s:\n%s", message.Data["text"], message.Data["url"])}
Expand Down
13 changes: 7 additions & 6 deletions server/ob11/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (c *Config) process(e *zero.Event) {
ID: tool.Int64ToString(e.Sender.ID),
// PortraitURI: u.User.PortraitURI,
},
MsgID: []string{tool.BytesToString(e.RawMessageID)},
MsgID: tool.BytesToString(e.RawMessageID),
Def: map[string]any{},
},
Message: e,
Expand All @@ -55,7 +55,7 @@ func (c *Config) process(e *zero.Event) {
ID: tool.Int64ToString(e.Sender.ID),
// PortraitURI: u.User.PortraitURI,
},
MsgID: []string{tool.BytesToString(e.RawMessageID)},
MsgID: tool.BytesToString(e.RawMessageID),
Def: map[string]any{},
},
Message: e,
Expand All @@ -74,22 +74,23 @@ func (c *Config) process(e *zero.Event) {

// 通知事件
case "notice":
//https://github.com/botuniverse/onebot-11/blob/master/event/notice.md
ctx := &rosm.Ctx{
BotType: "ob11",
Being: &rosm.Being{
RoomID2: e.ChannelID,
RoomID: tool.Int64ToString(e.GroupID) + e.GuildID,
User: &rosm.UserData{
// ID: tool.Int64ToString(e.Sender.ID),
// Name: e.Sender.NickName,
User: &rosm.UserData{
ID: tool.Int64ToString(e.UserID),
},
Def: map[string]any{},
},
Message: e,
Bot: c,
}
log.Debug(ctx)
//ctx.RunEvent()
ctx.RunEvent(rosm.EventType(ctx.Message.(*zero.Event).NoticeType))
case "request": //好有请求
default:
}
}
Expand Down
4 changes: 2 additions & 2 deletions server/qq/qqmsg/make.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func GuildMsgContent(ctx *rosm.Ctx, msg ...message.MessageSegment) *Content {
case "reply":
cnt.Reference = &ReferenceS{ID: message.Data["id"], NeedError: true}
case "replyuser":
cnt.Reference = &ReferenceS{ID: ctx.Being.MsgID[0], NeedError: true}
cnt.Reference = &ReferenceS{ID: ctx.Being.MsgID, NeedError: true}
}
}
cnt.Text=tool.HideURL(cnt.Text)
Expand Down Expand Up @@ -76,7 +76,7 @@ func GroupMsgContent(ctx *rosm.Ctx, msg ...message.MessageSegment) *Content {
case "reply":
cnt.Reference = &ReferenceS{ID: message.Data["id"], NeedError: true}
case "replyuser":
cnt.Reference = &ReferenceS{ID: ctx.Being.MsgID[0], NeedError: true}
cnt.Reference = &ReferenceS{ID: ctx.Being.MsgID, NeedError: true}
}
}
cnt.Text=tool.HideURL(cnt.Text)
Expand Down
10 changes: 5 additions & 5 deletions server/qq/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (c *Config) process(playload *WebsocketPayload) {
User: &rosm.UserData{
ID: raw.Author.UserOpenid,
},
MsgID: []string{raw.ID},
MsgID: raw.ID,
Def: H{"type": playload.T, "id": raw.ID},
},
}
Expand Down Expand Up @@ -57,7 +57,7 @@ func (c *Config) process(playload *WebsocketPayload) {
ID: raw.Author.ID,
Name: raw.Author.ID[len(raw.Author.ID)-8:],
},
MsgID: []string{raw.ID},
MsgID: raw.ID,
Def: H{"type": playload.T, "id": raw.ID},
},
}
Expand Down Expand Up @@ -91,10 +91,10 @@ func (c *Config) process(playload *WebsocketPayload) {
RoomID2: raw.GuildID,
ATList: at,
User: &rosm.UserData{
Name: raw.Author.Username,
ID: raw.Author.ID,
Name: raw.Author.Username,
ID: raw.Author.ID,
},
MsgID: []string{raw.ID},
MsgID: raw.ID,
Def: H{"type": playload.T, "id": raw.ID},
},
}
Expand Down
2 changes: 1 addition & 1 deletion tool/tool.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func SplitTypeAndString(value string) (types, string1 string) {
}

// 等待其他init加载完毕
func WaitInit() {
func WaitWhile() {
time.Sleep(time.Second * 2)
}

Expand Down

0 comments on commit b5723f8

Please sign in to comment.