Skip to content

Commit 308189b

Browse files
committed
✨ feat(消息开关): group and private
1 parent 5ec6bf9 commit 308189b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+7048
-1
lines changed

bot/groupmessage.go

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ import (
1010
)
1111

1212
func BotGroupMessageEvent(client *client.QQClient, event *message.GroupMessage) {
13+
if !database.OpenHave(event.GroupUin, "group") {
14+
return
15+
}
16+
1317
messageText := event.ToString()
1418
if messageText == "dingbot" {
1519
client.SendGroupMessage(event.GroupUin, []message.IMessageElement{message.NewText("dingbot version:" + appconfig.Version + "\nwebsite:https://github.com/dingdinglz/dingbot")})

bot/privatemessage.go

+4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ import (
99
)
1010

1111
func BotPrivateMessageEvent(client *client.QQClient, event *message.PrivateMessage) {
12+
if !database.OpenHave(event.Sender.Uin, "private") {
13+
return
14+
}
15+
1216
messageText := event.ToString()
1317

1418
{

database/init.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@ func DatabaseInit() {
1616
if err != nil {
1717
tool.ErrorOut("database", "open error", err)
1818
}
19-
DB.AutoMigrate(&KeyWordTable{})
19+
DB.AutoMigrate(&KeyWordTable{}, &OpenTable{})
2020
}

database/open.go

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package database
2+
3+
func OpenHave(Uin uint32, TypeName string) bool {
4+
var i int64
5+
DB.Model(&OpenTable{}).Where("typename = ?", TypeName).Where("uin = ?", Uin).Count(&i)
6+
return i != 0
7+
}
8+
9+
func OpenAdd(Uin uint32, TypeName string) {
10+
if OpenHave(Uin, TypeName) {
11+
return
12+
}
13+
var i OpenTable
14+
i.Uin = Uin
15+
i.Typename = TypeName
16+
DB.Create(&i)
17+
}
18+
19+
func OpenDelete(Uin uint32, TypeName string) {
20+
DB.Delete(&OpenTable{}, "uin = ? AND typename = ?", Uin, TypeName)
21+
}

database/tables.go

+9
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,12 @@ type KeyWordTable struct {
99
func (KeyWordTable) TableName() string {
1010
return "keyword"
1111
}
12+
13+
type OpenTable struct {
14+
Uin uint32
15+
Typename string
16+
}
17+
18+
func (OpenTable) TableName() string {
19+
return "open"
20+
}

route/add.go

+9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package route
22

33
import (
44
"github.com/dingdinglz/dingbot/database"
5+
"github.com/dingdinglz/dingbot/tool"
56
"github.com/gofiber/fiber/v2"
67
)
78

@@ -16,3 +17,11 @@ func AddKeywordRoute(c *fiber.Ctx) error {
1617
database.KeyWordCreate(c.FormValue("key"), c.FormValue("text"), p)
1718
return JsonMessage(c, 0, "ok")
1819
}
20+
21+
func AddOpenRoute(c *fiber.Ctx) error {
22+
if c.FormValue("type", "") == "" || c.FormValue("uin", "") == "" {
23+
return JsonMessage(c, -1, "none")
24+
}
25+
database.OpenAdd(tool.StringToUint32(c.FormValue("uin", "")), c.FormValue("type", ""))
26+
return JsonMessage(c, 0, "ok")
27+
}

route/delete.go

+9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package route
22

33
import (
44
"github.com/dingdinglz/dingbot/database"
5+
"github.com/dingdinglz/dingbot/tool"
56
"github.com/gofiber/fiber/v2"
67
)
78

@@ -12,3 +13,11 @@ func DeleteKeywordRoute(c *fiber.Ctx) error {
1213
database.KeyWordDelete(c.FormValue("key"))
1314
return JsonMessage(c, 0, "ok")
1415
}
16+
17+
func DeleteOpenRoute(c *fiber.Ctx) error {
18+
if c.FormValue("type", "") == "" || c.FormValue("uin", "") == "" {
19+
return JsonMessage(c, -1, "none")
20+
}
21+
database.OpenDelete(tool.StringToUint32(c.FormValue("uin", "")), c.FormValue("type", ""))
22+
return JsonMessage(c, 0, "ok")
23+
}

route/formal.go

+60
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package route
22

33
import (
4+
"github.com/dingdinglz/dingbot/bot"
45
"github.com/dingdinglz/dingbot/database"
56
"github.com/gofiber/fiber/v2"
67
)
@@ -15,3 +16,62 @@ func KeyWordRoute(c *fiber.Ctx) error {
1516
pageMap["Keys"] = database.KeyWordGetAll()
1617
return c.Render("key", pageMap, "layout")
1718
}
19+
20+
func PluginEditRoute(c *fiber.Ctx) error {
21+
pageMap := GenerateRenderMap("plugin-edit")
22+
return c.Render("plugin-edit", pageMap, "layout")
23+
}
24+
25+
type qqGroup struct {
26+
Name string
27+
Uin uint32
28+
MemberCount uint32
29+
}
30+
31+
func GroupOpenRoute(c *fiber.Ctx) error {
32+
if bot.DingQQBot == nil {
33+
return c.Redirect("/")
34+
}
35+
if !bot.DingQQBot.Online.Load() {
36+
return c.Redirect("/")
37+
}
38+
groups, _ := bot.DingQQBot.GetAllGroupsInfo()
39+
var qqgs []qqGroup
40+
for _, i := range groups {
41+
var _cnt qqGroup
42+
_cnt.Name = i.GroupName
43+
_cnt.Uin = i.GroupUin
44+
_cnt.MemberCount = i.MemberCount
45+
qqgs = append(qqgs, _cnt)
46+
}
47+
pageMap := GenerateRenderMap("group")
48+
pageMap["GroupList"] = qqgs
49+
return c.Render("group", pageMap, "layout")
50+
}
51+
52+
type qqFriend struct {
53+
Name string
54+
Uin uint32
55+
Avatar string
56+
}
57+
58+
func PrivateOpenRoute(c *fiber.Ctx) error {
59+
if bot.DingQQBot == nil {
60+
return c.Redirect("/")
61+
}
62+
if !bot.DingQQBot.Online.Load() {
63+
return c.Redirect("/")
64+
}
65+
friends, _ := bot.DingQQBot.GetFriendsData()
66+
var qqfs []qqFriend
67+
for _, i := range friends {
68+
var _cnt qqFriend
69+
_cnt.Name = i.Nickname
70+
_cnt.Uin = i.Uin
71+
_cnt.Avatar = i.Avatar
72+
qqfs = append(qqfs, _cnt)
73+
}
74+
pageMap := GenerateRenderMap("private")
75+
pageMap["FriendList"] = qqfs
76+
return c.Render("private", pageMap, "layout")
77+
}

server.go

+9
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"strconv"
88

99
"github.com/dingdinglz/dingbot/appconfig"
10+
"github.com/dingdinglz/dingbot/database"
1011
"github.com/dingdinglz/dingbot/route"
1112
"github.com/dingdinglz/dingbot/tool"
1213
"github.com/gofiber/fiber/v2"
@@ -19,6 +20,9 @@ func ServerInit() {
1920
rootPath, _ := os.Getwd()
2021
tEngine := html.New(filepath.Join(rootPath, "web", "index"), ".html")
2122
tEngine.ShouldReload = true
23+
tEngine.AddFunc("Open", func(Uin uint32, Typename string) bool {
24+
return database.OpenHave(Uin, Typename)
25+
})
2226
appconfig.MainServer = fiber.New(fiber.Config{Views: tEngine})
2327
appconfig.MainServer.Use(recover.New(), logger.New())
2428
appconfig.MainServer.Static("/", filepath.Join(rootPath, "web", "public"))
@@ -47,6 +51,9 @@ func ServerInitRun() {
4751
func ServerCommonRun() {
4852
appconfig.MainServer.Get("/", route.IndexRoute)
4953
appconfig.MainServer.Get("/key", route.KeyWordRoute)
54+
appconfig.MainServer.Get("/plugin-edit/:name", route.PluginEditRoute)
55+
appconfig.MainServer.Get("/group", route.GroupOpenRoute)
56+
appconfig.MainServer.Get("/private", route.PrivateOpenRoute)
5057

5158
apiRoute := appconfig.MainServer.Group("/api")
5259

@@ -63,9 +70,11 @@ func ServerCommonRun() {
6370

6471
apiAddRoute := apiRoute.Group("/add")
6572
apiAddRoute.Post("/keyword", route.AddKeywordRoute)
73+
apiAddRoute.Post("/open", route.AddOpenRoute)
6674

6775
apiDeleteRoute := apiRoute.Group("/delete")
6876
apiDeleteRoute.Post("/keyword", route.DeleteKeywordRoute)
77+
apiDeleteRoute.Post("/open", route.DeleteOpenRoute)
6978

7079
err := appconfig.MainServer.Listen("0.0.0.0:" + strconv.Itoa(appconfig.AppConfigVar.Port))
7180
if err != nil {

tool/conv.go

+8
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,11 @@ func StringToInt(i string) int {
99
}
1010
return cnt
1111
}
12+
13+
func StringToUint32(s string) uint32 {
14+
u64, err := strconv.ParseUint(s, 10, 32)
15+
if err != nil {
16+
return 0
17+
}
18+
return uint32(u64)
19+
}

web/index/group.html

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<script>
2+
function AddOpen(uin)
3+
{
4+
$.post("/api/add/open",{uin:uin,type:"group"},function(){
5+
location.reload();
6+
});
7+
}
8+
function DeleteOpen(uin)
9+
{
10+
$.post("/api/delete/open",{uin:uin,type:"group"},function(){
11+
location.reload();
12+
});
13+
}
14+
</script>
15+
<table class="layui-table">
16+
<thead>
17+
<tr>
18+
<th>群人数</th>
19+
<th>群聊名</th>
20+
<th>群号</th>
21+
<th>操作</th>
22+
</tr>
23+
</thead>
24+
<tbody>
25+
{{range .GroupList}}
26+
<tr>
27+
<td>
28+
{{.MemberCount}}
29+
</td>
30+
<td>{{.Name}}</td>
31+
<td>{{.Uin}}</td>
32+
{{if Open .Uin "group"}}
33+
<td><button class="layui-btn layui-btn-danger" onclick="DeleteOpen('{{.Uin}}')">关闭</button></td>
34+
{{else}}
35+
<td>
36+
<button class="layui-btn layui-btn-success" onclick="AddOpen('{{.Uin}}')">开启</button>
37+
</td>
38+
{{end}}
39+
</tr>
40+
{{end}}
41+
</tbody>
42+
</table>

web/index/layout.html

+13
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,19 @@
3737
<dl class="layui-nav-child">
3838
<dd id="page-key"><a href="/key">关键词回复</a></dd>
3939
</dl>
40+
<dl class="layui-nav-child">
41+
<dd id="page-group"><a href="/group">群接收开关</a></dd>
42+
</dl>
43+
<dl class="layui-nav-child">
44+
<dd id="page-private"><a href="/private">好友接收开关</a></dd>
45+
</dl>
46+
</li>
47+
<li class="layui-nav-item layui-nav-itemed">
48+
<a class="" href="javascript:;">插件</a>
49+
<dl class="layui-nav-child">
50+
<dd id="page-plugin"><a href="/plugin">插件列表</a></dd>
51+
<dd id="page-plugin-edit"><a href="/plugin-edit">插件编辑器</a></dd>
52+
</dl>
4053
</li>
4154
<li class="layui-nav-item"><a href="https://dingdinglz.github.io/p/dingbot-v2/">dingbot介绍</a></li>
4255
</ul>

0 commit comments

Comments
 (0)