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
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

View File

@ -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")

View File

@ -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" +

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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) {

View File

@ -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")

View File

@ -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
}

View File

@ -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
}