parent
130aabda81
commit
fed4a52075
@ -91,7 +91,7 @@ func (p *Pinner) PinBattleAlert() {
|
||||
pinChatMessageConfig := tgbotapi.PinChatMessageConfig{
|
||||
ChatID: pinnableMessage.Chat.ID,
|
||||
MessageID: pinnableMessage.MessageID,
|
||||
DisableNotification: true,
|
||||
DisableNotification: false,
|
||||
}
|
||||
|
||||
_, err = c.Bot.PinChatMessage(pinChatMessageConfig)
|
||||
|
@ -18,6 +18,11 @@ func (r *Router) routeGroupRequest(update *tgbotapi.Update, playerRaw *dbmapping
|
||||
var ebMsg = regexp.MustCompile("(\\s|^|ЗА|За|зА|за)(Е|е|Ё|ё)(Б|б)(\\s|Л|л|А|а|Т|т|У|у|Е|е|Ё|ё|И|и)")
|
||||
var piMsg = regexp.MustCompile("(П|п)(И|и)(З|з)(Д|д)")
|
||||
|
||||
squadHandled := c.Squader.ProcessMessage(update, chatRaw)
|
||||
if squadHandled != "ok" {
|
||||
return squadHandled
|
||||
}
|
||||
|
||||
// Welcomes
|
||||
if update.Message.NewChatMembers != nil {
|
||||
newUsers := *update.Message.NewChatMembers
|
||||
|
@ -38,6 +38,19 @@ func (s *Squader) GetSquadByID(squadID int) (dbmapping.SquadChat, bool) {
|
||||
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
|
||||
func (s *Squader) GetAllSquadChats() ([]dbmapping.Chat, bool) {
|
||||
groupChats := []dbmapping.Chat{}
|
||||
@ -51,6 +64,19 @@ func (s *Squader) GetAllSquadChats() ([]dbmapping.Chat, bool) {
|
||||
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
|
||||
func (s *Squader) GetUserRolesInSquads(playerRaw *dbmapping.Player) ([]dbmapping.SquadPlayerFull, bool) {
|
||||
userRoles := []dbmapping.SquadPlayerFull{}
|
||||
@ -78,3 +104,29 @@ func (s *Squader) GetUserRolesInSquads(playerRaw *dbmapping.Player) ([]dbmapping
|
||||
|
||||
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"
|
||||
}
|
||||
|
@ -173,9 +173,21 @@ func (s *Squader) getUserRoleForSquad(squadID int, playerID int) string {
|
||||
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 {
|
||||
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 {
|
||||
c.Log.Debug(err.Error())
|
||||
}
|
||||
@ -187,6 +199,55 @@ func (s *Squader) isUserAnyCommander(playerID int) bool {
|
||||
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 {
|
||||
message := "*Отряд уже существует*\n"
|
||||
message += "Проверьте, правильно ли вы ввели команду, и повторите попытку."
|
||||
@ -354,3 +415,74 @@ func (s *Squader) CreateSquad(update *tgbotapi.Update) string {
|
||||
|
||||
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"
|
||||
}
|
||||
|
@ -13,12 +13,17 @@ type SquaderInterface interface {
|
||||
Init()
|
||||
|
||||
GetAllSquadChats() ([]dbmapping.Chat, bool)
|
||||
GetAllSquadFloodChats() ([]dbmapping.Chat, bool)
|
||||
GetAvailableSquadChatsForUser(playerRaw *dbmapping.Player) ([]dbmapping.Chat, bool)
|
||||
GetSquadByID(squadID int) (dbmapping.SquadChat, bool)
|
||||
GetUserRolesInSquads(playerRaw *dbmapping.Player) ([]dbmapping.SquadPlayerFull, bool)
|
||||
IsChatASquadEnabled(chatRaw *dbmapping.Chat) string
|
||||
|
||||
AddUserToSquad(update *tgbotapi.Update, adderRaw *dbmapping.Player) string
|
||||
CreateSquad(update *tgbotapi.Update) string
|
||||
|
||||
SquadInfo(update *tgbotapi.Update, playerRaw *dbmapping.Player) string
|
||||
SquadsList(update *tgbotapi.Update, playerRaw *dbmapping.Player) string
|
||||
|
||||
ProcessMessage(update *tgbotapi.Update, chatRaw *dbmapping.Chat) string
|
||||
}
|
||||
|
Reference in New Issue
Block a user