From ca63d7be2f92b7812aad5a0252fad90af36cc609 Mon Sep 17 00:00:00 2001 From: Alphyron Date: Sat, 9 May 2020 20:54:42 +0200 Subject: [PATCH] :sparkles: Get group now at the beginning of updates --- cli/command.go | 7 +++++-- cli/commands/help.go | 3 ++- cli/commands/info.go | 3 ++- cli/commands/joinmessage.go | 12 ++++-------- cli/commands/kickmessage.go | 12 ++++-------- cli/commands/leavemessage.go | 12 ++++-------- cli/commands/toggleonlinecheck.go | 10 +++------- cli/commands/togglewritingforbid.go | 10 +++------- cli/commands/verifiedmessage.go | 12 ++++-------- cli/manager.go | 5 +++-- telegram/manager.go | 15 ++++++++++++--- telegram/routine.go | 7 +++++-- telegram/routines/command.go | 5 +++-- 13 files changed, 54 insertions(+), 59 deletions(-) diff --git a/cli/command.go b/cli/command.go index 70ca79c..b1f2300 100644 --- a/cli/command.go +++ b/cli/command.go @@ -1,12 +1,15 @@ package cli -import tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" +import ( + "git.dragon-labs.de/alphyron/group_helper/models" + tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" +) type Command interface { GetUsage() string GetCommand() string GetDescription() string - ExecuteCommand(*tgbotapi.BotAPI, *tgbotapi.Update) (bool, error) + ExecuteCommand(*tgbotapi.BotAPI, *tgbotapi.Update, *models.Group) (bool, error) AllowChatType(*tgbotapi.Chat) bool AllowMember(*tgbotapi.ChatMember) bool AllowEveryMember() bool diff --git a/cli/commands/help.go b/cli/commands/help.go index 9c94989..50af5c7 100644 --- a/cli/commands/help.go +++ b/cli/commands/help.go @@ -3,6 +3,7 @@ package commands import ( "fmt" "git.dragon-labs.de/alphyron/group_helper/cli" + "git.dragon-labs.de/alphyron/group_helper/models" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" "strings" ) @@ -27,7 +28,7 @@ func (h helpCommand) GetDescription() string { return "Information about all commands" } -func (h helpCommand) ExecuteCommand(bot *tgbotapi.BotAPI, update *tgbotapi.Update) (bool, error) { +func (h helpCommand) ExecuteCommand(bot *tgbotapi.BotAPI, update *tgbotapi.Update, group *models.Group) (bool, error) { var builder strings.Builder builder.WriteString("All supported Commands:\n") diff --git a/cli/commands/info.go b/cli/commands/info.go index 1f63a71..a3f817c 100644 --- a/cli/commands/info.go +++ b/cli/commands/info.go @@ -2,6 +2,7 @@ package commands import ( "git.dragon-labs.de/alphyron/group_helper/cli" + "git.dragon-labs.de/alphyron/group_helper/models" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" ) @@ -24,7 +25,7 @@ func (i infoCommand) GetDescription() string { return "Just some Bot information" } -func (i infoCommand) ExecuteCommand(bot *tgbotapi.BotAPI, update *tgbotapi.Update) (bool, error) { +func (i infoCommand) ExecuteCommand(bot *tgbotapi.BotAPI, update *tgbotapi.Update, group *models.Group) (bool, error) { infoMessage := "General Information to this Bot\n" + "===============================\n" + diff --git a/cli/commands/joinmessage.go b/cli/commands/joinmessage.go index 970f1f9..5a82763 100644 --- a/cli/commands/joinmessage.go +++ b/cli/commands/joinmessage.go @@ -3,6 +3,7 @@ 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" "strings" ) @@ -29,7 +30,7 @@ func (j joinMessage) GetDescription() string { return "Get or set the join message" } -func (j joinMessage) ExecuteCommand(api *tgbotapi.BotAPI, update *tgbotapi.Update) (bool, error) { +func (j joinMessage) ExecuteCommand(api *tgbotapi.BotAPI, update *tgbotapi.Update, group *models.Group) (bool, error) { message := update.Message.Text parts := strings.Split(message, " ") @@ -39,17 +40,12 @@ func (j joinMessage) ExecuteCommand(api *tgbotapi.BotAPI, update *tgbotapi.Updat } args := strings.Split(message, " ") - group, err := j.groupHelperService.GetGroupByID(update.Message.Chat.ID) - - if err != nil { - return false, err - } switch args[1] { case "get": msg := tgbotapi.NewMessage(update.Message.Chat.ID, group.UserJoinMessage) api.Send(tgbotapi.NewMessage(update.Message.Chat.ID, "Current JoinMessage (decoded)")) - _, err = api.Send(msg) + _, err := api.Send(msg) return err != nil, err case "set": if len(parts) <= 2 { @@ -59,7 +55,7 @@ func (j joinMessage) ExecuteCommand(api *tgbotapi.BotAPI, update *tgbotapi.Updat newJoinMessage := strings.Join(args[2:], " ") group.UserJoinMessage = newJoinMessage - group, err = j.groupHelperService.UpdateGroup(group) + group, err := j.groupHelperService.UpdateGroup(group) if err != nil { return false, err diff --git a/cli/commands/kickmessage.go b/cli/commands/kickmessage.go index 4d36a8d..f32adfe 100644 --- a/cli/commands/kickmessage.go +++ b/cli/commands/kickmessage.go @@ -3,6 +3,7 @@ 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" "strings" ) @@ -29,7 +30,7 @@ func (j kickMessage) GetDescription() string { return "Get or set the kick message" } -func (j kickMessage) ExecuteCommand(api *tgbotapi.BotAPI, update *tgbotapi.Update) (bool, error) { +func (j kickMessage) ExecuteCommand(api *tgbotapi.BotAPI, update *tgbotapi.Update, group *models.Group) (bool, error) { message := update.Message.Text parts := strings.Split(message, " ") @@ -39,17 +40,12 @@ func (j kickMessage) ExecuteCommand(api *tgbotapi.BotAPI, update *tgbotapi.Updat } args := strings.Split(message, " ") - group, err := j.groupHelperService.GetGroupByID(update.Message.Chat.ID) - - if err != nil { - return false, err - } switch args[1] { case "get": msg := tgbotapi.NewMessage(update.Message.Chat.ID, group.UserJoinMessage) api.Send(tgbotapi.NewMessage(update.Message.Chat.ID, "Current KickMessage (decoded)")) - _, err = api.Send(msg) + _, err := api.Send(msg) return err != nil, err case "set": if len(parts) <= 2 { @@ -59,7 +55,7 @@ func (j kickMessage) ExecuteCommand(api *tgbotapi.BotAPI, update *tgbotapi.Updat newJoinMessage := strings.Join(args[2:], " ") group.UserJoinMessage = newJoinMessage - group, err = j.groupHelperService.UpdateGroup(group) + group, err := j.groupHelperService.UpdateGroup(group) if err != nil { return false, err diff --git a/cli/commands/leavemessage.go b/cli/commands/leavemessage.go index 98fcc03..944a405 100644 --- a/cli/commands/leavemessage.go +++ b/cli/commands/leavemessage.go @@ -3,6 +3,7 @@ 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" "strings" ) @@ -29,7 +30,7 @@ func (j leaveMessage) GetDescription() string { return "Get or set the leave message" } -func (j leaveMessage) ExecuteCommand(api *tgbotapi.BotAPI, update *tgbotapi.Update) (bool, error) { +func (j leaveMessage) ExecuteCommand(api *tgbotapi.BotAPI, update *tgbotapi.Update, group *models.Group) (bool, error) { message := update.Message.Text parts := strings.Split(message, " ") @@ -39,17 +40,12 @@ func (j leaveMessage) ExecuteCommand(api *tgbotapi.BotAPI, update *tgbotapi.Upda } args := strings.Split(message, " ") - group, err := j.groupHelperService.GetGroupByID(update.Message.Chat.ID) - - if err != nil { - return false, err - } switch args[1] { case "get": msg := tgbotapi.NewMessage(update.Message.Chat.ID, group.UserLeaveMessage) api.Send(tgbotapi.NewMessage(update.Message.Chat.ID, "Current LeaveMessage (decoded)")) - _, err = api.Send(msg) + _, err := api.Send(msg) return err != nil, err case "set": if len(parts) <= 2 { @@ -59,7 +55,7 @@ func (j leaveMessage) ExecuteCommand(api *tgbotapi.BotAPI, update *tgbotapi.Upda newLeaveMessage := strings.Join(args[2:], " ") group.UserLeaveMessage = newLeaveMessage - group, err = j.groupHelperService.UpdateGroup(group) + group, err := j.groupHelperService.UpdateGroup(group) if err != nil { return false, err diff --git a/cli/commands/toggleonlinecheck.go b/cli/commands/toggleonlinecheck.go index 9bac12e..2b52499 100644 --- a/cli/commands/toggleonlinecheck.go +++ b/cli/commands/toggleonlinecheck.go @@ -3,6 +3,7 @@ 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" "strconv" ) @@ -29,15 +30,10 @@ func (i toggleOnlineCheckCommand) GetDescription() string { return "Toggle the setting for a online check when a user join" } -func (i toggleOnlineCheckCommand) ExecuteCommand(bot *tgbotapi.BotAPI, update *tgbotapi.Update) (bool, error) { - group, err := i.groupHelperService.GetGroupByID(update.Message.Chat.ID) - - if err != nil { - return false, err - } +func (i toggleOnlineCheckCommand) ExecuteCommand(bot *tgbotapi.BotAPI, update *tgbotapi.Update, group *models.Group) (bool, error) { group.OnlineCheck = !group.OnlineCheck - group, err = i.groupHelperService.UpdateGroup(group) + group, err := i.groupHelperService.UpdateGroup(group) if err != nil { return false, err diff --git a/cli/commands/togglewritingforbid.go b/cli/commands/togglewritingforbid.go index 8aaa7b5..fcb2cc5 100644 --- a/cli/commands/togglewritingforbid.go +++ b/cli/commands/togglewritingforbid.go @@ -3,6 +3,7 @@ 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" "strconv" ) @@ -29,15 +30,10 @@ func (i toggleWritingForbidCommand) GetDescription() string { return "Toggle the setting for forbid writing while not verified" } -func (i toggleWritingForbidCommand) ExecuteCommand(bot *tgbotapi.BotAPI, update *tgbotapi.Update) (bool, error) { - group, err := i.groupHelperService.GetGroupByID(update.Message.Chat.ID) - - if err != nil { - return false, err - } +func (i toggleWritingForbidCommand) ExecuteCommand(bot *tgbotapi.BotAPI, update *tgbotapi.Update, group *models.Group) (bool, error) { group.ForbidWriting = !group.ForbidWriting - group, err = i.groupHelperService.UpdateGroup(group) + group, err := i.groupHelperService.UpdateGroup(group) if err != nil { return false, err diff --git a/cli/commands/verifiedmessage.go b/cli/commands/verifiedmessage.go index a2af414..95d7620 100644 --- a/cli/commands/verifiedmessage.go +++ b/cli/commands/verifiedmessage.go @@ -3,6 +3,7 @@ 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" "strings" ) @@ -29,7 +30,7 @@ func (j verifiedMessage) GetDescription() string { return "Get or set the verified message" } -func (j verifiedMessage) ExecuteCommand(api *tgbotapi.BotAPI, update *tgbotapi.Update) (bool, error) { +func (j verifiedMessage) ExecuteCommand(api *tgbotapi.BotAPI, update *tgbotapi.Update, group *models.Group) (bool, error) { message := update.Message.Text parts := strings.Split(message, " ") @@ -39,17 +40,12 @@ func (j verifiedMessage) ExecuteCommand(api *tgbotapi.BotAPI, update *tgbotapi.U } args := strings.Split(message, " ") - group, err := j.groupHelperService.GetGroupByID(update.Message.Chat.ID) - - if err != nil { - return false, err - } switch args[1] { case "get": msg := tgbotapi.NewMessage(update.Message.Chat.ID, group.UserVerifiedMessage) api.Send(tgbotapi.NewMessage(update.Message.Chat.ID, "Current VerifiedMessage (decoded)")) - _, err = api.Send(msg) + _, err := api.Send(msg) return err != nil, err case "set": if len(parts) <= 2 { @@ -59,7 +55,7 @@ func (j verifiedMessage) ExecuteCommand(api *tgbotapi.BotAPI, update *tgbotapi.U newVerifiedMessage := strings.Join(args[2:], " ") group.UserVerifiedMessage = newVerifiedMessage - group, err = j.groupHelperService.UpdateGroup(group) + group, err := j.groupHelperService.UpdateGroup(group) if err != nil { return false, err diff --git a/cli/manager.go b/cli/manager.go index 279748c..084b879 100644 --- a/cli/manager.go +++ b/cli/manager.go @@ -1,6 +1,7 @@ package cli import ( + "git.dragon-labs.de/alphyron/group_helper/models" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" "strings" ) @@ -10,7 +11,7 @@ type CommandManager struct { Bot *tgbotapi.BotAPI } -func (cm CommandManager) ExecuteUpdate(update *tgbotapi.Update) (bool, error) { +func (cm CommandManager) ExecuteUpdate(update *tgbotapi.Update, group *models.Group) (bool, error) { if update.Message == nil { return true, nil } @@ -59,7 +60,7 @@ func (cm CommandManager) ExecuteUpdate(update *tgbotapi.Update) (bool, error) { } } - return command.ExecuteCommand(cm.Bot, update) + return command.ExecuteCommand(cm.Bot, update, group) } func (cm *CommandManager) RegisterCommand(command Command) { diff --git a/telegram/manager.go b/telegram/manager.go index 0f6839d..72d8c92 100644 --- a/telegram/manager.go +++ b/telegram/manager.go @@ -1,13 +1,15 @@ package telegram import ( + "git.dragon-labs.de/alphyron/group_helper/logic" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" "log" ) type RoutineManager struct { - Routine []Routine - Bot *tgbotapi.BotAPI + Routine []Routine + Bot *tgbotapi.BotAPI + GroupHelperService logic.GroupHelperService } func (rm RoutineManager) StartUpdates() { @@ -17,8 +19,15 @@ func (rm RoutineManager) StartUpdates() { for update := range updates { + group, err := rm.GroupHelperService.GetGroupByID(update.Message.Chat.ID) + + if err != nil { + log.Println(err) + continue + } + for _, routine := range rm.Routine { - err := routine.Update(&update) + err := routine.Update(&update, group) if err != nil { log.Printf("ERROR - Routine error") diff --git a/telegram/routine.go b/telegram/routine.go index 8dcec97..f10f015 100644 --- a/telegram/routine.go +++ b/telegram/routine.go @@ -1,7 +1,10 @@ package telegram -import tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" +import ( + "git.dragon-labs.de/alphyron/group_helper/models" + tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" +) type Routine interface { - Update(update *tgbotapi.Update) error + Update(update *tgbotapi.Update, group *models.Group) error } diff --git a/telegram/routines/command.go b/telegram/routines/command.go index 0d517c0..e5f4ba3 100644 --- a/telegram/routines/command.go +++ b/telegram/routines/command.go @@ -2,6 +2,7 @@ package routines import ( "git.dragon-labs.de/alphyron/group_helper/cli" + "git.dragon-labs.de/alphyron/group_helper/models" "git.dragon-labs.de/alphyron/group_helper/telegram" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" ) @@ -14,8 +15,8 @@ func NewCommandRoutine(commandManager *cli.CommandManager) telegram.Routine { return &commandRoutine{CommandManager: commandManager} } -func (cr commandRoutine) Update(update *tgbotapi.Update) error { - _, err := cr.CommandManager.ExecuteUpdate(update) +func (cr commandRoutine) Update(update *tgbotapi.Update, group *models.Group) error { + _, err := cr.CommandManager.ExecuteUpdate(update, group) return err }