🐛 Fix a little channel bug and add /rules and /setrules command

This commit is contained in:
Lennard Brinkhaus 2021-08-07 20:02:34 +02:00
parent 7d555687f4
commit d475c1ff9a
Signed by: lennard.brinkhaus
GPG Key ID: D4C9B6A87F97B070
16 changed files with 162 additions and 24 deletions

View File

@ -23,7 +23,7 @@ func CheckForLeavingUser(ticker *time.Ticker, quitChannel <-chan struct{}, group
} }
for _, group := range groups { for _, group := range groups {
time.Sleep(500 * time.Millisecond) time.Sleep(100 * time.Millisecond)
config := tgbotapi.ChatConfig{ config := tgbotapi.ChatConfig{
ChatID: group.GroupID, ChatID: group.GroupID,
@ -55,7 +55,7 @@ func CheckForLeavingUser(ticker *time.Ticker, quitChannel <-chan struct{}, group
} }
func checkGroupUser(group *models.Group, groupHelperService logic.GroupHelperService, bot *tgbotapi.BotAPI) { func checkGroupUser(group *models.Group, groupHelperService logic.GroupHelperService, bot *tgbotapi.BotAPI) {
rate := time.Second / 5 rate := time.Second / 25
throttle := time.Tick(rate) throttle := time.Tick(rate)
users, err := groupHelperService.ListGroupUsers(group) users, err := groupHelperService.ListGroupUsers(group)
@ -79,17 +79,9 @@ func checkGroupUser(group *models.Group, groupHelperService logic.GroupHelperSer
continue continue
} }
if !(member.User.IsBot || member.User.FirstName == "Telegram") { if !(member.User == nil || member.User.FirstName == "" || member.User.IsBot || member.User.FirstName == "Telegram") {
if member.User == nil || member.User.FirstName == "" {
member.User = &tgbotapi.User{
ID: -1,
FirstName: "Unknown",
IsBot: false,
}
}
leaveMessage := group.UserLeaveMessage leaveMessage := group.UserLeaveMessage
leaveMessage = message.ReplaceMessage(leaveMessage, member.User) leaveMessage = message.ReplaceMessage(leaveMessage, member.User, group)
msg := tgbotapi.NewMessage(group.GroupID, leaveMessage) msg := tgbotapi.NewMessage(group.GroupID, leaveMessage)
msg.ParseMode = "Markdown" msg.ParseMode = "Markdown"

View File

@ -76,7 +76,7 @@ func CheckUnverifiedUser(ticker *time.Ticker, quitChannel <-chan struct{}, group
} }
kickMessage := groups[user.GroupID].UserKickMessage kickMessage := groups[user.GroupID].UserKickMessage
kickMessage = message.ReplaceMessage(kickMessage, member.User) kickMessage = message.ReplaceMessage(kickMessage, member.User, groups[user.GroupID])
msg := tgbotapi.NewMessage(user.GroupID, kickMessage) msg := tgbotapi.NewMessage(user.GroupID, kickMessage)
msg.ParseMode = "Markdown" msg.ParseMode = "Markdown"

View File

@ -30,7 +30,7 @@ func (i infoCommand) ExecuteCommand(bot *tgbotapi.BotAPI, update *tgbotapi.Updat
"General Information to this Bot\n" + "General Information to this Bot\n" +
"===============================\n" + "===============================\n" +
"Developer: @Alphyron\n" + "Developer: @Alphyron\n" +
"Version: 2.2.7\n" + "Version: 2.3.0\n" +
"Git: [Gitea Repository](https://git.dragon-labs.de/alphyron/group_assistant)\n" + "Git: [Gitea Repository](https://git.dragon-labs.de/alphyron/group_assistant)\n" +
"===============================" "==============================="

View File

@ -62,7 +62,7 @@ func (j joinMessage) ExecuteCommand(api *tgbotapi.BotAPI, update *tgbotapi.Updat
return false, err return false, err
} }
msg := tgbotapi.NewMessage(update.Message.Chat.ID, message2.ReplaceMessage(group.UserJoinMessage, update.Message.From)) msg := tgbotapi.NewMessage(update.Message.Chat.ID, message2.ReplaceMessage(group.UserJoinMessage, update.Message.From, group))
msg.ParseMode = "Markdown" msg.ParseMode = "Markdown"
api.Send(tgbotapi.NewMessage(update.Message.Chat.ID, "Example JoinMessage")) api.Send(tgbotapi.NewMessage(update.Message.Chat.ID, "Example JoinMessage"))
_, err = api.Send(msg) _, err = api.Send(msg)

View File

@ -62,7 +62,7 @@ func (j kickMessage) ExecuteCommand(api *tgbotapi.BotAPI, update *tgbotapi.Updat
return false, err return false, err
} }
msg := tgbotapi.NewMessage(update.Message.Chat.ID, message2.ReplaceMessage(group.UserKickMessage, update.Message.From)) msg := tgbotapi.NewMessage(update.Message.Chat.ID, message2.ReplaceMessage(group.UserKickMessage, update.Message.From, group))
msg.ParseMode = "Markdown" msg.ParseMode = "Markdown"
api.Send(tgbotapi.NewMessage(update.Message.Chat.ID, "Example KickMessage")) api.Send(tgbotapi.NewMessage(update.Message.Chat.ID, "Example KickMessage"))
_, err = api.Send(msg) _, err = api.Send(msg)

View File

@ -62,7 +62,7 @@ func (j leaveMessage) ExecuteCommand(api *tgbotapi.BotAPI, update *tgbotapi.Upda
return false, err return false, err
} }
msg := tgbotapi.NewMessage(update.Message.Chat.ID, message2.ReplaceMessage(group.UserLeaveMessage, update.Message.From)) msg := tgbotapi.NewMessage(update.Message.Chat.ID, message2.ReplaceMessage(group.UserLeaveMessage, update.Message.From, group))
msg.ParseMode = "Markdown" msg.ParseMode = "Markdown"
api.Send(tgbotapi.NewMessage(update.Message.Chat.ID, "Example LeaveMessage")) api.Send(tgbotapi.NewMessage(update.Message.Chat.ID, "Example LeaveMessage"))
_, err = api.Send(msg) _, err = api.Send(msg)

48
cli/commands/rules.go Normal file
View File

@ -0,0 +1,48 @@
package commands
import (
"git.dragon-labs.de/alphyron/group_helper/cli"
"git.dragon-labs.de/alphyron/group_helper/logic"
"git.dragon-labs.de/alphyron/group_helper/models"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
)
type rules struct {
groupHelperService logic.GroupHelperService
}
func NewRules(groupHelperService logic.GroupHelperService) cli.Command {
return &rules{
groupHelperService: groupHelperService,
}
}
func (r rules) GetUsage() string {
return "/rules"
}
func (r rules) GetCommand() string {
return "/rules"
}
func (r rules) GetDescription() string {
return "Get the rules of this group"
}
func (r rules) ExecuteCommand(api *tgbotapi.BotAPI, update *tgbotapi.Update, group *models.Group) (bool, error) {
msg := tgbotapi.NewMessage(update.Message.Chat.ID, group.Rules)
_, err := api.Send(msg)
return err != nil, err
}
func (r rules) AllowChatType(chat *tgbotapi.Chat) bool {
return true
}
func (r rules) AllowMember(member *tgbotapi.ChatMember) bool {
return true
}
func (r rules) AllowEveryMember() bool {
return true
}

68
cli/commands/setrules.go Normal file
View File

@ -0,0 +1,68 @@
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
}

View File

@ -62,7 +62,7 @@ func (j verifiedMessage) ExecuteCommand(api *tgbotapi.BotAPI, update *tgbotapi.U
return false, err return false, err
} }
msg := tgbotapi.NewMessage(update.Message.Chat.ID, message2.ReplaceMessage(group.UserVerifiedMessage, update.Message.From)) msg := tgbotapi.NewMessage(update.Message.Chat.ID, message2.ReplaceMessage(group.UserVerifiedMessage, update.Message.From, group))
msg.ParseMode = "Markdown" msg.ParseMode = "Markdown"
api.Send(tgbotapi.NewMessage(update.Message.Chat.ID, "Example VerifiedMessage")) api.Send(tgbotapi.NewMessage(update.Message.Chat.ID, "Example VerifiedMessage"))
_, err = api.Send(msg) _, err = api.Send(msg)

View File

@ -66,7 +66,7 @@ func main() {
rm := InitialRoutineManager(bot, cm, service, userData) rm := InitialRoutineManager(bot, cm, service, userData)
verifyTicker := time.NewTicker(1 * time.Minute) verifyTicker := time.NewTicker(1 * time.Minute)
leaveTicker := time.NewTicker(1 * time.Minute) leaveTicker := time.NewTicker(5 * time.Minute)
quit := make(chan struct{}) quit := make(chan struct{})
go channels.CheckUnverifiedUser(verifyTicker, quit, service, bot, userData) go channels.CheckUnverifiedUser(verifyTicker, quit, service, bot, userData)
@ -107,6 +107,8 @@ func InitialCommandManager(bot *tgbotapi.BotAPI, service logic.GroupHelperServic
cm.RegisterCommand(commands.NewChatInfoCommand()) cm.RegisterCommand(commands.NewChatInfoCommand())
cm.RegisterCommand(commands.NewAdminGroupCommand(service)) cm.RegisterCommand(commands.NewAdminGroupCommand(service))
cm.RegisterCommand(commands.NewBroadcastCommand()) cm.RegisterCommand(commands.NewBroadcastCommand())
cm.RegisterCommand(commands.NewRules(service))
cm.RegisterCommand(commands.NewSetRules(service))
return cm return cm
} }

View File

@ -1,16 +1,18 @@
package message package message
import ( import (
"git.dragon-labs.de/alphyron/group_helper/models"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
"strconv" "strconv"
"strings" "strings"
) )
func ReplaceMessage(message string, user *tgbotapi.User) string { func ReplaceMessage(message string, user *tgbotapi.User, group *models.Group) string {
message = strings.Replace(message, "{{user.firstname}}", user.FirstName, -1) message = strings.Replace(message, "{{user.firstname}}", user.FirstName, -1)
message = strings.Replace(message, "{{user.lastname}}", user.LastName, -1) message = strings.Replace(message, "{{user.lastname}}", user.LastName, -1)
message = strings.Replace(message, "{{user.username}}", user.UserName, -1) message = strings.Replace(message, "{{user.username}}", user.UserName, -1)
message = strings.Replace(message, "{{user.name}}", user.UserName, -1) message = strings.Replace(message, "{{user.name}}", user.UserName, -1)
message = strings.Replace(message, "{{user.id}}", strconv.Itoa(user.ID), -1) message = strings.Replace(message, "{{user.id}}", strconv.Itoa(user.ID), -1)
message = strings.Replace(message, "{{group.rules}}", group.Rules, -1)
return message return message
} }

View File

@ -14,6 +14,7 @@ type Group struct {
Users []*User `gorm:"many2many:GroupUser;"` Users []*User `gorm:"many2many:GroupUser;"`
AdminGroupID uint64 `gorm:"column:admin_group_id"` AdminGroupID uint64 `gorm:"column:admin_group_id"`
ControlledGroups []*Group `gorm:"foreignkey:admin_group_id"` ControlledGroups []*Group `gorm:"foreignkey:admin_group_id"`
Rules string `gorm:"column:rules;type:text"`
} }
func (Group) TableName() string { func (Group) TableName() string {

View File

@ -94,6 +94,7 @@ func (g groupHelperRepository) UpdateGroup(group *models.Group) (*models.Group,
OnlineCheck: group.OnlineCheck, OnlineCheck: group.OnlineCheck,
KickCooldown: group.KickCooldown, KickCooldown: group.KickCooldown,
AdminGroupID: group.AdminGroupID, AdminGroupID: group.AdminGroupID,
Rules: group.Rules,
}).Error }).Error
return group, err return group, err
} }

View File

@ -73,6 +73,30 @@ func (d databaseRoutine) Update(botAPI *tgbotapi.BotAPI, update *tgbotapi.Update
return err return err
} }
_, err = d.groupHelperService.UserJoinGroup(user, group) isInGroup, err := d.groupHelperService.IsUserInGroup(user, group)
return err
if err != nil {
return err
}
if !isInGroup {
chatMember, err := botAPI.GetChatMember(tgbotapi.ChatConfigWithUser{
ChatID: group.GroupID,
UserID: update.Message.From.ID,
})
if err != nil {
return err
}
if chatMember.IsMember() || chatMember.IsCreator() || chatMember.IsAdministrator() {
_, err = d.groupHelperService.UserJoinGroup(user, group)
if err != nil {
return err
}
}
}
return nil
} }

View File

@ -53,7 +53,7 @@ func (j joinCheckerRoutine) Update(botAPI *tgbotapi.BotAPI, update *tgbotapi.Upd
markup := tgbotapi.NewInlineKeyboardMarkup(tasks...) markup := tgbotapi.NewInlineKeyboardMarkup(tasks...)
msg := tgbotapi.NewMessage(update.Message.Chat.ID, message2.ReplaceMessage(group.UserJoinMessage, &newUser)) msg := tgbotapi.NewMessage(update.Message.Chat.ID, message2.ReplaceMessage(group.UserJoinMessage, &newUser, group))
msg.ParseMode = "Markdown" msg.ParseMode = "Markdown"
msg.ReplyMarkup = markup msg.ReplyMarkup = markup

View File

@ -53,7 +53,7 @@ func (v verifierRoutine) Update(botAPI *tgbotapi.BotAPI, update *tgbotapi.Update
botAPI.AnswerCallbackQuery(tgbotapi.NewCallback(update.CallbackQuery.ID, "You are now verified. Welcome to the chat!")) botAPI.AnswerCallbackQuery(tgbotapi.NewCallback(update.CallbackQuery.ID, "You are now verified. Welcome to the chat!"))
verifyMessage := group.UserVerifiedMessage verifyMessage := group.UserVerifiedMessage
msg := tgbotapi.NewMessage(update.CallbackQuery.Message.Chat.ID, message2.ReplaceMessage(verifyMessage, update.CallbackQuery.From)) msg := tgbotapi.NewMessage(update.CallbackQuery.Message.Chat.ID, message2.ReplaceMessage(verifyMessage, update.CallbackQuery.From, group))
msg.ParseMode = "Markdown" msg.ParseMode = "Markdown"
_, err := botAPI.Send(msg) _, err := botAPI.Send(msg)