Get group now at the beginning of updates

This commit is contained in:
Alphyron 2020-05-09 20:54:42 +02:00
parent 755d704d5a
commit ca63d7be2f
13 changed files with 54 additions and 59 deletions

View File

@ -1,12 +1,15 @@
package cli 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 { type Command interface {
GetUsage() string GetUsage() string
GetCommand() string GetCommand() string
GetDescription() string GetDescription() string
ExecuteCommand(*tgbotapi.BotAPI, *tgbotapi.Update) (bool, error) ExecuteCommand(*tgbotapi.BotAPI, *tgbotapi.Update, *models.Group) (bool, error)
AllowChatType(*tgbotapi.Chat) bool AllowChatType(*tgbotapi.Chat) bool
AllowMember(*tgbotapi.ChatMember) bool AllowMember(*tgbotapi.ChatMember) bool
AllowEveryMember() bool AllowEveryMember() bool

View File

@ -3,6 +3,7 @@ package commands
import ( import (
"fmt" "fmt"
"git.dragon-labs.de/alphyron/group_helper/cli" "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" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
"strings" "strings"
) )
@ -27,7 +28,7 @@ func (h helpCommand) GetDescription() string {
return "Information about all commands" 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 var builder strings.Builder
builder.WriteString("All supported Commands:\n") builder.WriteString("All supported Commands:\n")

View File

@ -2,6 +2,7 @@ package commands
import ( import (
"git.dragon-labs.de/alphyron/group_helper/cli" "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" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
) )
@ -24,7 +25,7 @@ func (i infoCommand) GetDescription() string {
return "Just some Bot information" 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 := infoMessage :=
"General Information to this Bot\n" + "General Information to this Bot\n" +
"===============================\n" + "===============================\n" +

View File

@ -3,6 +3,7 @@ package commands
import ( import (
"git.dragon-labs.de/alphyron/group_helper/cli" "git.dragon-labs.de/alphyron/group_helper/cli"
"git.dragon-labs.de/alphyron/group_helper/logic" "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" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
"strings" "strings"
) )
@ -29,7 +30,7 @@ func (j joinMessage) GetDescription() string {
return "Get or set the join message" 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 message := update.Message.Text
parts := strings.Split(message, " ") parts := strings.Split(message, " ")
@ -39,17 +40,12 @@ func (j joinMessage) ExecuteCommand(api *tgbotapi.BotAPI, update *tgbotapi.Updat
} }
args := strings.Split(message, " ") args := strings.Split(message, " ")
group, err := j.groupHelperService.GetGroupByID(update.Message.Chat.ID)
if err != nil {
return false, err
}
switch args[1] { switch args[1] {
case "get": case "get":
msg := tgbotapi.NewMessage(update.Message.Chat.ID, group.UserJoinMessage) msg := tgbotapi.NewMessage(update.Message.Chat.ID, group.UserJoinMessage)
api.Send(tgbotapi.NewMessage(update.Message.Chat.ID, "Current JoinMessage (decoded)")) api.Send(tgbotapi.NewMessage(update.Message.Chat.ID, "Current JoinMessage (decoded)"))
_, err = api.Send(msg) _, err := api.Send(msg)
return err != nil, err return err != nil, err
case "set": case "set":
if len(parts) <= 2 { if len(parts) <= 2 {
@ -59,7 +55,7 @@ func (j joinMessage) ExecuteCommand(api *tgbotapi.BotAPI, update *tgbotapi.Updat
newJoinMessage := strings.Join(args[2:], " ") newJoinMessage := strings.Join(args[2:], " ")
group.UserJoinMessage = newJoinMessage group.UserJoinMessage = newJoinMessage
group, err = j.groupHelperService.UpdateGroup(group) group, err := j.groupHelperService.UpdateGroup(group)
if err != nil { if err != nil {
return false, err return false, err

View File

@ -3,6 +3,7 @@ package commands
import ( import (
"git.dragon-labs.de/alphyron/group_helper/cli" "git.dragon-labs.de/alphyron/group_helper/cli"
"git.dragon-labs.de/alphyron/group_helper/logic" "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" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
"strings" "strings"
) )
@ -29,7 +30,7 @@ func (j kickMessage) GetDescription() string {
return "Get or set the kick message" 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 message := update.Message.Text
parts := strings.Split(message, " ") parts := strings.Split(message, " ")
@ -39,17 +40,12 @@ func (j kickMessage) ExecuteCommand(api *tgbotapi.BotAPI, update *tgbotapi.Updat
} }
args := strings.Split(message, " ") args := strings.Split(message, " ")
group, err := j.groupHelperService.GetGroupByID(update.Message.Chat.ID)
if err != nil {
return false, err
}
switch args[1] { switch args[1] {
case "get": case "get":
msg := tgbotapi.NewMessage(update.Message.Chat.ID, group.UserJoinMessage) msg := tgbotapi.NewMessage(update.Message.Chat.ID, group.UserJoinMessage)
api.Send(tgbotapi.NewMessage(update.Message.Chat.ID, "Current KickMessage (decoded)")) api.Send(tgbotapi.NewMessage(update.Message.Chat.ID, "Current KickMessage (decoded)"))
_, err = api.Send(msg) _, err := api.Send(msg)
return err != nil, err return err != nil, err
case "set": case "set":
if len(parts) <= 2 { if len(parts) <= 2 {
@ -59,7 +55,7 @@ func (j kickMessage) ExecuteCommand(api *tgbotapi.BotAPI, update *tgbotapi.Updat
newJoinMessage := strings.Join(args[2:], " ") newJoinMessage := strings.Join(args[2:], " ")
group.UserJoinMessage = newJoinMessage group.UserJoinMessage = newJoinMessage
group, err = j.groupHelperService.UpdateGroup(group) group, err := j.groupHelperService.UpdateGroup(group)
if err != nil { if err != nil {
return false, err return false, err

View File

@ -3,6 +3,7 @@ package commands
import ( import (
"git.dragon-labs.de/alphyron/group_helper/cli" "git.dragon-labs.de/alphyron/group_helper/cli"
"git.dragon-labs.de/alphyron/group_helper/logic" "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" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
"strings" "strings"
) )
@ -29,7 +30,7 @@ func (j leaveMessage) GetDescription() string {
return "Get or set the leave message" 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 message := update.Message.Text
parts := strings.Split(message, " ") parts := strings.Split(message, " ")
@ -39,17 +40,12 @@ func (j leaveMessage) ExecuteCommand(api *tgbotapi.BotAPI, update *tgbotapi.Upda
} }
args := strings.Split(message, " ") args := strings.Split(message, " ")
group, err := j.groupHelperService.GetGroupByID(update.Message.Chat.ID)
if err != nil {
return false, err
}
switch args[1] { switch args[1] {
case "get": case "get":
msg := tgbotapi.NewMessage(update.Message.Chat.ID, group.UserLeaveMessage) msg := tgbotapi.NewMessage(update.Message.Chat.ID, group.UserLeaveMessage)
api.Send(tgbotapi.NewMessage(update.Message.Chat.ID, "Current LeaveMessage (decoded)")) api.Send(tgbotapi.NewMessage(update.Message.Chat.ID, "Current LeaveMessage (decoded)"))
_, err = api.Send(msg) _, err := api.Send(msg)
return err != nil, err return err != nil, err
case "set": case "set":
if len(parts) <= 2 { if len(parts) <= 2 {
@ -59,7 +55,7 @@ func (j leaveMessage) ExecuteCommand(api *tgbotapi.BotAPI, update *tgbotapi.Upda
newLeaveMessage := strings.Join(args[2:], " ") newLeaveMessage := strings.Join(args[2:], " ")
group.UserLeaveMessage = newLeaveMessage group.UserLeaveMessage = newLeaveMessage
group, err = j.groupHelperService.UpdateGroup(group) group, err := j.groupHelperService.UpdateGroup(group)
if err != nil { if err != nil {
return false, err return false, err

View File

@ -3,6 +3,7 @@ package commands
import ( import (
"git.dragon-labs.de/alphyron/group_helper/cli" "git.dragon-labs.de/alphyron/group_helper/cli"
"git.dragon-labs.de/alphyron/group_helper/logic" "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" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
"strconv" "strconv"
) )
@ -29,15 +30,10 @@ func (i toggleOnlineCheckCommand) GetDescription() string {
return "Toggle the setting for a online check when a user join" return "Toggle the setting for a online check when a user join"
} }
func (i toggleOnlineCheckCommand) ExecuteCommand(bot *tgbotapi.BotAPI, update *tgbotapi.Update) (bool, error) { func (i toggleOnlineCheckCommand) ExecuteCommand(bot *tgbotapi.BotAPI, update *tgbotapi.Update, group *models.Group) (bool, error) {
group, err := i.groupHelperService.GetGroupByID(update.Message.Chat.ID)
if err != nil {
return false, err
}
group.OnlineCheck = !group.OnlineCheck group.OnlineCheck = !group.OnlineCheck
group, err = i.groupHelperService.UpdateGroup(group) group, err := i.groupHelperService.UpdateGroup(group)
if err != nil { if err != nil {
return false, err return false, err

View File

@ -3,6 +3,7 @@ package commands
import ( import (
"git.dragon-labs.de/alphyron/group_helper/cli" "git.dragon-labs.de/alphyron/group_helper/cli"
"git.dragon-labs.de/alphyron/group_helper/logic" "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" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
"strconv" "strconv"
) )
@ -29,15 +30,10 @@ func (i toggleWritingForbidCommand) GetDescription() string {
return "Toggle the setting for forbid writing while not verified" return "Toggle the setting for forbid writing while not verified"
} }
func (i toggleWritingForbidCommand) ExecuteCommand(bot *tgbotapi.BotAPI, update *tgbotapi.Update) (bool, error) { func (i toggleWritingForbidCommand) ExecuteCommand(bot *tgbotapi.BotAPI, update *tgbotapi.Update, group *models.Group) (bool, error) {
group, err := i.groupHelperService.GetGroupByID(update.Message.Chat.ID)
if err != nil {
return false, err
}
group.ForbidWriting = !group.ForbidWriting group.ForbidWriting = !group.ForbidWriting
group, err = i.groupHelperService.UpdateGroup(group) group, err := i.groupHelperService.UpdateGroup(group)
if err != nil { if err != nil {
return false, err return false, err

View File

@ -3,6 +3,7 @@ package commands
import ( import (
"git.dragon-labs.de/alphyron/group_helper/cli" "git.dragon-labs.de/alphyron/group_helper/cli"
"git.dragon-labs.de/alphyron/group_helper/logic" "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" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
"strings" "strings"
) )
@ -29,7 +30,7 @@ func (j verifiedMessage) GetDescription() string {
return "Get or set the verified message" 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 message := update.Message.Text
parts := strings.Split(message, " ") parts := strings.Split(message, " ")
@ -39,17 +40,12 @@ func (j verifiedMessage) ExecuteCommand(api *tgbotapi.BotAPI, update *tgbotapi.U
} }
args := strings.Split(message, " ") args := strings.Split(message, " ")
group, err := j.groupHelperService.GetGroupByID(update.Message.Chat.ID)
if err != nil {
return false, err
}
switch args[1] { switch args[1] {
case "get": case "get":
msg := tgbotapi.NewMessage(update.Message.Chat.ID, group.UserVerifiedMessage) msg := tgbotapi.NewMessage(update.Message.Chat.ID, group.UserVerifiedMessage)
api.Send(tgbotapi.NewMessage(update.Message.Chat.ID, "Current VerifiedMessage (decoded)")) api.Send(tgbotapi.NewMessage(update.Message.Chat.ID, "Current VerifiedMessage (decoded)"))
_, err = api.Send(msg) _, err := api.Send(msg)
return err != nil, err return err != nil, err
case "set": case "set":
if len(parts) <= 2 { if len(parts) <= 2 {
@ -59,7 +55,7 @@ func (j verifiedMessage) ExecuteCommand(api *tgbotapi.BotAPI, update *tgbotapi.U
newVerifiedMessage := strings.Join(args[2:], " ") newVerifiedMessage := strings.Join(args[2:], " ")
group.UserVerifiedMessage = newVerifiedMessage group.UserVerifiedMessage = newVerifiedMessage
group, err = j.groupHelperService.UpdateGroup(group) group, err := j.groupHelperService.UpdateGroup(group)
if err != nil { if err != nil {
return false, err return false, err

View File

@ -1,6 +1,7 @@
package cli package cli
import ( import (
"git.dragon-labs.de/alphyron/group_helper/models"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
"strings" "strings"
) )
@ -10,7 +11,7 @@ type CommandManager struct {
Bot *tgbotapi.BotAPI 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 { if update.Message == nil {
return true, 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) { func (cm *CommandManager) RegisterCommand(command Command) {

View File

@ -1,6 +1,7 @@
package telegram package telegram
import ( import (
"git.dragon-labs.de/alphyron/group_helper/logic"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
"log" "log"
) )
@ -8,6 +9,7 @@ import (
type RoutineManager struct { type RoutineManager struct {
Routine []Routine Routine []Routine
Bot *tgbotapi.BotAPI Bot *tgbotapi.BotAPI
GroupHelperService logic.GroupHelperService
} }
func (rm RoutineManager) StartUpdates() { func (rm RoutineManager) StartUpdates() {
@ -17,8 +19,15 @@ func (rm RoutineManager) StartUpdates() {
for update := range updates { 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 { for _, routine := range rm.Routine {
err := routine.Update(&update) err := routine.Update(&update, group)
if err != nil { if err != nil {
log.Printf("ERROR - Routine error") log.Printf("ERROR - Routine error")

View File

@ -1,7 +1,10 @@
package telegram 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 { type Routine interface {
Update(update *tgbotapi.Update) error Update(update *tgbotapi.Update, group *models.Group) error
} }

View File

@ -2,6 +2,7 @@ package routines
import ( import (
"git.dragon-labs.de/alphyron/group_helper/cli" "git.dragon-labs.de/alphyron/group_helper/cli"
"git.dragon-labs.de/alphyron/group_helper/models"
"git.dragon-labs.de/alphyron/group_helper/telegram" "git.dragon-labs.de/alphyron/group_helper/telegram"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" 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} return &commandRoutine{CommandManager: commandManager}
} }
func (cr commandRoutine) Update(update *tgbotapi.Update) error { func (cr commandRoutine) Update(update *tgbotapi.Update, group *models.Group) error {
_, err := cr.CommandManager.ExecuteUpdate(update) _, err := cr.CommandManager.ExecuteUpdate(update, group)
return err return err
} }