-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.go
178 lines (158 loc) · 3.74 KB
/
helpers.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
package main
import (
"fmt"
"log/slog"
"os"
"regexp"
"strconv"
"strings"
"time"
tgbotapi "github.com/OvyFlash/telegram-bot-api"
)
func isWebhook() bool {
return MainConfig.Connection == "webhook"
}
func isDebugMode() bool {
return strings.ToLower(os.Getenv("BOT_DEBUG")) == "true"
}
func isCachedUser(userid int64) bool {
newMember := getMember(userid)
if newMember == nil {
cache.Member = append(cache.Member, ChatMember{
Id: userid,
WelcomeShowed: true,
Rank: 0,
MessageCount: 0,
})
return true
}
return false
}
func isNewMember(Member *tgbotapi.ChatMemberUpdated) bool {
if Member.OldChatMember.IsMember && !Member.NewChatMember.IsMember {
slog.Info(fmt.Sprintf("IsMember state changed %t -> %t", Member.OldChatMember.IsMember, Member.NewChatMember.IsMember))
return false
}
if Member.NewChatMember.IsMember {
return false
}
slog.Info(fmt.Sprintf("IsMember %t", Member.NewChatMember.IsMember))
return isCachedUser(Member.NewChatMember.User.ID)
}
func isMessageStartsWithEmoji(update tgbotapi.Update) bool {
if update.Message.Entities == nil {
return false
}
if update.Message.Entities[0].Type == "custom_emoji" && update.Message.Entities[0].Offset == 0 && len(update.Message.Text) > 4 {
return true
}
return false
}
func kickChatMember(chatID int64, userID int64) {
BanChatMember(chatID, userID, time.Now().UTC().Add(time.Hour*6).Unix())
}
func BanChatMember(chatID int64, userID int64, untilDate int64) {
//Ban for 11 months
if untilDate == 0 {
untilDate = time.Now().UTC().AddDate(0, 11, 0).Unix()
}
config := tgbotapi.BanChatMemberConfig{
ChatMemberConfig: tgbotapi.ChatMemberConfig{
ChatConfig: tgbotapi.ChatConfig{
ChatID: chatID,
},
UserID: userID,
},
UntilDate: untilDate,
}
bot.Send(config)
slog.Info(fmt.Sprintf("User banned: %d until %d", userID, untilDate))
}
func isChannelMessage(update tgbotapi.Update) bool {
return update.Message.SenderChat != nil
}
func isDenyBot(message *tgbotapi.Message) bool {
badbot := false
if message.ViaBot != nil {
for _, bot := range MainConfig.DenyBots {
if strings.ToLower(message.ViaBot.UserName) == bot {
badbot = true
break
}
}
}
return badbot
}
func getNameLink(user tgbotapi.User) string {
slog.Info("User:", "user", user)
userid := strconv.Itoa(int(user.ID))
name := ""
if user.FirstName != "" {
name = user.FirstName
}
if user.LastName != "" {
name = name + " " + user.LastName
}
if user.UserName != "" {
name = name + "(" + user.UserName + ")"
}
return "<a href=\"tg://user?id=" + userid + "\">" + name + "</a>"
}
func deleteMessage(chatID int64, messageId int) {
bot.Send(tgbotapi.NewDeleteMessage(chatID, messageId))
}
func isBadMessage(message string) bool {
for _, word := range MainConfig.ForbiddenText {
if word[0] == 'r' {
regex := regexp.MustCompile(word[2:])
if regex.MatchString(message) {
slog.Info("TriggeredBad: ", word[2:])
return true
}
} else {
if strings.Contains(message, word) {
slog.Info("TriggeredBad: ", word)
return true
}
}
}
return false
}
func toggle_debugMode() string {
msg := ""
if bot.Debug {
bot.Debug = false
msg = "Debug mode off"
} else {
bot.Debug = true
msg = "Debug mode on"
}
slog.Info(msg)
return msg
}
func toggle_forceMode() string {
msg := ""
if forceProtection {
forceProtection = false
msg = "ForceProtection mode off"
} else {
forceProtection = true
msg = "ForceProtection mode on"
}
slog.Info(msg)
return msg
}
func isAdmin(userId int64) bool {
for _, admin := range MainConfig.Admins {
if int64(admin) == userId {
return true
}
}
return false
}
func getPinnedMessage() string {
return MainConfig.PinnedMessage
}
func getPinnedMessageId() int {
return MainConfig.PinnedMessageId
}