From 746734ec904c39aa8bd76c203503b46e984a1650 Mon Sep 17 00:00:00 2001 From: Alphyron Date: Sun, 26 Apr 2020 00:49:41 +0200 Subject: [PATCH] :sparkles: Implement online check toggle command --- cli/commands/toogleonlinecheck.go | 62 +++++++++++++++++++++++++++++++ main.go | 1 + models/group.go | 8 ++-- 3 files changed, 68 insertions(+), 3 deletions(-) create mode 100644 cli/commands/toogleonlinecheck.go diff --git a/cli/commands/toogleonlinecheck.go b/cli/commands/toogleonlinecheck.go new file mode 100644 index 0000000..9bac12e --- /dev/null +++ b/cli/commands/toogleonlinecheck.go @@ -0,0 +1,62 @@ +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" + "strconv" +) + +type toggleOnlineCheckCommand struct { + groupHelperService logic.GroupHelperService +} + +func NewToggleOnlineCheckCommand(groupHelperService logic.GroupHelperService) cli.Command { + return &toggleOnlineCheckCommand{ + groupHelperService: groupHelperService, + } +} + +func (i toggleOnlineCheckCommand) GetUsage() string { + return "/toggleOnlineCheck" +} + +func (i toggleOnlineCheckCommand) GetCommand() string { + return "/toggleOnlineCheck" +} + +func (i toggleOnlineCheckCommand) GetDescription() string { + return "Toggle the setting for a online check when a user join" +} + +func (i toggleOnlineCheckCommand) ExecuteCommand(bot *tgbotapi.BotAPI, update *tgbotapi.Update) (bool, error) { + group, err := i.groupHelperService.GetGroupByID(update.Message.Chat.ID) + + if err != nil { + return false, err + } + + group.OnlineCheck = !group.OnlineCheck + group, err = i.groupHelperService.UpdateGroup(group) + + if err != nil { + return false, err + } + + msg := tgbotapi.NewMessage(update.Message.Chat.ID, "Update the group setting for OnlineCheck to: "+strconv.FormatBool(group.OnlineCheck)) + _, err = bot.Send(msg) + + return err == nil, err +} + +func (i toggleOnlineCheckCommand) AllowChatType(chat *tgbotapi.Chat) bool { + return chat.IsGroup() || chat.IsSuperGroup() +} + +func (i toggleOnlineCheckCommand) AllowMember(member *tgbotapi.ChatMember) bool { + return member.IsAdministrator() || member.IsCreator() +} + +func (i toggleOnlineCheckCommand) AllowEveryMember() bool { + return false +} diff --git a/main.go b/main.go index 90ba2d1..4313ede 100644 --- a/main.go +++ b/main.go @@ -81,6 +81,7 @@ func InitialCommandManager(bot *tgbotapi.BotAPI, service logic.GroupHelperServic cm.RegisterCommand(commands.NewLeaveMessage(service)) cm.RegisterCommand(commands.NewKickMessage(service)) cm.RegisterCommand(commands.NewVerifiedMessage(service)) + cm.RegisterCommand(commands.NewToggleOnlineCheckCommand(service)) return cm } diff --git a/models/group.go b/models/group.go index 8a2543b..76a74d9 100644 --- a/models/group.go +++ b/models/group.go @@ -9,7 +9,8 @@ type Group struct { UserLeaveMessage string `gorm:"column:msg_user_leave;type:text"` UserKickMessage string `gorm:"column:msg_user_kick;type:text"` ForbidWriting bool `gorm:"column:forbid_writing"` - OnlineCheck int `gorm:"column:kick_cooldown"` + OnlineCheck bool `gorm:"column:online_check"` + KickCooldown int `gorm:"column:kick_cooldown"` Users []*User `gorm:"many2many:GroupUser;"` } @@ -30,7 +31,8 @@ func (group *Group) FillDefaultValues() { group.UserVerifiedMessage = "[{{user.firstname}} {{user.lastname}}](tg://user?id={{user.id}}) is now permitted to stay in the chat!" } - if group.OnlineCheck == 0 { - group.OnlineCheck = 5 + if group.KickCooldown == 0 { + group.KickCooldown = 5 } + }