Skip to content

Commit 9c6e781

Browse files
authored
Add /pass command (#6)
1 parent a5b8e84 commit 9c6e781

File tree

2 files changed

+77
-2
lines changed

2 files changed

+77
-2
lines changed

main.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ func main() {
2828
reportKills(bot, &update, kills)
2929
}
3030

31+
if strings.HasPrefix(update.Message.Text, "/pass ") && fromAdminEvent(&update) {
32+
addPassList(bot, &update)
33+
}
34+
3135
// Exit automatically from group after the bot receive a message from it
3236
for _, trollGroup := range trollGroups {
3337
if fromChatEvent(&update, strings.TrimLeft(trollGroup, "@")) {
@@ -38,6 +42,12 @@ func main() {
3842

3943
if newChatMemberEvent(&update) {
4044
for _, member := range *update.Message.NewChatMembers {
45+
if pass, ok := hasPass(member); ok {
46+
removePassList(bot, &update, pass)
47+
welcomeMessage(bot, &update, member)
48+
continue
49+
}
50+
4151
if trollHouse := findTrollHouses(botHidden, member.ID); trollHouse != "" {
4252
err := kickTroll(bot, &update, member, trollHouse)
4353
if err == nil {

troll_shield.go

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Copyright 2020 the commonlispbr authors. All rights reserved
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
4-
54
package main
65

76
import (
@@ -39,6 +38,14 @@ var trollGroups = []string{
3938
"@mlbrasil",
4039
}
4140

41+
var passList = []string{}
42+
43+
var admins = []string{
44+
"lerax",
45+
"luksamuk",
46+
"perkunos",
47+
}
48+
4249
const logfile = "troll-shield.log"
4350
const killsFile = "kills.txt"
4451

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

194201
if err != nil {
195-
return nil, fmt.Errorf("Setup %v failed with: %v", envVar, err)
202+
return nil, fmt.Errorf("setup %v failed with: %v", envVar, err)
196203
}
197204

198205
bot.Debug = true
@@ -266,3 +273,61 @@ func reportKills(bot TrollShieldBot, update *telegram.Update, kills int64) {
266273
}
267274
reply(bot, update, txt)
268275
}
276+
277+
// parse:
278+
// - /pass <@username>
279+
// - /pass <FirstName [LastName]>
280+
// and return what is available
281+
func extractPassUserName(command string) string {
282+
tokens := strings.Split(command, " ")
283+
n := len(tokens)
284+
return strings.Join(tokens[1:n], " ")
285+
}
286+
287+
// If has pass, return true and and return the matched pass
288+
func hasPass(user telegram.User) (string, bool) {
289+
userName := getUserName(user)
290+
for _, pass := range passList {
291+
if strings.HasPrefix(userName, pass) || user.FirstName == pass {
292+
return pass, true
293+
}
294+
}
295+
return "", false
296+
}
297+
298+
// remove pass list and reply the consumed pass list
299+
func removePassList(bot TrollShieldBot, update *telegram.Update, pass string) {
300+
// Remove the element at index i from a.
301+
n := len(passList)
302+
for i, p := range passList {
303+
if p == pass {
304+
// remove pass from passList
305+
passList[i] = passList[n-1] // Copy last element to index i.
306+
passList[n-1] = "" // Erase last element (write zero value).
307+
passList = passList[:n-1] // Truncate slice.
308+
}
309+
}
310+
reply(bot, update, fmt.Sprintf("O passe para %q foi consumido.", pass))
311+
}
312+
313+
// check if a message cames from a @commonlispbr admin
314+
func fromAdminEvent(update *telegram.Update) bool {
315+
if update.Message.From == nil {
316+
return false
317+
}
318+
fromUserName := update.Message.From.UserName
319+
for _, admin := range admins {
320+
if admin == fromUserName {
321+
return true
322+
}
323+
}
324+
325+
return false
326+
}
327+
328+
// addPass to passList and send a message
329+
func addPassList(bot TrollShieldBot, update *telegram.Update) {
330+
userName := extractPassUserName(update.Message.Text)
331+
passList = append(passList, userName)
332+
reply(bot, update, fmt.Sprintf("O passe para %q foi adicionado.", userName))
333+
}

0 commit comments

Comments
 (0)