@@ -2,6 +2,8 @@ package telegram
2
2
3
3
import (
4
4
"ManyACG/config"
5
+ "ManyACG/service"
6
+ "context"
5
7
"os"
6
8
7
9
. "ManyACG/logger"
17
19
GroupChatID telego.ChatID // 附属群组
18
20
)
19
21
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 {
49
24
{
50
25
Command : "start" ,
51
26
Description : "开始涩涩" ,
@@ -76,12 +51,7 @@ func InitBot() {
76
51
},
77
52
}
78
53
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 {
85
55
{
86
56
Command : "set_admin" ,
87
57
Description : "设置管理员" ,
@@ -119,12 +89,52 @@ func InitBot() {
119
89
Description : "处理无哈希的图片" ,
120
90
},
121
91
}
92
+ )
122
93
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
124
121
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 {
126
136
Bot .SetMyCommands (& telego.SetMyCommandsParams {
127
- Commands : adminCommands ,
137
+ Commands : allCommands ,
128
138
Scope : & telego.BotCommandScopeChat {
129
139
Type : telego .ScopeTypeChat ,
130
140
ChatID : telegoutil .ID (adminID ),
@@ -134,12 +144,29 @@ func InitBot() {
134
144
continue
135
145
}
136
146
Bot .SetMyCommands (& telego.SetMyCommandsParams {
137
- Commands : adminCommands ,
147
+ Commands : allCommands ,
138
148
Scope : & telego.BotCommandScopeChatMember {
139
149
Type : telego .ScopeTypeChat ,
140
150
ChatID : GroupChatID ,
141
151
UserID : adminID ,
142
152
},
143
153
})
144
154
}
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
+
145
172
}
0 commit comments