group_helper/channels/verifiychecker.go

132 lines
3.7 KiB
Go

package channels
import (
"fmt"
"git.dragon-labs.de/alphyron/group_helper/logic"
"git.dragon-labs.de/alphyron/group_helper/message"
"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:
deleteVerifyUser := make([]*obj.VerifyUser, 0)
groups := make(map[int64]*models.Group, 0)
users := make(map[int]*models.User, 0)
for _, user := range data.Users {
user.LeftTime -= 1
log.Println(fmt.Sprintf("Verify checker: (%d/%d/%d)", user.GroupID, user.UserID, user.LeftTime))
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)
continue
}
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)
continue
}
users[user.UserID] = user_db
}
userconfig := tgbotapi.ChatConfigWithUser{
ChatID: user.GroupID,
UserID: user.UserID,
}
member, _ := bot.GetChatMember(userconfig)
deleteVerifyUser = append(deleteVerifyUser, user)
_, 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,
})
continue
}
kickMessage := groups[user.GroupID].UserKickMessage
kickMessage = message.ReplaceMessage(kickMessage, member.User, groups[user.GroupID])
_, 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)
} else {
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)
}
}
}
}
for _, user := range deleteVerifyUser {
data.RemoveUser(user.UserID, user.GroupID)
}
case <-quitChannel:
ticker.Stop()
log.Println("ERROR - VerifyChecker channel stopped!!!")
return
}
}
}