Skip to content

Commit 58c4eb4

Browse files
committed
member join/leave
1 parent 8b87418 commit 58c4eb4

File tree

3 files changed

+90
-7
lines changed

3 files changed

+90
-7
lines changed

main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ func main() {
5151
log.Infof("加载上报插件 Report")
5252
plugin.AddPrivateMessagePlugin(plugins.ReportPrivateMessage)
5353
plugin.AddGroupMessagePlugin(plugins.ReportGroupMessage)
54+
plugin.AddMemberJoinGroupPlugin(plugins.ReportMemberJoin)
55+
plugin.AddMemberLeaveGroupPlugin(plugins.ReportMemberLeave)
5456

5557
plugin.Serve(cli)
5658
log.Infof("插件加载完成")

pkg/plugin/plugin.go

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ import (
77
)
88

99
type (
10-
PrivateMessagePlugin = func(*client.QQClient, *message.PrivateMessage) int32
11-
GroupMessagePlugin = func(*client.QQClient, *message.GroupMessage) int32
10+
PrivateMessagePlugin = func(*client.QQClient, *message.PrivateMessage) int32
11+
GroupMessagePlugin = func(*client.QQClient, *message.GroupMessage) int32
12+
MemberJoinGroupPlugin = func(*client.QQClient, *client.MemberJoinGroupEvent) int32
13+
MemberLeaveGroupPlugin = func(*client.QQClient, *client.MemberLeaveGroupEvent) int32
1214
)
1315

1416
const (
@@ -18,11 +20,14 @@ const (
1820

1921
var PrivateMessagePluginList = make([]PrivateMessagePlugin, 0)
2022
var GroupMessagePluginList = make([]GroupMessagePlugin, 0)
23+
var MemberJoinGroupPluginList = make([]MemberJoinGroupPlugin, 0)
24+
var MemberLeaveGroupPluginList = make([]MemberLeaveGroupPlugin, 0)
2125

2226
func Serve(cli *client.QQClient) {
2327
cli.OnPrivateMessage(handlePrivateMessage)
2428
cli.OnGroupMessage(handleGroupMessage)
25-
29+
cli.OnGroupMemberJoined(handleMemberJoinGroup)
30+
cli.OnGroupMemberLeaved(handleMemberLeaveGroup)
2631
}
2732

2833
// 添加私聊消息插件
@@ -35,20 +40,50 @@ func AddGroupMessagePlugin(plugin GroupMessagePlugin) {
3540
GroupMessagePluginList = append(GroupMessagePluginList, plugin)
3641
}
3742

38-
func handlePrivateMessage(cli *client.QQClient, msg *message.PrivateMessage) {
43+
// 添加群成员加入插件
44+
func AddMemberJoinGroupPlugin(plugin MemberJoinGroupPlugin) {
45+
MemberJoinGroupPluginList = append(MemberJoinGroupPluginList, plugin)
46+
}
47+
48+
// 添加群成员离开插件
49+
func AddMemberLeaveGroupPlugin(plugin MemberLeaveGroupPlugin) {
50+
MemberLeaveGroupPluginList = append(MemberLeaveGroupPluginList, plugin)
51+
}
52+
53+
func handlePrivateMessage(cli *client.QQClient, event *message.PrivateMessage) {
3954
SafeGo(func() {
4055
for _, plugin := range PrivateMessagePluginList {
41-
if result := plugin(cli, msg); result == MessageBlock {
56+
if result := plugin(cli, event); result == MessageBlock {
4257
break
4358
}
4459
}
4560
})
4661
}
4762

48-
func handleGroupMessage(cli *client.QQClient, msg *message.GroupMessage) {
63+
func handleGroupMessage(cli *client.QQClient, event *message.GroupMessage) {
4964
SafeGo(func() {
5065
for _, plugin := range GroupMessagePluginList {
51-
if result := plugin(cli, msg); result == MessageBlock {
66+
if result := plugin(cli, event); result == MessageBlock {
67+
break
68+
}
69+
}
70+
})
71+
}
72+
73+
func handleMemberJoinGroup(cli *client.QQClient, event *client.MemberJoinGroupEvent) {
74+
SafeGo(func() {
75+
for _, plugin := range MemberJoinGroupPluginList {
76+
if result := plugin(cli, event); result == MessageBlock {
77+
break
78+
}
79+
}
80+
})
81+
}
82+
83+
func handleMemberLeaveGroup(cli *client.QQClient, event *client.MemberLeaveGroupEvent) {
84+
SafeGo(func() {
85+
for _, plugin := range MemberLeaveGroupPluginList {
86+
if result := plugin(cli, event); result == MessageBlock {
5287
break
5388
}
5489
}

service/plugins/report.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,49 @@ func ReportGroupMessage(cli *client.QQClient, event *message.GroupMessage) int32
5757
bot.HandleEventFrame(cli, eventProto)
5858
return plugin.MessageIgnore
5959
}
60+
61+
func ReportMemberJoin(cli *client.QQClient, event *client.MemberJoinGroupEvent) int32 {
62+
eventProto := &onebot.Frame{
63+
FrameType: onebot.Frame_TGroupIncreaseNoticeEvent,
64+
}
65+
eventProto.Data = &onebot.Frame_GroupIncreaseNoticeEvent{
66+
GroupIncreaseNoticeEvent: &onebot.GroupIncreaseNoticeEvent{
67+
Time: time.Now().Unix(),
68+
SelfId: cli.Uin,
69+
PostType: "message",
70+
NoticeType: "group_increase",
71+
SubType: "approve",
72+
GroupId: event.Group.Code,
73+
UserId: event.Member.Uin,
74+
},
75+
}
76+
bot.HandleEventFrame(cli, eventProto)
77+
return plugin.MessageIgnore
78+
}
79+
80+
func ReportMemberLeave(cli *client.QQClient, event *client.MemberLeaveGroupEvent) int32 {
81+
eventProto := &onebot.Frame{
82+
FrameType: onebot.Frame_TGroupDecreaseNoticeEvent,
83+
}
84+
subType := "leave"
85+
var operatorId int64 = 0
86+
if event.Operator != nil {
87+
subType = "kick"
88+
operatorId = event.Operator.Uin
89+
}
90+
91+
eventProto.Data = &onebot.Frame_GroupDecreaseNoticeEvent{
92+
GroupDecreaseNoticeEvent: &onebot.GroupDecreaseNoticeEvent{
93+
Time: time.Now().Unix(),
94+
SelfId: cli.Uin,
95+
PostType: "message",
96+
NoticeType: "group_decrease",
97+
SubType: subType,
98+
GroupId: event.Group.Code,
99+
UserId: event.Member.Uin,
100+
OperatorId: operatorId,
101+
},
102+
}
103+
bot.HandleEventFrame(cli, eventProto)
104+
return plugin.MessageIgnore
105+
}

0 commit comments

Comments
 (0)