From f6316ada970fc4a0a707317b3e98befc28d50442 Mon Sep 17 00:00:00 2001 From: Alphyron Date: Sat, 25 Apr 2020 23:32:11 +0200 Subject: [PATCH] :sparkles: Implement leave set and get message command --- cli/commands/leavemessage.go | 90 ++++++++++++++++++++++++++++++++++++ main.go | 1 + 2 files changed, 91 insertions(+) create mode 100644 cli/commands/leavemessage.go diff --git a/cli/commands/leavemessage.go b/cli/commands/leavemessage.go new file mode 100644 index 0000000..1d9c8ec --- /dev/null +++ b/cli/commands/leavemessage.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 leaveMessage struct { + groupHelperService logic.GroupHelperService +} + +func NewLeaveMessage(groupHelperService logic.GroupHelperService) cli.Command { + return &leaveMessage{ + groupHelperService: groupHelperService, + } +} + +func (j leaveMessage) GetUsage() string { + return "/leave (set/get) [message]" +} + +func (j leaveMessage) GetCommand() string { + return "/leave" +} + +func (j leaveMessage) GetDescription() string { + return "Get or set the leave message" +} + +func (j leaveMessage) ExecuteCommand(api *tgbotapi.BotAPI, update *tgbotapi.Update) (bool, error) { + message := update.Message.Text + parts := strings.Split(message, " ") + + if len(parts) == 0 { + _, 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.UserLeaveMessage) + api.Send(tgbotapi.NewMessage(update.Message.Chat.ID, "Current LeaveMessage (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 + } + + newLeaveMessage := strings.Join(args[2:], " ") + group.UserLeaveMessage = newLeaveMessage + 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.UserJoinMessage) + msg.ParseMode = "Markdown" + api.Send(tgbotapi.NewMessage(update.Message.Chat.ID, "Example LeaveMessage")) + _, 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 leaveMessage) AllowChatType(chat *tgbotapi.Chat) bool { + return chat.IsGroup() || chat.IsSuperGroup() +} + +func (j leaveMessage) AllowMember(member *tgbotapi.ChatMember) bool { + return member.IsAdministrator() || member.IsCreator() +} + +func (j leaveMessage) AllowEveryMember() bool { + return false +} diff --git a/main.go b/main.go index 133534f..852f98c 100644 --- a/main.go +++ b/main.go @@ -78,6 +78,7 @@ func InitialCommandManager(bot *tgbotapi.BotAPI, service logic.GroupHelperServic cm.RegisterCommand(commands.NewInfoCommand()) cm.RegisterCommand(commands.NewHelpCommand(cm)) cm.RegisterCommand(commands.NewJoinMessage(service)) + cm.RegisterCommand(commands.NewLeaveMessage(service)) return cm }