forked from tucnak/telebot
-
Notifications
You must be signed in to change notification settings - Fork 1
/
admin.go
275 lines (236 loc) · 7.53 KB
/
admin.go
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
package telebot
import (
"encoding/json"
"strconv"
"time"
)
// ChatInviteLink object represents an invite for a chat.
type ChatInviteLink struct {
// The invite link.
InviteLink string `json:"invite_link"`
// The creator of the link.
Creator *User `json:"creator"`
// If the link is primary.
IsPrimary bool `json:"is_primary"`
// If the link is revoked.
IsRevoked bool `json:"is_revoked"`
// (Optional) Point in time when the link will expire, use
// ChatInviteLink.ExpireDate() to get time.Time
ExpireUnixtime int64 `json:"expire_date,omitempty"`
// (Optional) Maximum number of users that can be members of
// the chat simultaneously.
MemberLimit int `json:"member_limit,omitempty"`
}
// ExpireDate returns the moment of the link expiration in local time.
func (c *ChatInviteLink) ExpireDate() time.Time {
return time.Unix(c.ExpireUnixtime, 0)
}
// ChatMemberUpdated object represents changes in the status of a chat member.
type ChatMemberUpdated struct {
// Chat where the user belongs to.
Chat Chat `json:"chat"`
// From which user the action was triggered.
From User `json:"from"`
// Unixtime, use ChatMemberUpdated.Time() to get time.Time
Unixtime int64 `json:"date"`
// Previous information about the chat member.
OldChatMember *ChatMember `json:"old_chat_member"`
// New information about the chat member.
NewChatMember *ChatMember `json:"new_chat_member"`
// (Optional) InviteLink which was used by the user to
// join the chat; for joining by invite link events only.
InviteLink *ChatInviteLink `json:"invite_link"`
}
// Time returns the moment of the change in local time.
func (c *ChatMemberUpdated) Time() time.Time {
return time.Unix(c.Unixtime, 0)
}
// Rights is a list of privileges available to chat members.
type Rights struct {
CanBeEdited bool `json:"can_be_edited"`
CanChangeInfo bool `json:"can_change_info"`
CanPostMessages bool `json:"can_post_messages"`
CanEditMessages bool `json:"can_edit_messages"`
CanDeleteMessages bool `json:"can_delete_messages"`
CanInviteUsers bool `json:"can_invite_users"`
CanRestrictMembers bool `json:"can_restrict_members"`
CanPinMessages bool `json:"can_pin_messages"`
CanPromoteMembers bool `json:"can_promote_members"`
CanSendMessages bool `json:"can_send_messages"`
CanSendMedia bool `json:"can_send_media_messages"`
CanSendPolls bool `json:"can_send_polls"`
CanSendOther bool `json:"can_send_other_messages"`
CanAddPreviews bool `json:"can_add_web_page_previews"`
CanManageVoiceChats bool `json:"can_manage_voice_chats"`
CanManageChat bool `json:"can_manage_chat"`
}
// NoRights is the default Rights{}.
func NoRights() Rights { return Rights{} }
// NoRestrictions should be used when un-restricting or
// un-promoting user.
//
// member.Rights = tb.NoRestrictions()
// bot.Restrict(chat, member)
//
func NoRestrictions() Rights {
return Rights{
CanBeEdited: true,
CanChangeInfo: false,
CanPostMessages: false,
CanEditMessages: false,
CanDeleteMessages: false,
CanInviteUsers: false,
CanRestrictMembers: false,
CanPinMessages: false,
CanPromoteMembers: false,
CanSendMessages: true,
CanSendMedia: true,
CanSendPolls: true,
CanSendOther: true,
CanAddPreviews: true,
CanManageVoiceChats: false,
CanManageChat: false,
}
}
// AdminRights could be used to promote user to admin.
func AdminRights() Rights {
return Rights{
CanBeEdited: true,
CanChangeInfo: true,
CanPostMessages: true,
CanEditMessages: true,
CanDeleteMessages: true,
CanInviteUsers: true,
CanRestrictMembers: true,
CanPinMessages: true,
CanPromoteMembers: true,
CanSendMessages: true,
CanSendMedia: true,
CanSendPolls: true,
CanSendOther: true,
CanAddPreviews: true,
CanManageVoiceChats: true,
CanManageChat: true,
}
}
// Forever is a ExpireUnixtime of "forever" banning.
func Forever() int64 {
return time.Now().Add(367 * 24 * time.Hour).Unix()
}
// Ban will ban user from chat until `member.RestrictedUntil`.
func (b *Bot) Ban(chat *Chat, member *ChatMember, revokeMessages ...bool) error {
params := map[string]string{
"chat_id": chat.Recipient(),
"user_id": member.User.Recipient(),
"until_date": strconv.FormatInt(member.RestrictedUntil, 10),
}
if len(revokeMessages) > 0 {
params["revoke_messages"] = strconv.FormatBool(revokeMessages[0])
}
_, err := b.Raw("kickChatMember", params)
return err
}
// Unban will unban user from chat, who would have thought eh?
// forBanned does nothing if the user is not banned.
func (b *Bot) Unban(chat *Chat, user *User, forBanned ...bool) error {
params := map[string]string{
"chat_id": chat.Recipient(),
"user_id": user.Recipient(),
}
if len(forBanned) > 0 {
params["only_if_banned"] = strconv.FormatBool(forBanned[0])
}
_, err := b.Raw("unbanChatMember", params)
return err
}
// Restrict lets you restrict a subset of member's rights until
// member.RestrictedUntil, such as:
//
// * can send messages
// * can send media
// * can send other
// * can add web page previews
//
func (b *Bot) Restrict(chat *Chat, member *ChatMember) error {
prv, until := member.Rights, member.RestrictedUntil
params := map[string]interface{}{
"chat_id": chat.Recipient(),
"user_id": member.User.Recipient(),
"until_date": strconv.FormatInt(until, 10),
}
embedRights(params, prv)
_, err := b.Raw("restrictChatMember", params)
return err
}
// Promote lets you update member's admin rights, such as:
//
// * can change info
// * can post messages
// * can edit messages
// * can delete messages
// * can invite users
// * can restrict members
// * can pin messages
// * can promote members
//
func (b *Bot) Promote(chat *Chat, member *ChatMember) error {
prv := member.Rights
params := map[string]interface{}{
"chat_id": chat.Recipient(),
"user_id": member.User.Recipient(),
"is_anonymous": member.Anonymous,
}
embedRights(params, prv)
_, err := b.Raw("promoteChatMember", params)
return err
}
// AdminsOf returns a member list of chat admins.
//
// On success, returns an Array of ChatMember objects that
// contains information about all chat administrators except other bots.
// If the chat is a group or a supergroup and
// no administrators were appointed, only the creator will be returned.
func (b *Bot) AdminsOf(chat *Chat) ([]ChatMember, error) {
params := map[string]string{
"chat_id": chat.Recipient(),
}
data, err := b.Raw("getChatAdministrators", params)
if err != nil {
return nil, err
}
var resp struct {
Result []ChatMember
}
if err := json.Unmarshal(data, &resp); err != nil {
return nil, wrapError(err)
}
return resp.Result, nil
}
// Len returns the number of members in a chat.
func (b *Bot) Len(chat *Chat) (int, error) {
params := map[string]string{
"chat_id": chat.Recipient(),
}
data, err := b.Raw("getChatMembersCount", params)
if err != nil {
return 0, err
}
var resp struct {
Result int
}
if err := json.Unmarshal(data, &resp); err != nil {
return 0, wrapError(err)
}
return resp.Result, nil
}
// SetAdminTitle sets a custom title for an administrator.
// A title should be 0-16 characters length, emoji are not allowed.
func (b *Bot) SetAdminTitle(chat *Chat, user *User, title string) error {
params := map[string]string{
"chat_id": chat.Recipient(),
"user_id": user.Recipient(),
"custom_title": title,
}
_, err := b.Raw("setChatAdministratorCustomTitle", params)
return err
}