feat: add commands

This commit is contained in:
Lennard Brinkhaus 2024-10-28 21:39:17 +01:00
parent f2b432a0cc
commit 2ae3e4f196
3 changed files with 139 additions and 2 deletions

View File

@ -0,0 +1,70 @@
package commands
import (
"git.dragon-labs.de/alphyron/group_helper/cli"
"git.dragon-labs.de/alphyron/group_helper/models"
"git.dragon-labs.de/alphyron/group_helper/obj"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
"strconv"
"strings"
)
package commands
import (
"git.dragon-labs.de/alphyron/group_helper/cli"
"git.dragon-labs.de/alphyron/group_helper/models"
"git.dragon-labs.de/alphyron/group_helper/obj"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
"strconv"
"strings"
)
type listVerifyCommand struct {
verifyData *obj.VerifyData
}
func (w listVerifyCommand) GetUsage() string {
return "/verifylist"
}
func (w listVerifyCommand) GetCommand() string {
return "/verifylist"
}
func (w listVerifyCommand) GetDescription() string {
return "List all UserIDs that try to verify right now"
}
func (w listVerifyCommand) ExecuteCommand(api *tgbotapi.BotAPI, update *tgbotapi.Update, group *models.Group) (bool, error) {
currentUsers := make([]string, 0)
for _, user := range w.verifyData.Users {
if user.GroupID != group.GroupID {
continue
}
currentUsers = append(currentUsers, strconv.Itoa(user.UserID))
}
_, err := api.Send(tgbotapi.NewMessage(update.Message.Chat.ID, "Users: [" + strings.Join(currentUsers, ",") + "]"))
return err != nil, err
}
func (w listVerifyCommand) AllowChatType(chat *tgbotapi.Chat) bool {
return chat.IsGroup() || chat.IsSuperGroup()
}
func (w listVerifyCommand) AllowMember(member *tgbotapi.ChatMember) bool {
return member.IsAdministrator() || member.IsCreator()
}
func (w listVerifyCommand) AllowEveryMember() bool {
return false
}
func NewListVerifyCommand(verifyData *obj.VerifyData) cli.Command {
return &listVerifyCommand{
verifyData: verifyData,
}
}

65
cli/commands/whitelist.go Normal file
View File

@ -0,0 +1,65 @@
package commands
import (
"git.dragon-labs.de/alphyron/group_helper/cli"
"git.dragon-labs.de/alphyron/group_helper/models"
"git.dragon-labs.de/alphyron/group_helper/obj"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
"strconv"
"strings"
)
type whitelistCommand struct {
verifyData *obj.VerifyData
}
func (w whitelistCommand) GetUsage() string {
return "/whitelist [user_id]"
}
func (w whitelistCommand) GetCommand() string {
return "/whitelist"
}
func (w whitelistCommand) GetDescription() string {
return "Whitelist an given User so the bot cancel the JOIN Validation"
}
func (w whitelistCommand) 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"+w.GetUsage()))
return err != nil, err
}
userID, err := strconv.Atoi(parts[1])
if err != nil {
_, err := api.Send(tgbotapi.NewMessage(update.Message.Chat.ID, "Please provide an integer as ID:\n"+w.GetUsage()))
return err != nil, err
}
w.verifyData.RemoveUser(userID, group.GroupID)
_, err = api.Send(tgbotapi.NewMessage(update.Message.Chat.ID, "User is free :D"))
return err != nil, err
}
func (w whitelistCommand) AllowChatType(chat *tgbotapi.Chat) bool {
return chat.IsGroup() || chat.IsSuperGroup()
}
func (w whitelistCommand) AllowMember(member *tgbotapi.ChatMember) bool {
return member.IsAdministrator() || member.IsCreator()
}
func (w whitelistCommand) AllowEveryMember() bool {
return false
}
func NewWhitelistCommand(verifyData *obj.VerifyData) cli.Command {
return &whitelistCommand{
verifyData: verifyData,
}
}

View File

@ -62,7 +62,7 @@ func main() {
Users: make([]*obj.VerifyUser, 0),
}
cm := InitialCommandManager(bot, service)
cm := InitialCommandManager(bot, service, userData)
rm := InitialRoutineManager(bot, cm, service, userData)
verifyTicker := time.NewTicker(1 * time.Minute)
@ -90,7 +90,7 @@ func InitialRoutineManager(bot *tgbotapi.BotAPI, commandManager *cli.CommandMana
return rm
}
func InitialCommandManager(bot *tgbotapi.BotAPI, service logic.GroupHelperService) *cli.CommandManager {
func InitialCommandManager(bot *tgbotapi.BotAPI, service logic.GroupHelperService, verifyData *obj.VerifyData) *cli.CommandManager {
cm := &cli.CommandManager{
Commands: make([]cli.Command, 0),
Bot: bot,
@ -109,6 +109,8 @@ func InitialCommandManager(bot *tgbotapi.BotAPI, service logic.GroupHelperServic
cm.RegisterCommand(commands.NewBroadcastCommand())
cm.RegisterCommand(commands.NewRules(service))
cm.RegisterCommand(commands.NewSetRules(service))
cm.RegisterCommand(commands.NewWhitelistCommand(verifyData))
cm.RegisterCommand(commands.NewListVerifyCommand(verifyData))
return cm
}