diff --git a/channels/leaveduserchecker.go b/channels/leaveduserchecker.go index efe545f..2ccc376 100644 --- a/channels/leaveduserchecker.go +++ b/channels/leaveduserchecker.go @@ -23,7 +23,7 @@ func CheckForLeavingUser(ticker *time.Ticker, quitChannel <-chan struct{}, group } for _, group := range groups { - time.Sleep(500 * time.Millisecond) + time.Sleep(100 * time.Millisecond) config := tgbotapi.ChatConfig{ ChatID: group.GroupID, @@ -55,7 +55,7 @@ func CheckForLeavingUser(ticker *time.Ticker, quitChannel <-chan struct{}, group } func checkGroupUser(group *models.Group, groupHelperService logic.GroupHelperService, bot *tgbotapi.BotAPI) { - rate := time.Second / 5 + rate := time.Second / 25 throttle := time.Tick(rate) users, err := groupHelperService.ListGroupUsers(group) @@ -79,17 +79,9 @@ func checkGroupUser(group *models.Group, groupHelperService logic.GroupHelperSer continue } - if !(member.User.IsBot || member.User.FirstName == "Telegram") { - if member.User == nil || member.User.FirstName == "" { - member.User = &tgbotapi.User{ - ID: -1, - FirstName: "Unknown", - IsBot: false, - } - } - + if !(member.User == nil || member.User.FirstName == "" || member.User.IsBot || member.User.FirstName == "Telegram") { leaveMessage := group.UserLeaveMessage - leaveMessage = message.ReplaceMessage(leaveMessage, member.User) + leaveMessage = message.ReplaceMessage(leaveMessage, member.User, group) msg := tgbotapi.NewMessage(group.GroupID, leaveMessage) msg.ParseMode = "Markdown" diff --git a/channels/verifiychecker.go b/channels/verifiychecker.go index f647725..76b7731 100644 --- a/channels/verifiychecker.go +++ b/channels/verifiychecker.go @@ -76,7 +76,7 @@ func CheckUnverifiedUser(ticker *time.Ticker, quitChannel <-chan struct{}, group } kickMessage := groups[user.GroupID].UserKickMessage - kickMessage = message.ReplaceMessage(kickMessage, member.User) + kickMessage = message.ReplaceMessage(kickMessage, member.User, groups[user.GroupID]) msg := tgbotapi.NewMessage(user.GroupID, kickMessage) msg.ParseMode = "Markdown" diff --git a/cli/commands/info.go b/cli/commands/info.go index 747f566..b8a0d29 100644 --- a/cli/commands/info.go +++ b/cli/commands/info.go @@ -30,7 +30,7 @@ func (i infoCommand) ExecuteCommand(bot *tgbotapi.BotAPI, update *tgbotapi.Updat "General Information to this Bot\n" + "===============================\n" + "Developer: @Alphyron\n" + - "Version: 2.2.7\n" + + "Version: 2.3.0\n" + "Git: [Gitea Repository](https://git.dragon-labs.de/alphyron/group_assistant)\n" + "===============================" diff --git a/cli/commands/joinmessage.go b/cli/commands/joinmessage.go index 68e1e1a..3af8b2d 100644 --- a/cli/commands/joinmessage.go +++ b/cli/commands/joinmessage.go @@ -62,7 +62,7 @@ func (j joinMessage) ExecuteCommand(api *tgbotapi.BotAPI, update *tgbotapi.Updat return false, err } - msg := tgbotapi.NewMessage(update.Message.Chat.ID, message2.ReplaceMessage(group.UserJoinMessage, update.Message.From)) + msg := tgbotapi.NewMessage(update.Message.Chat.ID, message2.ReplaceMessage(group.UserJoinMessage, update.Message.From, group)) msg.ParseMode = "Markdown" api.Send(tgbotapi.NewMessage(update.Message.Chat.ID, "Example JoinMessage")) _, err = api.Send(msg) diff --git a/cli/commands/kickmessage.go b/cli/commands/kickmessage.go index c8deafc..04b857d 100644 --- a/cli/commands/kickmessage.go +++ b/cli/commands/kickmessage.go @@ -62,7 +62,7 @@ func (j kickMessage) ExecuteCommand(api *tgbotapi.BotAPI, update *tgbotapi.Updat return false, err } - msg := tgbotapi.NewMessage(update.Message.Chat.ID, message2.ReplaceMessage(group.UserKickMessage, update.Message.From)) + msg := tgbotapi.NewMessage(update.Message.Chat.ID, message2.ReplaceMessage(group.UserKickMessage, update.Message.From, group)) msg.ParseMode = "Markdown" api.Send(tgbotapi.NewMessage(update.Message.Chat.ID, "Example KickMessage")) _, err = api.Send(msg) diff --git a/cli/commands/leavemessage.go b/cli/commands/leavemessage.go index cec2cd5..e71e623 100644 --- a/cli/commands/leavemessage.go +++ b/cli/commands/leavemessage.go @@ -62,7 +62,7 @@ func (j leaveMessage) ExecuteCommand(api *tgbotapi.BotAPI, update *tgbotapi.Upda return false, err } - msg := tgbotapi.NewMessage(update.Message.Chat.ID, message2.ReplaceMessage(group.UserLeaveMessage, update.Message.From)) + msg := tgbotapi.NewMessage(update.Message.Chat.ID, message2.ReplaceMessage(group.UserLeaveMessage, update.Message.From, group)) msg.ParseMode = "Markdown" api.Send(tgbotapi.NewMessage(update.Message.Chat.ID, "Example LeaveMessage")) _, err = api.Send(msg) diff --git a/cli/commands/rules.go b/cli/commands/rules.go new file mode 100644 index 0000000..638eef9 --- /dev/null +++ b/cli/commands/rules.go @@ -0,0 +1,48 @@ +package commands + +import ( + "git.dragon-labs.de/alphyron/group_helper/cli" + "git.dragon-labs.de/alphyron/group_helper/logic" + "git.dragon-labs.de/alphyron/group_helper/models" + tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" +) + +type rules struct { + groupHelperService logic.GroupHelperService +} + +func NewRules(groupHelperService logic.GroupHelperService) cli.Command { + return &rules{ + groupHelperService: groupHelperService, + } +} + +func (r rules) GetUsage() string { + return "/rules" +} + +func (r rules) GetCommand() string { + return "/rules" +} + +func (r rules) GetDescription() string { + return "Get the rules of this group" +} + +func (r rules) ExecuteCommand(api *tgbotapi.BotAPI, update *tgbotapi.Update, group *models.Group) (bool, error) { + msg := tgbotapi.NewMessage(update.Message.Chat.ID, group.Rules) + _, err := api.Send(msg) + return err != nil, err +} + +func (r rules) AllowChatType(chat *tgbotapi.Chat) bool { + return true +} + +func (r rules) AllowMember(member *tgbotapi.ChatMember) bool { + return true +} + +func (r rules) AllowEveryMember() bool { + return true +} diff --git a/cli/commands/setrules.go b/cli/commands/setrules.go new file mode 100644 index 0000000..b77d3dd --- /dev/null +++ b/cli/commands/setrules.go @@ -0,0 +1,68 @@ +package commands + +import ( + "git.dragon-labs.de/alphyron/group_helper/cli" + "git.dragon-labs.de/alphyron/group_helper/logic" + message2 "git.dragon-labs.de/alphyron/group_helper/message" + "git.dragon-labs.de/alphyron/group_helper/models" + tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" + "strings" +) + +type setRules struct { + groupHelperService logic.GroupHelperService +} + +func NewSetRules(groupHelperService logic.GroupHelperService) cli.Command { + return &setRules{ + groupHelperService: groupHelperService, + } +} + +func (r setRules) GetUsage() string { + return "/setrules [message]" +} + +func (r setRules) GetCommand() string { + return "/setrules" +} + +func (r setRules) GetDescription() string { + return "Set the rules of this group" +} + +func (r setRules) ExecuteCommand(api *tgbotapi.BotAPI, update *tgbotapi.Update, group *models.Group) (bool, error) { + message := update.Message.Text + parts := strings.Split(message, " ") + + if len(parts) == 1 { + _, err := api.Send(tgbotapi.NewMessage(update.Message.Chat.ID, "Wrong usage of this Command:\n"+r.GetUsage())) + return err != nil, err + } + + newRulesMessage := strings.Join(parts[1:], " ") + group.Rules = newRulesMessage + group, err := r.groupHelperService.UpdateGroup(group) + + if err != nil { + return false, err + } + + msg := tgbotapi.NewMessage(update.Message.Chat.ID, message2.ReplaceMessage(group.Rules, update.Message.From, group)) + msg.ParseMode = "Markdown" + api.Send(tgbotapi.NewMessage(update.Message.Chat.ID, "Example Rules")) + _, err = api.Send(msg) + return err != nil, err +} + +func (r setRules) AllowChatType(chat *tgbotapi.Chat) bool { + return true +} + +func (r setRules) AllowMember(member *tgbotapi.ChatMember) bool { + return member.IsAdministrator() || member.IsCreator() +} + +func (r setRules) AllowEveryMember() bool { + return false +} diff --git a/cli/commands/verifiedmessage.go b/cli/commands/verifiedmessage.go index affaed3..9298375 100644 --- a/cli/commands/verifiedmessage.go +++ b/cli/commands/verifiedmessage.go @@ -62,7 +62,7 @@ func (j verifiedMessage) ExecuteCommand(api *tgbotapi.BotAPI, update *tgbotapi.U return false, err } - msg := tgbotapi.NewMessage(update.Message.Chat.ID, message2.ReplaceMessage(group.UserVerifiedMessage, update.Message.From)) + msg := tgbotapi.NewMessage(update.Message.Chat.ID, message2.ReplaceMessage(group.UserVerifiedMessage, update.Message.From, group)) msg.ParseMode = "Markdown" api.Send(tgbotapi.NewMessage(update.Message.Chat.ID, "Example VerifiedMessage")) _, err = api.Send(msg) diff --git a/main.go b/main.go index ce80d9c..eccfd53 100644 --- a/main.go +++ b/main.go @@ -66,7 +66,7 @@ func main() { rm := InitialRoutineManager(bot, cm, service, userData) verifyTicker := time.NewTicker(1 * time.Minute) - leaveTicker := time.NewTicker(1 * time.Minute) + leaveTicker := time.NewTicker(5 * time.Minute) quit := make(chan struct{}) go channels.CheckUnverifiedUser(verifyTicker, quit, service, bot, userData) @@ -107,6 +107,8 @@ func InitialCommandManager(bot *tgbotapi.BotAPI, service logic.GroupHelperServic cm.RegisterCommand(commands.NewChatInfoCommand()) cm.RegisterCommand(commands.NewAdminGroupCommand(service)) cm.RegisterCommand(commands.NewBroadcastCommand()) + cm.RegisterCommand(commands.NewRules(service)) + cm.RegisterCommand(commands.NewSetRules(service)) return cm } diff --git a/message/replace.go b/message/replace.go index 83e0732..d9a42c4 100644 --- a/message/replace.go +++ b/message/replace.go @@ -1,16 +1,18 @@ package message import ( + "git.dragon-labs.de/alphyron/group_helper/models" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" "strconv" "strings" ) -func ReplaceMessage(message string, user *tgbotapi.User) string { +func ReplaceMessage(message string, user *tgbotapi.User, group *models.Group) string { message = strings.Replace(message, "{{user.firstname}}", user.FirstName, -1) message = strings.Replace(message, "{{user.lastname}}", user.LastName, -1) message = strings.Replace(message, "{{user.username}}", user.UserName, -1) message = strings.Replace(message, "{{user.name}}", user.UserName, -1) message = strings.Replace(message, "{{user.id}}", strconv.Itoa(user.ID), -1) + message = strings.Replace(message, "{{group.rules}}", group.Rules, -1) return message } diff --git a/models/group.go b/models/group.go index fbf636c..83376b4 100644 --- a/models/group.go +++ b/models/group.go @@ -14,6 +14,7 @@ type Group struct { Users []*User `gorm:"many2many:GroupUser;"` AdminGroupID uint64 `gorm:"column:admin_group_id"` ControlledGroups []*Group `gorm:"foreignkey:admin_group_id"` + Rules string `gorm:"column:rules;type:text"` } func (Group) TableName() string { diff --git a/repository/sql/grouphelprepository.go b/repository/sql/grouphelprepository.go index 5fa4c9e..79f4e60 100644 --- a/repository/sql/grouphelprepository.go +++ b/repository/sql/grouphelprepository.go @@ -94,6 +94,7 @@ func (g groupHelperRepository) UpdateGroup(group *models.Group) (*models.Group, OnlineCheck: group.OnlineCheck, KickCooldown: group.KickCooldown, AdminGroupID: group.AdminGroupID, + Rules: group.Rules, }).Error return group, err } diff --git a/telegram/routines/database.go b/telegram/routines/database.go index 9a7865a..616179f 100644 --- a/telegram/routines/database.go +++ b/telegram/routines/database.go @@ -73,6 +73,30 @@ func (d databaseRoutine) Update(botAPI *tgbotapi.BotAPI, update *tgbotapi.Update return err } - _, err = d.groupHelperService.UserJoinGroup(user, group) - return err + isInGroup, err := d.groupHelperService.IsUserInGroup(user, group) + + if err != nil { + return err + } + + if !isInGroup { + chatMember, err := botAPI.GetChatMember(tgbotapi.ChatConfigWithUser{ + ChatID: group.GroupID, + UserID: update.Message.From.ID, + }) + + if err != nil { + return err + } + + if chatMember.IsMember() || chatMember.IsCreator() || chatMember.IsAdministrator() { + _, err = d.groupHelperService.UserJoinGroup(user, group) + + if err != nil { + return err + } + } + } + + return nil } diff --git a/telegram/routines/joinchecker.go b/telegram/routines/joinchecker.go index ed14f22..91e10ed 100644 --- a/telegram/routines/joinchecker.go +++ b/telegram/routines/joinchecker.go @@ -53,7 +53,7 @@ func (j joinCheckerRoutine) Update(botAPI *tgbotapi.BotAPI, update *tgbotapi.Upd markup := tgbotapi.NewInlineKeyboardMarkup(tasks...) - msg := tgbotapi.NewMessage(update.Message.Chat.ID, message2.ReplaceMessage(group.UserJoinMessage, &newUser)) + msg := tgbotapi.NewMessage(update.Message.Chat.ID, message2.ReplaceMessage(group.UserJoinMessage, &newUser, group)) msg.ParseMode = "Markdown" msg.ReplyMarkup = markup diff --git a/telegram/routines/verifier.go b/telegram/routines/verifier.go index 7ecde5f..e7423c3 100644 --- a/telegram/routines/verifier.go +++ b/telegram/routines/verifier.go @@ -53,7 +53,7 @@ func (v verifierRoutine) Update(botAPI *tgbotapi.BotAPI, update *tgbotapi.Update botAPI.AnswerCallbackQuery(tgbotapi.NewCallback(update.CallbackQuery.ID, "You are now verified. Welcome to the chat!")) verifyMessage := group.UserVerifiedMessage - msg := tgbotapi.NewMessage(update.CallbackQuery.Message.Chat.ID, message2.ReplaceMessage(verifyMessage, update.CallbackQuery.From)) + msg := tgbotapi.NewMessage(update.CallbackQuery.Message.Chat.ID, message2.ReplaceMessage(verifyMessage, update.CallbackQuery.From, group)) msg.ParseMode = "Markdown" _, err := botAPI.Send(msg)