Skip to content

Commit

Permalink
Add /pass command (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
ryukinix committed Aug 10, 2020
1 parent a5b8e84 commit 9c6e781
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 2 deletions.
10 changes: 10 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ func main() {
reportKills(bot, &update, kills)
}

if strings.HasPrefix(update.Message.Text, "/pass ") && fromAdminEvent(&update) {
addPassList(bot, &update)
}

// Exit automatically from group after the bot receive a message from it
for _, trollGroup := range trollGroups {
if fromChatEvent(&update, strings.TrimLeft(trollGroup, "@")) {
Expand All @@ -38,6 +42,12 @@ func main() {

if newChatMemberEvent(&update) {
for _, member := range *update.Message.NewChatMembers {
if pass, ok := hasPass(member); ok {
removePassList(bot, &update, pass)
welcomeMessage(bot, &update, member)
continue
}

if trollHouse := findTrollHouses(botHidden, member.ID); trollHouse != "" {
err := kickTroll(bot, &update, member, trollHouse)
if err == nil {
Expand Down
69 changes: 67 additions & 2 deletions troll_shield.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright 2020 the commonlispbr authors. All rights reserved
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package main

import (
Expand Down Expand Up @@ -39,6 +38,14 @@ var trollGroups = []string{
"@mlbrasil",
}

var passList = []string{}

var admins = []string{
"lerax",
"luksamuk",
"perkunos",
}

const logfile = "troll-shield.log"
const killsFile = "kills.txt"

Expand Down Expand Up @@ -192,7 +199,7 @@ func setupBot(envVar string) (*telegram.BotAPI, error) {
bot, err := telegram.NewBotAPI(token)

if err != nil {
return nil, fmt.Errorf("Setup %v failed with: %v", envVar, err)
return nil, fmt.Errorf("setup %v failed with: %v", envVar, err)
}

bot.Debug = true
Expand Down Expand Up @@ -266,3 +273,61 @@ func reportKills(bot TrollShieldBot, update *telegram.Update, kills int64) {
}
reply(bot, update, txt)
}

// parse:
// - /pass <@username>
// - /pass <FirstName [LastName]>
// and return what is available
func extractPassUserName(command string) string {
tokens := strings.Split(command, " ")
n := len(tokens)
return strings.Join(tokens[1:n], " ")
}

// If has pass, return true and and return the matched pass
func hasPass(user telegram.User) (string, bool) {
userName := getUserName(user)
for _, pass := range passList {
if strings.HasPrefix(userName, pass) || user.FirstName == pass {
return pass, true
}
}
return "", false
}

// remove pass list and reply the consumed pass list
func removePassList(bot TrollShieldBot, update *telegram.Update, pass string) {
// Remove the element at index i from a.
n := len(passList)
for i, p := range passList {
if p == pass {
// remove pass from passList
passList[i] = passList[n-1] // Copy last element to index i.
passList[n-1] = "" // Erase last element (write zero value).
passList = passList[:n-1] // Truncate slice.
}
}
reply(bot, update, fmt.Sprintf("O passe para %q foi consumido.", pass))
}

// check if a message cames from a @commonlispbr admin
func fromAdminEvent(update *telegram.Update) bool {
if update.Message.From == nil {
return false
}
fromUserName := update.Message.From.UserName
for _, admin := range admins {
if admin == fromUserName {
return true
}
}

return false
}

// addPass to passList and send a message
func addPassList(bot TrollShieldBot, update *telegram.Update) {
userName := extractPassUserName(update.Message.Text)
passList = append(passList, userName)
reply(bot, update, fmt.Sprintf("O passe para %q foi adicionado.", userName))
}

0 comments on commit 9c6e781

Please sign in to comment.