2020-04-25 21:32:11 +00:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"git.dragon-labs.de/alphyron/group_helper/cli"
|
|
|
|
"git.dragon-labs.de/alphyron/group_helper/logic"
|
2020-06-27 16:38:59 +00:00
|
|
|
message2 "git.dragon-labs.de/alphyron/group_helper/message"
|
2020-05-09 18:54:42 +00:00
|
|
|
"git.dragon-labs.de/alphyron/group_helper/models"
|
2020-04-25 21:32:11 +00:00
|
|
|
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"
|
|
|
|
}
|
|
|
|
|
2020-05-09 18:54:42 +00:00
|
|
|
func (j leaveMessage) ExecuteCommand(api *tgbotapi.BotAPI, update *tgbotapi.Update, group *models.Group) (bool, error) {
|
2020-04-25 21:32:11 +00:00
|
|
|
message := update.Message.Text
|
|
|
|
parts := strings.Split(message, " ")
|
|
|
|
|
2020-04-25 21:47:37 +00:00
|
|
|
if len(parts) == 1 {
|
2020-04-25 21:32:11 +00:00
|
|
|
_, 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, " ")
|
|
|
|
|
|
|
|
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)"))
|
2020-05-09 18:54:42 +00:00
|
|
|
_, err := api.Send(msg)
|
2020-04-25 21:32:11 +00:00
|
|
|
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
|
2020-05-09 18:54:42 +00:00
|
|
|
group, err := j.groupHelperService.UpdateGroup(group)
|
2020-04-25 21:32:11 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2021-08-07 18:02:34 +00:00
|
|
|
msg := tgbotapi.NewMessage(update.Message.Chat.ID, message2.ReplaceMessage(group.UserLeaveMessage, update.Message.From, group))
|
2020-04-25 21:32:11 +00:00
|
|
|
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
|
|
|
|
}
|