Skip to content

Commit e472d10

Browse files
committed
chore: set admin commands for group in admin db
1 parent d4dbba3 commit e472d10

File tree

4 files changed

+108
-40
lines changed

4 files changed

+108
-40
lines changed

cmd/version.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
)
88

99
const (
10-
Version string = "0.7.15"
10+
Version string = "0.7.16"
1111
)
1212

1313
var VersionCmd = &cobra.Command{

dao/admin.go

+13
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,16 @@ func DeleteAdminByUserID(ctx context.Context, userID int64) (*mongo.DeleteResult
4444
func UpdateAdmin(ctx context.Context, admin *model.AdminModel) (*mongo.UpdateResult, error) {
4545
return adminCollection.ReplaceOne(ctx, bson.M{"user_id": admin.UserID}, admin)
4646
}
47+
48+
func GetAdmins(ctx context.Context) ([]model.AdminModel, error) {
49+
cursor, err := adminCollection.Find(ctx, bson.M{})
50+
if err != nil {
51+
return nil, err
52+
}
53+
var admins []model.AdminModel
54+
err = cursor.All(ctx, &admins)
55+
if err != nil {
56+
return nil, err
57+
}
58+
return admins, nil
59+
}

service/user.go

+28
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,31 @@ func CreateOrUpdateAdmin(ctx context.Context, userID int64, permissions []types.
7575
_, err = dao.UpdateAdmin(ctx, admin)
7676
return err
7777
}
78+
79+
func GetAdminUserIDs(ctx context.Context) ([]int64, error) {
80+
admins, err := dao.GetAdmins(ctx)
81+
if err != nil {
82+
return nil, err
83+
}
84+
var userIDs []int64
85+
for _, admin := range admins {
86+
if admin.UserID > 0 {
87+
userIDs = append(userIDs, admin.UserID)
88+
}
89+
}
90+
return userIDs, nil
91+
}
92+
93+
func GetAdminGroupIDs(ctx context.Context) ([]int64, error) {
94+
admins, err := dao.GetAdmins(ctx)
95+
if err != nil {
96+
return nil, err
97+
}
98+
var groupIDs []int64
99+
for _, admin := range admins {
100+
if admin.UserID < 0 {
101+
groupIDs = append(groupIDs, admin.UserID)
102+
}
103+
}
104+
return groupIDs, nil
105+
}

telegram/telegram.go

+66-39
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package telegram
22

33
import (
44
"ManyACG/config"
5+
"ManyACG/service"
6+
"context"
57
"os"
68

79
. "ManyACG/logger"
@@ -17,35 +19,8 @@ var (
1719
GroupChatID telego.ChatID // 附属群组
1820
)
1921

20-
func InitBot() {
21-
var err error
22-
Bot, err = telego.NewBot(
23-
config.Cfg.Telegram.Token,
24-
telego.WithDefaultLogger(false, true),
25-
telego.WithAPIServer(config.Cfg.Telegram.APIURL),
26-
)
27-
if err != nil {
28-
Logger.Fatalf("Error when creating bot: %s", err)
29-
os.Exit(1)
30-
}
31-
if config.Cfg.Telegram.Username != "" {
32-
ChannelChatID = telegoutil.Username(config.Cfg.Telegram.Username)
33-
} else {
34-
ChannelChatID = telegoutil.ID(config.Cfg.Telegram.ChatID)
35-
}
36-
37-
if config.Cfg.Telegram.GroupID != 0 {
38-
GroupChatID = telegoutil.ID(config.Cfg.Telegram.GroupID)
39-
}
40-
41-
me, err := Bot.GetMe()
42-
if err != nil {
43-
Logger.Errorf("Error when getting bot info: %s", err)
44-
os.Exit(1)
45-
}
46-
BotUsername = me.Username
47-
48-
commonCommands := []telego.BotCommand{
22+
var (
23+
CommonCommands = []telego.BotCommand{
4924
{
5025
Command: "start",
5126
Description: "开始涩涩",
@@ -76,12 +51,7 @@ func InitBot() {
7651
},
7752
}
7853

79-
Bot.SetMyCommands(&telego.SetMyCommandsParams{
80-
Commands: commonCommands,
81-
Scope: &telego.BotCommandScopeDefault{Type: telego.ScopeTypeDefault},
82-
})
83-
84-
adminCommands := []telego.BotCommand{
54+
AdminCommands = []telego.BotCommand{
8555
{
8656
Command: "set_admin",
8757
Description: "设置管理员",
@@ -119,12 +89,52 @@ func InitBot() {
11989
Description: "处理无哈希的图片",
12090
},
12191
}
92+
)
12293

123-
adminCommands = append(commonCommands, adminCommands...)
94+
func InitBot() {
95+
var err error
96+
Bot, err = telego.NewBot(
97+
config.Cfg.Telegram.Token,
98+
telego.WithDefaultLogger(false, true),
99+
telego.WithAPIServer(config.Cfg.Telegram.APIURL),
100+
)
101+
if err != nil {
102+
Logger.Fatalf("Error when creating bot: %s", err)
103+
os.Exit(1)
104+
}
105+
if config.Cfg.Telegram.Username != "" {
106+
ChannelChatID = telegoutil.Username(config.Cfg.Telegram.Username)
107+
} else {
108+
ChannelChatID = telegoutil.ID(config.Cfg.Telegram.ChatID)
109+
}
110+
111+
if config.Cfg.Telegram.GroupID != 0 {
112+
GroupChatID = telegoutil.ID(config.Cfg.Telegram.GroupID)
113+
}
114+
115+
me, err := Bot.GetMe()
116+
if err != nil {
117+
Logger.Errorf("Error when getting bot info: %s", err)
118+
os.Exit(1)
119+
}
120+
BotUsername = me.Username
124121

125-
for _, adminID := range config.Cfg.Telegram.Admins {
122+
Bot.SetMyCommands(&telego.SetMyCommandsParams{
123+
Commands: CommonCommands,
124+
Scope: &telego.BotCommandScopeDefault{Type: telego.ScopeTypeDefault},
125+
})
126+
127+
allCommands := append(CommonCommands, AdminCommands...)
128+
129+
adminUserIDs, err := service.GetAdminUserIDs(context.TODO())
130+
if err != nil {
131+
Logger.Warnf("Error when getting admin user IDs: %s", err)
132+
return
133+
}
134+
135+
for _, adminID := range adminUserIDs {
126136
Bot.SetMyCommands(&telego.SetMyCommandsParams{
127-
Commands: adminCommands,
137+
Commands: allCommands,
128138
Scope: &telego.BotCommandScopeChat{
129139
Type: telego.ScopeTypeChat,
130140
ChatID: telegoutil.ID(adminID),
@@ -134,12 +144,29 @@ func InitBot() {
134144
continue
135145
}
136146
Bot.SetMyCommands(&telego.SetMyCommandsParams{
137-
Commands: adminCommands,
147+
Commands: allCommands,
138148
Scope: &telego.BotCommandScopeChatMember{
139149
Type: telego.ScopeTypeChat,
140150
ChatID: GroupChatID,
141151
UserID: adminID,
142152
},
143153
})
144154
}
155+
156+
adminGroupIDs, err := service.GetAdminGroupIDs(context.TODO())
157+
if err != nil {
158+
Logger.Warnf("Error when getting admin group IDs: %s", err)
159+
return
160+
}
161+
162+
for _, adminID := range adminGroupIDs {
163+
Bot.SetMyCommands(&telego.SetMyCommandsParams{
164+
Commands: allCommands,
165+
Scope: &telego.BotCommandScopeChat{
166+
Type: telego.ScopeTypeChat,
167+
ChatID: telegoutil.ID(adminID),
168+
},
169+
})
170+
}
171+
145172
}

0 commit comments

Comments
 (0)