2020-06-08 20:12:36 +00:00
|
|
|
package channels
|
|
|
|
|
|
|
|
import (
|
2020-08-09 08:07:16 +00:00
|
|
|
"fmt"
|
2020-06-08 20:12:36 +00:00
|
|
|
"git.dragon-labs.de/alphyron/group_helper/logic"
|
2020-06-27 16:38:59 +00:00
|
|
|
"git.dragon-labs.de/alphyron/group_helper/message"
|
2020-06-08 20:12:36 +00:00
|
|
|
"git.dragon-labs.de/alphyron/group_helper/models"
|
|
|
|
"git.dragon-labs.de/alphyron/group_helper/obj"
|
|
|
|
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
|
|
|
|
"log"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func CheckUnverifiedUser(ticker *time.Ticker, quitChannel <-chan struct{}, groupHelperService logic.GroupHelperService, bot *tgbotapi.BotAPI, data *obj.VerifyData) {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ticker.C:
|
|
|
|
|
2020-08-09 08:07:16 +00:00
|
|
|
deleteVerifyUser := make([]*obj.VerifyUser, 0)
|
2020-06-08 20:12:36 +00:00
|
|
|
|
|
|
|
groups := make(map[int64]*models.Group, 0)
|
|
|
|
users := make(map[int]*models.User, 0)
|
|
|
|
|
|
|
|
for _, user := range data.Users {
|
|
|
|
user.LeftTime -= 1
|
2020-08-09 08:07:16 +00:00
|
|
|
log.Println(fmt.Sprintf("Verify checker: (%d/%d/%d)", user.GroupID, user.UserID, user.LeftTime))
|
2020-06-08 20:12:36 +00:00
|
|
|
|
|
|
|
if user.LeftTime <= 0 {
|
|
|
|
log.Println(len(data.Users))
|
|
|
|
|
|
|
|
if _, ok := groups[user.GroupID]; !ok {
|
|
|
|
group, err := groupHelperService.GetGroupByID(user.GroupID)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Error while getting the Group in the verifychecker for id " + strconv.FormatInt(user.GroupID, 10))
|
|
|
|
log.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
groups[user.GroupID] = group
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := users[user.UserID]; !ok {
|
|
|
|
user_db, err := groupHelperService.GetUserByID(int64(user.UserID))
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Error while getting the User in the verifychecker for id " + strconv.Itoa(user.UserID))
|
|
|
|
log.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
users[user.UserID] = user_db
|
|
|
|
}
|
|
|
|
|
|
|
|
userconfig := tgbotapi.ChatConfigWithUser{
|
|
|
|
ChatID: user.GroupID,
|
|
|
|
UserID: user.UserID,
|
|
|
|
}
|
|
|
|
|
|
|
|
member, _ := bot.GetChatMember(userconfig)
|
2020-08-09 08:07:16 +00:00
|
|
|
deleteVerifyUser = append(deleteVerifyUser, user)
|
2020-06-08 20:12:36 +00:00
|
|
|
_, err := groupHelperService.UserLeaveGroup(users[user.UserID], groups[user.GroupID])
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error while removing the user from group in db in the verifier (user/group) (%d/%d)", user.UserID, user.GroupID)
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if member.HasLeft() {
|
|
|
|
bot.DeleteMessage(tgbotapi.DeleteMessageConfig{
|
|
|
|
ChatID: user.GroupID,
|
|
|
|
MessageID: user.MessageID,
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
kickMessage := groups[user.GroupID].UserKickMessage
|
2020-06-27 16:38:59 +00:00
|
|
|
kickMessage = message.ReplaceMessage(kickMessage, member.User)
|
2020-06-08 20:12:36 +00:00
|
|
|
|
|
|
|
msg := tgbotapi.NewMessage(user.GroupID, kickMessage)
|
|
|
|
msg.ParseMode = "Markdown"
|
|
|
|
_, err = bot.Send(msg)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Println("problem while send a message in the verifier:")
|
|
|
|
log.Printf("Problem Error Message: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
kickConfig := tgbotapi.KickChatMemberConfig{
|
|
|
|
ChatMemberConfig: tgbotapi.ChatMemberConfig{
|
|
|
|
UserID: user.UserID,
|
|
|
|
ChatID: user.GroupID,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
res, _ := bot.KickChatMember(kickConfig)
|
|
|
|
if !res.Ok {
|
|
|
|
msg := tgbotapi.NewMessage(user.GroupID, "I can't kick @"+member.User.UserName+"\nReason:\n"+res.Description)
|
|
|
|
msg.ParseMode = "Markdown"
|
|
|
|
_, err = bot.Send(msg)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Println("problem while send a message in the verifier:")
|
|
|
|
log.Printf("Problem Error Message: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = bot.DeleteMessage(tgbotapi.DeleteMessageConfig{
|
|
|
|
ChatID: user.GroupID,
|
|
|
|
MessageID: user.MessageID,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Println("problem while deleting a message in the verifier:")
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-09 08:07:16 +00:00
|
|
|
for _, user := range deleteVerifyUser {
|
2020-06-08 20:12:36 +00:00
|
|
|
data.RemoveUser(user.UserID, user.GroupID)
|
|
|
|
}
|
|
|
|
|
|
|
|
case <-quitChannel:
|
|
|
|
ticker.Stop()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|