Archived
1

Bot administration functions for squads chats

Closes #11
Closes #15
This commit is contained in:
Vladimir Hodakov 2017-11-26 06:48:13 +04:00
parent 130aabda81
commit fed4a52075
5 changed files with 196 additions and 2 deletions

View File

@ -91,7 +91,7 @@ func (p *Pinner) PinBattleAlert() {
pinChatMessageConfig := tgbotapi.PinChatMessageConfig{ pinChatMessageConfig := tgbotapi.PinChatMessageConfig{
ChatID: pinnableMessage.Chat.ID, ChatID: pinnableMessage.Chat.ID,
MessageID: pinnableMessage.MessageID, MessageID: pinnableMessage.MessageID,
DisableNotification: true, DisableNotification: false,
} }
_, err = c.Bot.PinChatMessage(pinChatMessageConfig) _, err = c.Bot.PinChatMessage(pinChatMessageConfig)

View File

@ -18,6 +18,11 @@ func (r *Router) routeGroupRequest(update *tgbotapi.Update, playerRaw *dbmapping
var ebMsg = regexp.MustCompile("(\\s|^|ЗА|За|зА|за)(Е|е|Ё|ё)(Б|б)(\\s|Л|л|А|а|Т|т|У|у|Е|е|Ё|ё|И|и)") var ebMsg = regexp.MustCompile("(\\s|^|ЗА|За|зА|за)(Е|е|Ё|ё)(Б|б)(\\s|Л|л|А|а|Т|т|У|у|Е|е|Ё|ё|И|и)")
var piMsg = regexp.MustCompile("(П|п)(И|и)(З|з)(Д|д)") var piMsg = regexp.MustCompile("(П|п)(И|и)(З|з)(Д|д)")
squadHandled := c.Squader.ProcessMessage(update, chatRaw)
if squadHandled != "ok" {
return squadHandled
}
// Welcomes // Welcomes
if update.Message.NewChatMembers != nil { if update.Message.NewChatMembers != nil {
newUsers := *update.Message.NewChatMembers newUsers := *update.Message.NewChatMembers

View File

@ -38,6 +38,19 @@ func (s *Squader) GetSquadByID(squadID int) (dbmapping.SquadChat, bool) {
return squadFull, true return squadFull, true
} }
// GetAvailableSquadChatsForUser returns squad chats which user can join
func (s *Squader) GetAvailableSquadChatsForUser(playerRaw *dbmapping.Player) ([]dbmapping.Chat, bool) {
groupChats := []dbmapping.Chat{}
err := c.Db.Select(&groupChats, c.Db.Rebind("SELECT ch.* FROM chats ch, squads s, squads_players sp WHERE (s.chat_id=ch.id OR s.flood_chat_id=ch.id) AND sp.player_id = ? AND s.id = sp.squad_id"), playerRaw.ID)
if err != nil {
c.Log.Error(err)
return groupChats, false
}
return groupChats, true
}
// GetAllSquadChats returns all main squad chats // GetAllSquadChats returns all main squad chats
func (s *Squader) GetAllSquadChats() ([]dbmapping.Chat, bool) { func (s *Squader) GetAllSquadChats() ([]dbmapping.Chat, bool) {
groupChats := []dbmapping.Chat{} groupChats := []dbmapping.Chat{}
@ -51,6 +64,19 @@ func (s *Squader) GetAllSquadChats() ([]dbmapping.Chat, bool) {
return groupChats, true return groupChats, true
} }
// GetAllSquadFloodChats returns all flood squad chats
func (s *Squader) GetAllSquadFloodChats() ([]dbmapping.Chat, bool) {
groupChats := []dbmapping.Chat{}
err := c.Db.Select(&groupChats, "SELECT ch.* FROM chats ch, squads s WHERE s.flood_chat_id=ch.id")
if err != nil {
c.Log.Error(err)
return groupChats, false
}
return groupChats, true
}
// GetUserRolesInSquads lists all user roles // GetUserRolesInSquads lists all user roles
func (s *Squader) GetUserRolesInSquads(playerRaw *dbmapping.Player) ([]dbmapping.SquadPlayerFull, bool) { func (s *Squader) GetUserRolesInSquads(playerRaw *dbmapping.Player) ([]dbmapping.SquadPlayerFull, bool) {
userRoles := []dbmapping.SquadPlayerFull{} userRoles := []dbmapping.SquadPlayerFull{}
@ -78,3 +104,29 @@ func (s *Squader) GetUserRolesInSquads(playerRaw *dbmapping.Player) ([]dbmapping
return userRoles, true return userRoles, true
} }
// IsChatASquadEnabled checks group chat for restricting actions for squad
func (s *Squader) IsChatASquadEnabled(chatRaw *dbmapping.Chat) string {
mainChats, ok := s.GetAllSquadChats()
if !ok {
return "no"
}
floodChats, ok := s.GetAllSquadFloodChats()
if !ok {
return "no"
}
for i := range mainChats {
if *chatRaw == mainChats[i] {
return "main"
}
}
for i := range floodChats {
if *chatRaw == floodChats[i] {
return "flood"
}
}
return "no"
}

View File

@ -173,9 +173,21 @@ func (s *Squader) getUserRoleForSquad(squadID int, playerID int) string {
return squadPlayer.UserType return squadPlayer.UserType
} }
func (s *Squader) deleteFloodMessage(update *tgbotapi.Update) {
deleteMessageConfig := tgbotapi.DeleteMessageConfig{
ChatID: update.Message.Chat.ID,
MessageID: update.Message.MessageID,
}
_, err := c.Bot.DeleteMessage(deleteMessageConfig)
if err != nil {
c.Log.Error(err.Error())
}
}
func (s *Squader) isUserAnyCommander(playerID int) bool { func (s *Squader) isUserAnyCommander(playerID int) bool {
squadPlayers := []dbmapping.SquadPlayer{} squadPlayers := []dbmapping.SquadPlayer{}
err := c.Db.Select(&squadPlayers, c.Db.Rebind("SELECT * FROM squads_players WHERE player_id=?"), playerID) err := c.Db.Select(&squadPlayers, c.Db.Rebind("SELECT * FROM squads_players WHERE player_id=? AND user_type='commander'"), playerID)
if err != nil { if err != nil {
c.Log.Debug(err.Error()) c.Log.Debug(err.Error())
} }
@ -187,6 +199,55 @@ func (s *Squader) isUserAnyCommander(playerID int) bool {
return false return false
} }
func (s *Squader) getCommandersForSquadViaChat(chatRaw *dbmapping.Chat) ([]dbmapping.Player, bool) {
commanders := []dbmapping.Player{}
err := c.Db.Select(&commanders, c.Db.Rebind("SELECT p.* FROM players p, squads_players sp, squads s WHERE (s.chat_id=? OR s.flood_chat_id=?) AND sp.squad_id = s.id AND sp.user_type = 'commander' AND sp.player_id = p.id"), chatRaw.ID, chatRaw.ID)
if err != nil {
c.Log.Debug(err.Error())
return commanders, false
}
return commanders, true
}
func (s *Squader) kickUserFromSquadChat(user *tgbotapi.User, chatRaw *dbmapping.Chat) {
chatUserConfig := tgbotapi.ChatMemberConfig{
ChatID: chatRaw.TelegramID,
UserID: user.ID,
}
kickConfig := tgbotapi.KickChatMemberConfig{
ChatMemberConfig: chatUserConfig,
UntilDate: 1893456000,
}
_, err := c.Bot.KickChatMember(kickConfig)
if err != nil {
c.Log.Error(err.Error())
}
suerName := ""
if user.UserName != "" {
suerName = "@" + user.UserName
} else {
suerName = user.FirstName
if user.LastName != "" {
suerName += " " + user.LastName
}
}
commanders, ok := s.getCommandersForSquadViaChat(chatRaw)
if ok {
for i := range commanders {
message := "Некто " + c.Users.FormatUsername(suerName) + " попытался зайти в чат _" + chatRaw.Name + "_ и был изгнан ботом, так как не имеет на это прав."
msg := tgbotapi.NewMessage(int64(commanders[i].TelegramID), message)
msg.ParseMode = "Markdown"
c.Bot.Send(msg)
}
}
}
func (s *Squader) squadCreationDuplicate(update *tgbotapi.Update) string { func (s *Squader) squadCreationDuplicate(update *tgbotapi.Update) string {
message := "*Отряд уже существует*\n" message := "*Отряд уже существует*\n"
message += "Проверьте, правильно ли вы ввели команду, и повторите попытку." message += "Проверьте, правильно ли вы ввели команду, и повторите попытку."
@ -354,3 +415,74 @@ func (s *Squader) CreateSquad(update *tgbotapi.Update) string {
return s.squadCreationSuccess(update) return s.squadCreationSuccess(update)
} }
// ProcessMessage handles all squad-specified administration actions
func (s *Squader) ProcessMessage(update *tgbotapi.Update, chatRaw *dbmapping.Chat) string {
// It will pass message or do some extra actions
// If it returns "ok", we can pass message to router, otherwise we will stop here
processMain := false
processFlood := false
messageProcessed := false
switch s.IsChatASquadEnabled(chatRaw) {
case "main":
processMain = true
case "flood":
processFlood = true
default:
return "ok"
}
// Kicking non-squad members from any chat
if processMain || processFlood {
if update.Message.NewChatMembers != nil {
newUsers := *update.Message.NewChatMembers
if len(newUsers) > 0 {
for i := range newUsers {
playerRaw, ok := c.Users.GetOrCreatePlayer(newUsers[i].ID)
if !ok {
s.kickUserFromSquadChat(&newUsers[i], chatRaw)
messageProcessed = true
}
availableChats, ok := s.GetAvailableSquadChatsForUser(&playerRaw)
if !ok {
s.kickUserFromSquadChat(&newUsers[i], chatRaw)
messageProcessed = true
}
isChatValid := false
for i := range availableChats {
if availableChats[i] == *chatRaw {
isChatValid = true
}
}
if !isChatValid {
s.kickUserFromSquadChat(&newUsers[i], chatRaw)
messageProcessed = true
}
}
}
}
}
if processMain {
c.Log.Debug("Message found in one of squad's main chats.")
talker, ok := c.Users.GetOrCreatePlayer(update.Message.From.ID)
if !ok {
s.deleteFloodMessage(update)
messageProcessed = true
}
if (update.Message.From.UserName != "i2_bot") && (update.Message.From.UserName != "i2_bot_dev") && !s.isUserAnyCommander(talker.ID) {
s.deleteFloodMessage(update)
messageProcessed = true
}
}
if messageProcessed {
return "fail"
}
return "ok"
}

View File

@ -13,12 +13,17 @@ type SquaderInterface interface {
Init() Init()
GetAllSquadChats() ([]dbmapping.Chat, bool) GetAllSquadChats() ([]dbmapping.Chat, bool)
GetAllSquadFloodChats() ([]dbmapping.Chat, bool)
GetAvailableSquadChatsForUser(playerRaw *dbmapping.Player) ([]dbmapping.Chat, bool)
GetSquadByID(squadID int) (dbmapping.SquadChat, bool) GetSquadByID(squadID int) (dbmapping.SquadChat, bool)
GetUserRolesInSquads(playerRaw *dbmapping.Player) ([]dbmapping.SquadPlayerFull, bool) GetUserRolesInSquads(playerRaw *dbmapping.Player) ([]dbmapping.SquadPlayerFull, bool)
IsChatASquadEnabled(chatRaw *dbmapping.Chat) string
AddUserToSquad(update *tgbotapi.Update, adderRaw *dbmapping.Player) string AddUserToSquad(update *tgbotapi.Update, adderRaw *dbmapping.Player) string
CreateSquad(update *tgbotapi.Update) string CreateSquad(update *tgbotapi.Update) string
SquadInfo(update *tgbotapi.Update, playerRaw *dbmapping.Player) string SquadInfo(update *tgbotapi.Update, playerRaw *dbmapping.Player) string
SquadsList(update *tgbotapi.Update, playerRaw *dbmapping.Player) string SquadsList(update *tgbotapi.Update, playerRaw *dbmapping.Player) string
ProcessMessage(update *tgbotapi.Update, chatRaw *dbmapping.Chat) string
} }