package commands import ( "encoding/json" "fmt" "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" "log" "strconv" "strings" ) type adminGroupCommand struct { groupHelperService logic.GroupHelperService } func NewAdminGroupCommand(groupHelperService logic.GroupHelperService) cli.Command { return &adminGroupCommand{ groupHelperService: groupHelperService, } } func (i adminGroupCommand) GetUsage() string { return "/admin (add/del/list) [groupid]" } func (i adminGroupCommand) GetCommand() string { return "/admin" } func (i adminGroupCommand) GetDescription() string { return "Add, Remove or List Admin-Groups" } func (i adminGroupCommand) ExecuteCommand(bot *tgbotapi.BotAPI, update *tgbotapi.Update, group *models.Group) (bool, error) { message := update.Message.Text parts := strings.Split(message, " ") if len(parts) == 1 { _, err := bot.Send(tgbotapi.NewMessage(update.Message.Chat.ID, "Wrong usage of this Command:\n"+i.GetUsage())) return err != nil, err } args := strings.Split(message, " ") switch args[1] { case "list": message := fmt.Sprintf("Groups is managed by:\n") if group.AdminGroupID != 0 { adminGroup, err := i.groupHelperService.GetGroupByDBID(group.AdminGroupID) if err != nil { return false, err } chat, err := bot.GetChat(tgbotapi.ChatConfig{ ChatID: adminGroup.GroupID, }) nameBytes, _ := json.Marshal(chat) log.Println(string(nameBytes)) if err == nil { message += fmt.Sprintf(" - %d: %s\n", adminGroup.AdminGroupID, chat.Title) } else { message += fmt.Sprintf(" - %d: %d\n", adminGroup.AdminGroupID, adminGroup.GroupID) } } else { message += fmt.Sprintf("There is no Admin Group\n") } message += "\n\nGroups to manage:\n" if len(group.ControlledGroups) == 0 { message += fmt.Sprintf("There are no Groups to Manage") } else { for i, controlled := range group.ControlledGroups { message += fmt.Sprintf("%d.) %v ", i+1, controlled.GroupID) } } msg := tgbotapi.NewMessage(update.Message.Chat.ID, message) _, err := bot.Send(msg) return err != nil, err case "add": if len(parts) <= 2 { _, err := bot.Send(tgbotapi.NewMessage(update.Message.Chat.ID, "Wrong usage of this Command:\n"+i.GetUsage())) return err != nil, err } groupID := parts[2] groupIDInt, err := strconv.ParseInt(groupID, 10, 64) if err != nil { return false, err } newGroup, err := i.groupHelperService.GetGroupByID(groupIDInt) if err != nil { return false, err } newGroup.ControlledGroups = append(newGroup.ControlledGroups, group) newGroup, err = i.groupHelperService.UpdateGroup(newGroup) if err != nil { return false, err } msg := tgbotapi.NewMessage(update.Message.Chat.ID, "The group is now maintained by the given Group") _, err = bot.Send(msg) if err != nil { return false, err } msg = tgbotapi.NewMessage(newGroup.GroupID, fmt.Sprintf("This group can now control the following Group:\n"+ "- %s", update.Message.Chat.Type)) _, err = bot.Send(msg) return err != nil, err case "del": if len(parts) <= 2 { _, err := bot.Send(tgbotapi.NewMessage(update.Message.Chat.ID, "Wrong usage of this Command:\n"+i.GetUsage())) return err != nil, err } groupID := parts[2] groupIDInt, err := strconv.ParseInt(groupID, 10, 64) if err != nil { return false, err } group, err := i.groupHelperService.GetGroupByID(groupIDInt) if err != nil { return false, err } group.AdminGroupID = 0 newGroup, err := i.groupHelperService.UpdateGroup(group) if err != nil { return false, err } msg := tgbotapi.NewMessage(newGroup.GroupID, "This group is now Maintained by no other Group") _, err = bot.Send(msg) return err != nil, err default: _, err := bot.Send(tgbotapi.NewMessage(update.Message.Chat.ID, "Wrong usage of this Command:\n"+i.GetUsage())) return err != nil, err } return true, nil } func (i adminGroupCommand) AllowChatType(chat *tgbotapi.Chat) bool { return chat.IsGroup() || chat.IsSuperGroup() } func (i adminGroupCommand) AllowMember(member *tgbotapi.ChatMember) bool { return member.IsAdministrator() || member.IsCreator() } func (i adminGroupCommand) AllowEveryMember() bool { return false }