-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add /kills command with save state functionality (#4)
* Add /kills command with save state functionality CHANGELOG: - Kills state it's loaded at session start, if doesn't exists set to 0. - Kills state it's saved on the file kills.txt - Kills state it's saved after each ban, to ensure being saved. - If /kills it's called will report the current kills state. * Add unit tests * Improve test coverage on edge cases: fix some behaviors too Important fix: os.O_TRUNC should be set as flag in os.OpenFile to overwrite the kills.txt file properly. Otherwise, ff the kills.txt had initially "random-fuckups", writing 10 will turn out in: "10ndom-fuckups". This is not the intended behavior. * Separate main function in main.go and mark as !test This will make more easier to control the unit tests, since main is not possible to test.
- Loading branch information
Showing
5 changed files
with
149 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,3 +17,4 @@ | |
/troll-shield | ||
|
||
*.log | ||
/kills.txt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
//+build !test | ||
// 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 ( | ||
"strings" | ||
) | ||
|
||
func main() { | ||
setupLogging() | ||
bot, botHidden, err := setupBots() | ||
if err != nil { | ||
log.Fatal(err.Error()) | ||
} | ||
kills := loadKills(killsFile) | ||
log.Printf("Currently kill state: %v", kills) | ||
|
||
for update := range getUpdates(bot) { | ||
if messageEvent(&update) { | ||
if update.Message.Text == "/lelerax" { | ||
reply(bot, &update, "Estou vivo.") | ||
} | ||
|
||
if update.Message.Text == "/kills" { | ||
reportKills(bot, &update, kills) | ||
} | ||
|
||
// Exit automatically from group after the bot receive a message from it | ||
for _, trollGroup := range trollGroups { | ||
if fromChatEvent(&update, strings.TrimLeft(trollGroup, "@")) { | ||
leaveChat(bot, &update, trollGroup) | ||
} | ||
} | ||
} | ||
|
||
if newChatMemberEvent(&update) { | ||
for _, member := range *update.Message.NewChatMembers { | ||
if trollHouse := findTrollHouses(botHidden, member.ID); trollHouse != "" { | ||
err := kickTroll(bot, &update, member, trollHouse) | ||
if err == nil { | ||
kills++ | ||
if err := saveKills(killsFile, kills); err != nil { | ||
log.Printf("saving kills failed: %v", err) | ||
} | ||
} | ||
} else if fromChatEvent(&update, "commonlispbr") && !member.IsBot { | ||
welcomeMessage(bot, &update, member) | ||
} | ||
|
||
// Exit automatically from groups when I'm joining it | ||
for _, trollGroup := range trollGroups { | ||
if fromChatEvent(&update, strings.TrimLeft(trollGroup, "@")) && member.UserName == bot.Self.UserName { | ||
leaveChat(bot, &update, trollGroup) | ||
} | ||
} | ||
|
||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters