-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathchat_member_methods.go
More file actions
324 lines (271 loc) · 8.58 KB
/
Copy pathchat_member_methods.go
File metadata and controls
324 lines (271 loc) · 8.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
package botapi
import (
"context"
"github.com/gotd/td/telegram/peers"
"github.com/gotd/td/tg"
)
// resolveChannel resolves a ChatID to its MTProto input channel. Member
// management is only available in supergroups and channels.
func (b *Bot) resolveChannel(ctx context.Context, chat ChatID) (tg.InputChannelClass, error) {
p, err := b.resolvePeer(ctx, chat)
if err != nil {
return nil, err
}
ch, ok := p.(peers.Channel)
if !ok {
return nil, &Error{Code: 400, Description: "Bad Request: method is available only for supergroups and channels"}
}
return ch.InputChannel(), nil
}
// BanChatMemberOption configures a BanChatMember call.
type BanChatMemberOption func(*banConfig)
type banConfig struct {
untilDate int
revokeHistory bool
}
// WithBanUntil sets the Unix time when the user will be unbanned (0 or far in
// the future means forever).
func WithBanUntil(untilDate int) BanChatMemberOption {
return func(c *banConfig) { c.untilDate = untilDate }
}
// WithRevokeMessages deletes all messages from the user being banned.
func WithRevokeMessages() BanChatMemberOption {
return func(c *banConfig) { c.revokeHistory = true }
}
// BanChatMember bans a user from a supergroup or channel. The bot must be an
// administrator with the can_restrict_members right.
func (b *Bot) BanChatMember(ctx context.Context, chat ChatID, userID int64, opts ...BanChatMemberOption) error {
var cfg banConfig
for _, o := range opts {
o(&cfg)
}
channel, err := b.resolveChannel(ctx, chat)
if err != nil {
return err
}
user, err := b.resolveInputUser(ctx, userID)
if err != nil {
return err
}
if _, err := b.raw.ChannelsEditBanned(ctx, &tg.ChannelsEditBannedRequest{
Channel: channel,
Participant: userToInputPeer(user),
BannedRights: tg.ChatBannedRights{
ViewMessages: true,
SendMessages: true,
SendMedia: true,
SendStickers: true,
SendGifs: true,
SendGames: true,
SendInline: true,
EmbedLinks: true,
UntilDate: cfg.untilDate,
},
}); err != nil {
return asAPIError(err)
}
if cfg.revokeHistory {
if _, err := b.raw.ChannelsDeleteParticipantHistory(ctx, &tg.ChannelsDeleteParticipantHistoryRequest{
Channel: channel,
Participant: userToInputPeer(user),
}); err != nil {
return asAPIError(err)
}
}
return nil
}
// UnbanChatMember removes a user's ban from a supergroup or channel, clearing
// all restrictions. It does not re-add the user to the chat.
func (b *Bot) UnbanChatMember(ctx context.Context, chat ChatID, userID int64) error {
channel, err := b.resolveChannel(ctx, chat)
if err != nil {
return err
}
user, err := b.resolveInputUser(ctx, userID)
if err != nil {
return err
}
if _, err := b.raw.ChannelsEditBanned(ctx, &tg.ChannelsEditBannedRequest{
Channel: channel,
Participant: userToInputPeer(user),
BannedRights: tg.ChatBannedRights{}, // empty rights clear the ban
}); err != nil {
return asAPIError(err)
}
return nil
}
// RestrictChatMember restricts a user in a supergroup. permissions is an
// allow-list; denied actions are turned into MTProto banned rights. untilDate is
// a Unix time (0 means forever).
func (b *Bot) RestrictChatMember(ctx context.Context, chat ChatID, userID int64, permissions ChatPermissions, untilDate int) error {
channel, err := b.resolveChannel(ctx, chat)
if err != nil {
return err
}
user, err := b.resolveInputUser(ctx, userID)
if err != nil {
return err
}
if _, err := b.raw.ChannelsEditBanned(ctx, &tg.ChannelsEditBannedRequest{
Channel: channel,
Participant: userToInputPeer(user),
BannedRights: permissions.toBannedRights(untilDate),
}); err != nil {
return asAPIError(err)
}
return nil
}
// PromoteChatMember promotes or demotes a user in a supergroup or channel. The
// granted rights are the true fields of rights; pass a zero value to demote.
func (b *Bot) PromoteChatMember(ctx context.Context, chat ChatID, userID int64, rights ChatAdminRights) error {
channel, err := b.resolveChannel(ctx, chat)
if err != nil {
return err
}
user, err := b.resolveInputUser(ctx, userID)
if err != nil {
return err
}
if _, err := b.raw.ChannelsEditAdmin(ctx, &tg.ChannelsEditAdminRequest{
Channel: channel,
UserID: user,
AdminRights: rights.toTg(),
Rank: rights.CustomTitle,
}); err != nil {
return asAPIError(err)
}
return nil
}
// GetChatMember returns information about a member of a supergroup or channel.
func (b *Bot) GetChatMember(ctx context.Context, chat ChatID, userID int64) (ChatMember, error) {
channel, err := b.resolveChannel(ctx, chat)
if err != nil {
return nil, err
}
user, err := b.resolveInputUser(ctx, userID)
if err != nil {
return nil, err
}
res, err := b.raw.ChannelsGetParticipant(ctx, &tg.ChannelsGetParticipantRequest{
Channel: channel,
Participant: userToInputPeer(user),
})
if err != nil {
return nil, asAPIError(err)
}
return chatMemberFromParticipant(res.Participant, usersByID(res.Users)), nil
}
// GetChatAdministrators returns the administrators of a supergroup or channel,
// excluding other bots.
func (b *Bot) GetChatAdministrators(ctx context.Context, chat ChatID) ([]ChatMember, error) {
channel, err := b.resolveChannel(ctx, chat)
if err != nil {
return nil, err
}
res, err := b.raw.ChannelsGetParticipants(ctx, &tg.ChannelsGetParticipantsRequest{
Channel: channel,
Filter: &tg.ChannelParticipantsAdmins{},
Limit: 200,
})
if err != nil {
return nil, asAPIError(err)
}
participants, ok := res.AsModified()
if !ok {
return nil, &Error{Code: 500, Description: "Internal Server Error: unexpected participants response"}
}
users := usersByID(participants.Users)
out := make([]ChatMember, 0, len(participants.Participants))
for _, p := range participants.Participants {
out = append(out, chatMemberFromParticipant(p, users))
}
return out, nil
}
// GetChatMemberCount returns the number of members in a supergroup or channel.
func (b *Bot) GetChatMemberCount(ctx context.Context, chat ChatID) (int, error) {
channel, err := b.resolveChannel(ctx, chat)
if err != nil {
return 0, err
}
res, err := b.raw.ChannelsGetParticipants(ctx, &tg.ChannelsGetParticipantsRequest{
Channel: channel,
Filter: &tg.ChannelParticipantsRecent{},
Limit: 1,
})
if err != nil {
return 0, asAPIError(err)
}
participants, ok := res.AsModified()
if !ok {
return 0, &Error{Code: 500, Description: "Internal Server Error: unexpected participants response"}
}
return participants.Count, nil
}
// userToInputPeer wraps an input user as the input peer the participant-editing
// methods expect.
func userToInputPeer(u tg.InputUserClass) tg.InputPeerClass {
switch v := u.(type) {
case *tg.InputUser:
return &tg.InputPeerUser{UserID: v.UserID, AccessHash: v.AccessHash}
case *tg.InputUserFromMessage:
return &tg.InputPeerUserFromMessage{Peer: v.Peer, MsgID: v.MsgID, UserID: v.UserID}
case *tg.InputUserSelf:
return &tg.InputPeerSelf{}
default:
return &tg.InputPeerEmpty{}
}
}
// SetChatAdministratorCustomTitle sets a custom title (rank) for an
// administrator that the bot has promoted in a supergroup. It preserves the
// administrator's existing rights.
func (b *Bot) SetChatAdministratorCustomTitle(ctx context.Context, chat ChatID, userID int64, customTitle string) error {
channel, err := b.resolveChannel(ctx, chat)
if err != nil {
return err
}
user, err := b.resolveInputUser(ctx, userID)
if err != nil {
return err
}
res, err := b.raw.ChannelsGetParticipant(ctx, &tg.ChannelsGetParticipantRequest{
Channel: channel,
Participant: userToInputPeer(user),
})
if err != nil {
return asAPIError(err)
}
admin, ok := res.Participant.(*tg.ChannelParticipantAdmin)
if !ok {
return &Error{Code: 400, Description: "Bad Request: user is not an administrator"}
}
if _, err := b.raw.ChannelsEditAdmin(ctx, &tg.ChannelsEditAdminRequest{
Channel: channel,
UserID: user,
AdminRights: admin.AdminRights,
Rank: customTitle,
}); err != nil {
return asAPIError(err)
}
return nil
}
// SetChatMemberTag sets a custom tag (rank) for a member of a supergroup or
// channel. An empty tag removes it. The bot must be an administrator with the
// appropriate rights.
func (b *Bot) SetChatMemberTag(ctx context.Context, chat ChatID, userID int64, tag string) error {
peer, err := b.resolveInputPeer(ctx, chat)
if err != nil {
return err
}
user, err := b.resolveInputUser(ctx, userID)
if err != nil {
return err
}
if _, err := b.raw.MessagesEditChatParticipantRank(ctx, &tg.MessagesEditChatParticipantRankRequest{
Peer: peer,
Participant: userToInputPeer(user),
Rank: tag,
}); err != nil {
return asAPIError(err)
}
return nil
}