group_helper/channels/verifiychecker.go

127 lines
3.3 KiB
Go
Raw Normal View History

package channels
import (
"git.dragon-labs.de/alphyron/group_helper/logic"
"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:
delete := 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
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)
delete = append(delete, 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,
})
return
}
kickMessage := groups[user.GroupID].UserKickMessage
//TODO Replace placeholder
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)
}
}
}
for _, user := range delete {
data.RemoveUser(user.UserID, user.GroupID)
}
case <-quitChannel:
ticker.Stop()
return
}
}
}