Skip to content

Commit

Permalink
Make findTrollHouses run concurrently and return multiple groups
Browse files Browse the repository at this point in the history
  • Loading branch information
ryukinix committed Jul 23, 2020
1 parent 7aad9ec commit 2c092c2
Showing 1 changed file with 30 additions and 19 deletions.
49 changes: 30 additions & 19 deletions troll_shield.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"io"
logger "log"
"os"
"strings"
"sync"

tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
)
Expand All @@ -31,23 +33,37 @@ var log = logger.New(os.Stderr, "", logger.LstdFlags)

// findTrollHouse return the troll house group name if is well-known
// otherwise, returns a empty string
func findTrollHouse(bot *tgbotapi.BotAPI, userID int) (string, error) {
var error error
func findTrollHouses(bot *tgbotapi.BotAPI, userID int) string {
ch := make(chan string, len(trollGroups))
var wait sync.WaitGroup
for _, trollGroup := range trollGroups {
c, err := bot.GetChatMember(tgbotapi.ChatConfigWithUser{
SuperGroupUsername: trollGroup,
UserID: userID,
})
if err != nil {
error = err
continue
}
if c.IsMember() || c.IsCreator() || c.IsAdministrator() {
return trollGroup, nil
wait.Add(1)
go func(group string) {
defer wait.Done()
c, _ := bot.GetChatMember(tgbotapi.ChatConfigWithUser{
SuperGroupUsername: group,
UserID: userID,
})
if c.IsMember() || c.IsCreator() || c.IsAdministrator() {
ch <- group
} else {
ch <- ""
}
}(trollGroup)

}
go func() {
wait.Wait()
close(ch)
}()
var houses []string
for house := range ch {
if house != "" {
houses = append(houses, house)
}
}

return "", error
return strings.Join(houses, ", ")
}

// messageEvent: return true if is a message event
Expand Down Expand Up @@ -132,12 +148,7 @@ func main() {

if newChatMemberEvent(&update) {
for _, member := range *update.Message.NewChatMembers {
trollHouse, err := findTrollHouse(bot, member.ID)
if err != nil {
log.Printf("[!] findTrollHouse returned a error: %v", err)
}

if trollHouse != "" {
if trollHouse := findTrollHouses(bot, member.ID); trollHouse != "" {
chatMember := tgbotapi.ChatMemberConfig{
ChatID: update.Message.Chat.ID,
UserID: member.ID,
Expand Down

0 comments on commit 2c092c2

Please sign in to comment.