diff --git a/cli/commands/verifiedmessage.go b/cli/commands/verifiedmessage.go new file mode 100644 index 0000000..a2af414 --- /dev/null +++ b/cli/commands/verifiedmessage.go @@ -0,0 +1,90 @@ +package commands + +import ( + "git.dragon-labs.de/alphyron/group_helper/cli" + "git.dragon-labs.de/alphyron/group_helper/logic" + tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" + "strings" +) + +type verifiedMessage struct { + groupHelperService logic.GroupHelperService +} + +func NewVerifiedMessage(groupHelperService logic.GroupHelperService) cli.Command { + return &verifiedMessage{ + groupHelperService: groupHelperService, + } +} + +func (j verifiedMessage) GetUsage() string { + return "/verified (set/get) [message]" +} + +func (j verifiedMessage) GetCommand() string { + return "/verified" +} + +func (j verifiedMessage) GetDescription() string { + return "Get or set the verified message" +} + +func (j verifiedMessage) ExecuteCommand(api *tgbotapi.BotAPI, update *tgbotapi.Update) (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"+j.GetUsage())) + return err != nil, err + } + + 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) + return err != nil, err + case "set": + if len(parts) <= 2 { + _, err := api.Send(tgbotapi.NewMessage(update.Message.Chat.ID, "Wrong usage of this Command:\n"+j.GetUsage())) + return err != nil, err + } + + newVerifiedMessage := strings.Join(args[2:], " ") + group.UserVerifiedMessage = newVerifiedMessage + group, err = j.groupHelperService.UpdateGroup(group) + + if err != nil { + return false, err + } + + //TODO Create a replace for placeholder + msg := tgbotapi.NewMessage(update.Message.Chat.ID, group.UserVerifiedMessage) + msg.ParseMode = "Markdown" + api.Send(tgbotapi.NewMessage(update.Message.Chat.ID, "Example VerifiedMessage")) + _, err = api.Send(msg) + return err != nil, err + default: + _, err := api.Send(tgbotapi.NewMessage(update.Message.Chat.ID, "Wrong usage of this Command:\n"+j.GetUsage())) + return err != nil, err + } +} + +func (j verifiedMessage) AllowChatType(chat *tgbotapi.Chat) bool { + return chat.IsGroup() || chat.IsSuperGroup() +} + +func (j verifiedMessage) AllowMember(member *tgbotapi.ChatMember) bool { + return member.IsAdministrator() || member.IsCreator() +} + +func (j verifiedMessage) AllowEveryMember() bool { + return false +} diff --git a/main.go b/main.go index 8fc2df5..90ba2d1 100644 --- a/main.go +++ b/main.go @@ -80,6 +80,7 @@ func InitialCommandManager(bot *tgbotapi.BotAPI, service logic.GroupHelperServic cm.RegisterCommand(commands.NewJoinMessage(service)) cm.RegisterCommand(commands.NewLeaveMessage(service)) cm.RegisterCommand(commands.NewKickMessage(service)) + cm.RegisterCommand(commands.NewVerifiedMessage(service)) return cm }