-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgroups.go
139 lines (114 loc) · 3.01 KB
/
groups.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
package hudorbot
import (
"fmt"
"strconv"
"github.com/go-redis/redis"
)
func whiteListKey(groupID int64) string {
return fmt.Sprintf("whitelist:%d", groupID)
}
func groupKey(groupID int64) string {
return fmt.Sprintf("group:%d", groupID)
}
// TODO isBotApproved function
func changeGroupActiveStatus(conn *redis.Client, chatID int64, isActive bool) error {
gpKey := groupKey(chatID)
return conn.HSet(gpKey, "isActive", isActive).Err()
}
func changeGroupShowWarnStatus(conn *redis.Client, chatID int64, showWarn bool) error {
gpKey := groupKey(chatID)
return conn.HSet(gpKey, "showWarn", showWarn).Err()
}
func changeGroupWarnLimit(conn *redis.Client, chatID int64, newLimit int64) error {
gpKey := groupKey(chatID)
return conn.HSet(gpKey, "limit", newLimit).Err()
}
func groupCreator(conn *redis.Client, chatID int64) (creator int, err error) {
gpKey := groupKey(chatID)
return conn.HGet(gpKey, "creator").Int()
}
func isGroupActive(conn *redis.Client, chatID int64) (bool, error) {
gpKey := groupKey(chatID)
isActiveString, err := conn.HGet(gpKey, "isActive").Result()
if err != nil {
return false, err
}
isActive, err := strconv.ParseBool(isActiveString)
if err != nil {
isActive = false
}
return isActive, nil
}
func findGroupByID(conn *redis.Client, chatID int64) (*groupSettings, error) {
gpKey := groupKey(chatID)
gHash, err := conn.HGetAll(gpKey).Result()
if err != nil {
return nil, err
}
if len(gHash) == 0 {
return nil, nil
}
groupSettings := newGroupSettings(gHash)
return groupSettings, nil
}
func newGroupSettings(groupSetting map[string]string) *groupSettings {
isActive, err := strconv.ParseBool(groupSetting["isActive"])
if err != nil {
isActive = false
}
showWarn, err := strconv.ParseBool(groupSetting["showWarn"])
if err != nil {
showWarn = false
}
limit, err := strconv.ParseInt(groupSetting["limit"], 10, 64)
if err != nil {
limit = 0
}
creator, err := strconv.ParseInt(groupSetting["creator"], 10, 64)
if err != nil {
creator = 0
}
gp := &groupSettings{
Limit: limit,
IsActive: isActive,
ShowWarn: showWarn,
Creator: int(creator),
Title: groupSetting["title"],
Description: groupSetting["description"],
}
return gp
}
type minimalGroup struct {
ID int64
Title string
}
type groupSettings struct {
IsActive bool
ShowWarn bool
Limit int64
Creator int
Title string
Description string
}
func (g *groupSettings) IsActiveFa() string {
if g.IsActive {
return "❇️ فعال ❇️"
}
return "🚫 غیر فعال 🚫"
}
func (g *groupSettings) ShowWarnFa() string {
if g.ShowWarn {
return "❇️ فعال ❇️"
}
return "🚫 غیر فعال 🚫"
}
func (g *groupSettings) Map() map[string]interface{} {
return map[string]interface{}{
"isActive": strconv.FormatBool(g.IsActive),
"showWarn": strconv.FormatBool(g.ShowWarn),
"limit": strconv.FormatInt(g.Limit, 10),
"creator": strconv.FormatInt(int64(g.Creator), 10),
"title": g.Title,
"description": g.Description,
}
}