group_helper/cli/commands/setrules.go

69 lines
1.8 KiB
Go
Raw Normal View History

package commands
import (
"git.dragon-labs.de/alphyron/group_helper/cli"
"git.dragon-labs.de/alphyron/group_helper/logic"
message2 "git.dragon-labs.de/alphyron/group_helper/message"
"git.dragon-labs.de/alphyron/group_helper/models"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
"strings"
)
type setRules struct {
groupHelperService logic.GroupHelperService
}
func NewSetRules(groupHelperService logic.GroupHelperService) cli.Command {
return &setRules{
groupHelperService: groupHelperService,
}
}
func (r setRules) GetUsage() string {
return "/setrules [message]"
}
func (r setRules) GetCommand() string {
return "/setrules"
}
func (r setRules) GetDescription() string {
return "Set the rules of this group"
}
func (r setRules) ExecuteCommand(api *tgbotapi.BotAPI, update *tgbotapi.Update, group *models.Group) (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"+r.GetUsage()))
return err != nil, err
}
newRulesMessage := strings.Join(parts[1:], " ")
group.Rules = newRulesMessage
group, err := r.groupHelperService.UpdateGroup(group)
if err != nil {
return false, err
}
msg := tgbotapi.NewMessage(update.Message.Chat.ID, message2.ReplaceMessage(group.Rules, update.Message.From, group))
msg.ParseMode = "Markdown"
api.Send(tgbotapi.NewMessage(update.Message.Chat.ID, "Example Rules"))
_, err = api.Send(msg)
return err != nil, err
}
func (r setRules) AllowChatType(chat *tgbotapi.Chat) bool {
return true
}
func (r setRules) AllowMember(member *tgbotapi.ChatMember) bool {
return member.IsAdministrator() || member.IsCreator()
}
func (r setRules) AllowEveryMember() bool {
return false
}