/
connect.go
175 lines (161 loc) · 5.09 KB
/
connect.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
package plugins
import (
"fmt"
"regexp"
"strconv"
"strings"
"github.com/Jisin0/Go-Filter-Bot/utils"
"github.com/Jisin0/Go-Filter-Bot/utils/customfilters"
"github.com/PaulSonOfLars/gotgbot/v2"
"github.com/PaulSonOfLars/gotgbot/v2/ext"
)
var cbConnectRegex *regexp.Regexp = regexp.MustCompile(`cbconnect\((.+)\)`)
func Connect(bot *gotgbot.Bot, update *ext.Context) error {
//Check for any existing connections first
_, k := DB.GetConnection(update.Message.From.Id)
if k {
update.Message.Reply(
bot,
"Looks Like You Are Already Connected To A Chat, Please /disconnect From It To Connect To Another One :)",
&gotgbot.SendMessageOpts{},
)
return nil
} else {
if update.Message.Chat.Type == "private" {
//Require a chat id if the command was used in a private chat
args := strings.Split(update.Message.Text, " ")
var chat_raw string
if len(args) < 2 {
utils.Listen(
customfilters.Listen(update.Message),
func(_ *gotgbot.Bot, ctx *ext.Context) error {
chat_raw = strings.Split(ctx.Message.Text, " ")[0]
return nil
},
)
} else {
chat_raw = args[1]
}
chat_id, e := strconv.ParseInt(chat_raw, 0, 64)
if e != nil {
//If converion of raw chat_id to an int64 fails i.e it isnt a number
update.Message.Reply(
bot,
"That Doesnt Seem Like A Valid ChatId A ChatId Looks Something Like -100xxxxxxxxxx :(",
&gotgbot.SendMessageOpts{},
)
return nil
} else {
//Verify and connect
admins, err := bot.GetChatAdministrators(chat_id)
if err != nil {
update.Message.Reply(
bot,
fmt.Sprintf("Sorry Looks Like I Couldnt Find That Chat With Id <code>%v</code>. Make Sure I'm Admin There With Full Permissions :(", chat_id),
&gotgbot.SendMessageOpts{ParseMode: "HTML"},
)
} else {
for _, admin := range admins {
if update.Message.From.Id == admin.GetUser().Id {
DB.ConnectUser(update.Message.From.Id, chat_id)
update.Message.Reply(
bot,
"Awesome I've Succesfully Connected You To Your Group !",
&gotgbot.SendMessageOpts{},
)
return nil
}
}
update.Message.Reply(
bot,
"You Cant Connect To A Chat Where You're Not Admin :)",
&gotgbot.SendMessageOpts{},
)
return nil
}
}
} else if update.Message.Chat.Type == "supergroup" || update.Message.Chat.Type == "group" {
//For groups or supergroups just connect
if update.Message.From.Id == 0 {
//Connect using button in case user is anonymous
update.Message.Reply(
bot,
"It Looks Like You Are Anonymous Click The Button Below To Connect :(",
&gotgbot.SendMessageOpts{
ReplyMarkup: gotgbot.InlineKeyboardMarkup{InlineKeyboard: [][]gotgbot.InlineKeyboardButton{{{Text: "Connect Me", CallbackData: "cbconnect(con)"}}}},
},
)
return nil
} else {
//Verification stuff
admins, _ := bot.GetChatAdministrators(update.Message.Chat.Id)
for _, admin := range admins {
if update.Message.From.Id == admin.GetUser().Id {
DB.ConnectUser(update.Message.From.Id, update.Message.Chat.Id)
update.Message.Reply(
bot,
"Awesome I've Succesfully Connected You To This Group !",
&gotgbot.SendMessageOpts{},
)
return nil
}
}
update.Message.Reply(
bot,
"Ok Mr. Non-Admin :)",
&gotgbot.SendMessageOpts{},
)
return nil
}
}
}
return nil
}
func CbConnect(bot *gotgbot.Bot, update *ext.Context) error {
//Function to handle callback from connect button
action := cbConnectRegex.FindStringSubmatch(update.CallbackQuery.Data)[1]
if action == "con" {
admins, _ := bot.GetChatAdministrators(update.CallbackQuery.Message.Chat.Id)
for _, admin := range admins {
if update.CallbackQuery.From.Id == admin.GetUser().Id {
DB.ConnectUser(update.CallbackQuery.From.Id, update.CallbackQuery.Message.Chat.Id)
update.CallbackQuery.Answer(
bot,
&gotgbot.AnswerCallbackQueryOpts{Text: "Awesome I've Succesfully Connected You To This Group !", ShowAlert: true},
)
update.CallbackQuery.Message.Delete(bot)
return nil
}
}
update.CallbackQuery.Answer(
bot,
&gotgbot.AnswerCallbackQueryOpts{Text: "Ok Mr. Non-Admin :)", ShowAlert: true},
)
return nil
} else if action == "dis" {
DB.DeleteConnection(update.CallbackQuery.From.Id)
update.CallbackQuery.Answer(
bot,
&gotgbot.AnswerCallbackQueryOpts{ShowAlert: true, Text: "All Of Your Connections Were Cleared :)"},
)
}
return nil
}
func Disconnect(bot *gotgbot.Bot, update *ext.Context) error {
//Function to handle /diconnect command
if update.Message.From.Id == 0 {
update.Message.Reply(
bot,
"Sorry Looks Like You Are Anonymous Use The Button Below To Prove Your Identity :)",
&gotgbot.SendMessageOpts{ReplyMarkup: gotgbot.InlineKeyboardMarkup{InlineKeyboard: [][]gotgbot.InlineKeyboardButton{{{Text: "Disconnect Me", CallbackData: "cbconnect(dis)"}}}}},
)
} else {
DB.DeleteConnection(update.Message.From.Id)
update.Message.Reply(
bot,
"Any Existing Connections Were Cleared Successfully :)",
&gotgbot.SendMessageOpts{},
)
}
return nil
}