Implement join set and get command

This commit is contained in:
Alphyron 2020-04-25 23:21:57 +02:00
parent a44c3463d3
commit dd157bfd5a
8 changed files with 115 additions and 38 deletions

View File

@ -40,10 +40,7 @@ func (h helpCommand) ExecuteCommand(bot *tgbotapi.BotAPI, update *tgbotapi.Updat
_, err := bot.Send(msg)
if err != nil {
return false, err
}
return true, nil
return err == nil, nil
}
func (h helpCommand) AllowChatType(chat *tgbotapi.Chat) bool {

View File

@ -38,10 +38,7 @@ func (i infoCommand) ExecuteCommand(bot *tgbotapi.BotAPI, update *tgbotapi.Updat
_, err := bot.Send(msg)
if err != nil {
return false, err
}
return true, nil
return err == nil, nil
}
func (i infoCommand) AllowChatType(*tgbotapi.Chat) bool {

View File

@ -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 joinMessage struct {
groupHelperService logic.GroupHelperService
}
func NewJoinMessage(groupHelperService logic.GroupHelperService) cli.Command {
return &joinMessage{
groupHelperService: groupHelperService,
}
}
func (j joinMessage) GetUsage() string {
return "/join (set/get) [message]"
}
func (j joinMessage) GetCommand() string {
return "/join"
}
func (j joinMessage) GetDescription() string {
return "Get or set the join message"
}
func (j joinMessage) 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.UserJoinMessage)
api.Send(tgbotapi.NewMessage(update.Message.Chat.ID, "Current JoinMessage (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.UserJoinMessage)
msg.ParseMode = "Markdown"
api.Send(tgbotapi.NewMessage(update.Message.Chat.ID, "Example JoinMessage"))
_, 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 joinMessage) AllowChatType(chat *tgbotapi.Chat) bool {
return chat.IsGroup() || chat.IsSuperGroup()
}
func (j joinMessage) AllowMember(member *tgbotapi.ChatMember) bool {
return member.IsAdministrator() || member.IsCreator()
}
func (j joinMessage) AllowEveryMember() bool {
return false
}

View File

@ -54,10 +54,8 @@ func (cm CommandManager) ExecuteUpdate(update *tgbotapi.Update) (bool, error) {
msg := tgbotapi.NewMessage(update.Message.Chat.ID, "You are not allowed to use this command.")
msg.ReplyToMessageID = update.Message.MessageID
_, err := cm.Bot.Send(msg)
if err != nil {
return false, err
}
return true, nil
return err == nil, nil
}
}

View File

@ -31,7 +31,7 @@ func (g groupHelperService) UpdateGroup(group *models.Group) (*models.Group, err
return nil, errors.New("you need set the group id to update an existing group")
}
return g.UpdateGroup(group)
return g.groupHelperRepo.UpdateGroup(group)
}
func (g groupHelperService) GetGroupByID(groupID int64) (*models.Group, error) {

View File

@ -2,7 +2,6 @@ package logic
import (
"git.dragon-labs.de/alphyron/group_helper/models"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
)
type GroupHelperService interface {
@ -22,20 +21,3 @@ type GroupHelperService interface {
ListGroups() ([]*models.Group, error)
ListUsers() ([]*models.User, error)
}
type CommandService interface {
HelpCommand(*tgbotapi.Message) error
InfoCommand(*tgbotapi.Message) error
PlaceholderCommand(*tgbotapi.Message) error
PrintJoinMessage(*tgbotapi.Message) error
PrintLeaveMessage(*tgbotapi.Message) error
PrintKickMessage(*tgbotapi.Message) error
PrintVerifiedMessage(*tgbotapi.Message) error
PrintGroupStatus() error
UpdateJoinMessage(*tgbotapi.Message) error
UpdateLeaveMessage(*tgbotapi.Message) error
UpdateKickMessage(*tgbotapi.Message) error
UpdateVerifiedMessage(*tgbotapi.Message) error
UpdateOnlineCheck(*tgbotapi.Message) error
UpdateForbidWriting(*tgbotapi.Message) error
}

14
main.go
View File

@ -44,9 +44,16 @@ func main() {
log.Fatal(err)
}
logic.NewGroupHelperService(groupHelperRepo)
err = groupHelperRepo.InitRepository()
cm := InitialCommandManager(bot)
if err != nil {
log.Println("Problem while initial the Database")
log.Fatal(err)
}
service := logic.NewGroupHelperService(groupHelperRepo)
cm := InitialCommandManager(bot, service)
u := tgbotapi.NewUpdate(0)
u.Timeout = 60
@ -62,7 +69,7 @@ func main() {
}
}
func InitialCommandManager(bot *tgbotapi.BotAPI) *cli.CommandManager {
func InitialCommandManager(bot *tgbotapi.BotAPI, service logic.GroupHelperService) *cli.CommandManager {
cm := &cli.CommandManager{
Commands: make([]cli.Command, 0),
Bot: bot,
@ -70,6 +77,7 @@ func InitialCommandManager(bot *tgbotapi.BotAPI) *cli.CommandManager {
cm.RegisterCommand(commands.NewInfoCommand())
cm.RegisterCommand(commands.NewHelpCommand(cm))
cm.RegisterCommand(commands.NewJoinMessage(service))
return cm
}

View File

@ -89,12 +89,17 @@ func (g groupHelperRepository) UpdateGroup(group *models.Group) (*models.Group,
func (g groupHelperRepository) GetGroupByID(id int64) (*models.Group, error) {
var group models.Group
err := g.Conn.Where("group_id = ?", id).First(&group).Error
if err != nil {
return nil, errors.New("problem while getting the group by his group id - " + err.Error())
defaultGroup := &models.Group{}
defaultGroup.FillDefaultValues()
defaultGroup.GroupID = id
err := g.Conn.Where("group_id = ?", id).First(&group)
if err.RecordNotFound() {
return g.CreateGroup(defaultGroup)
}
return &group, nil
return &group, err.Error
}
func (g groupHelperRepository) GetGroupDatabaseSize(group *models.Group) (int, error) {