From 6135ed75bef252414e865d21aa2f75441acb2bc3 Mon Sep 17 00:00:00 2001 From: Alphyron Date: Sat, 25 Apr 2020 23:47:37 +0200 Subject: [PATCH] :sparkles: Implement kick set and get message command --- cli/commands/joinmessage.go | 2 +- cli/commands/kickmessage.go | 90 ++++++++++++++++++++++++++++++++++++ cli/commands/leavemessage.go | 4 +- main.go | 1 + 4 files changed, 94 insertions(+), 3 deletions(-) create mode 100644 cli/commands/kickmessage.go diff --git a/cli/commands/joinmessage.go b/cli/commands/joinmessage.go index 2901c5f..970f1f9 100644 --- a/cli/commands/joinmessage.go +++ b/cli/commands/joinmessage.go @@ -33,7 +33,7 @@ func (j joinMessage) ExecuteCommand(api *tgbotapi.BotAPI, update *tgbotapi.Updat message := update.Message.Text parts := strings.Split(message, " ") - if len(parts) == 0 { + 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 } diff --git a/cli/commands/kickmessage.go b/cli/commands/kickmessage.go new file mode 100644 index 0000000..4d36a8d --- /dev/null +++ b/cli/commands/kickmessage.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 kickMessage struct { + groupHelperService logic.GroupHelperService +} + +func NewKickMessage(groupHelperService logic.GroupHelperService) cli.Command { + return &kickMessage{ + groupHelperService: groupHelperService, + } +} + +func (j kickMessage) GetUsage() string { + return "/kick (set/get) [message]" +} + +func (j kickMessage) GetCommand() string { + return "/kick" +} + +func (j kickMessage) GetDescription() string { + return "Get or set the kick message" +} + +func (j kickMessage) 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.UserJoinMessage) + api.Send(tgbotapi.NewMessage(update.Message.Chat.ID, "Current KickMessage (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 + } + + newJoinMessage := strings.Join(args[2:], " ") + group.UserJoinMessage = newJoinMessage + 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.UserKickMessage) + msg.ParseMode = "Markdown" + api.Send(tgbotapi.NewMessage(update.Message.Chat.ID, "Example KickMessage")) + _, 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 kickMessage) AllowChatType(chat *tgbotapi.Chat) bool { + return chat.IsGroup() || chat.IsSuperGroup() +} + +func (j kickMessage) AllowMember(member *tgbotapi.ChatMember) bool { + return member.IsAdministrator() || member.IsCreator() +} + +func (j kickMessage) AllowEveryMember() bool { + return false +} diff --git a/cli/commands/leavemessage.go b/cli/commands/leavemessage.go index 1d9c8ec..98fcc03 100644 --- a/cli/commands/leavemessage.go +++ b/cli/commands/leavemessage.go @@ -33,7 +33,7 @@ func (j leaveMessage) ExecuteCommand(api *tgbotapi.BotAPI, update *tgbotapi.Upda message := update.Message.Text parts := strings.Split(message, " ") - if len(parts) == 0 { + 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 } @@ -66,7 +66,7 @@ func (j leaveMessage) ExecuteCommand(api *tgbotapi.BotAPI, update *tgbotapi.Upda } //TODO Create a replace for placeholder - msg := tgbotapi.NewMessage(update.Message.Chat.ID, group.UserJoinMessage) + msg := tgbotapi.NewMessage(update.Message.Chat.ID, group.UserLeaveMessage) msg.ParseMode = "Markdown" api.Send(tgbotapi.NewMessage(update.Message.Chat.ID, "Example LeaveMessage")) _, err = api.Send(msg) diff --git a/main.go b/main.go index 852f98c..8fc2df5 100644 --- a/main.go +++ b/main.go @@ -79,6 +79,7 @@ func InitialCommandManager(bot *tgbotapi.BotAPI, service logic.GroupHelperServic cm.RegisterCommand(commands.NewHelpCommand(cm)) cm.RegisterCommand(commands.NewJoinMessage(service)) cm.RegisterCommand(commands.NewLeaveMessage(service)) + cm.RegisterCommand(commands.NewKickMessage(service)) return cm }