Skip to content

Commit

Permalink
feat: autoban when using baneed words (#5)
Browse files Browse the repository at this point in the history
* build: fix local import inside format command

* build: update golangci config

* build(deps): bump dependencies version

* feat(wip): add autoban support

* feat: implement auto ban feature
  • Loading branch information
RiccardoM committed Aug 13, 2023
1 parent 79d3d02 commit 734b0d9
Show file tree
Hide file tree
Showing 8 changed files with 363 additions and 317 deletions.
31 changes: 17 additions & 14 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -1,36 +1,30 @@
run:
tests: false
# # timeout for analysis, e.g. 30s, 5m, default is 1m
# timeout: 5m

linters:
disable-all: true
enable:
- bodyclose
- deadcode
- depguard
- dogsled
- exportloopref
- goconst
- gocritic
- gofmt
- goimports
- golint
- gosec
- gosimple
- govet
- ineffassign
- maligned
- misspell
- nakedret
- scopelint
- nolintlint
- staticcheck
- structcheck
- stylecheck
- typecheck
- unconvert
- unused
- unparam
- misspell
- nolintlint

issues:
exclude-rules:
Expand All @@ -46,20 +40,29 @@ issues:
- text: "ST1003:"
linters:
- stylecheck

# FIXME: Disabled until golangci-lint updates stylecheck with this fix:
# https://github.com/dominikh/go-tools/issues/389
- text: "ST1016:"
# - text: "ST1016:"
# linters:
# - stylecheck

# TODO: Remove these when we are ready to drop support for Legacy Amino
- text: 'SA1019: "github.com/cosmos/cosmos-sdk/types/bech32/legacybech32" is deprecated'
linters:
- stylecheck
- staticcheck
- text: "SA1019: legacybech32.UnmarshalPubKey is deprecated"
linters:
- staticcheck

max-issues-per-linter: 10000
max-same-issues: 10000

linters-settings:
goimports:
local-prefixes: github.com/desmos-labs/hephaestus
dogsled:
max-blank-identifiers: 3
maligned:
# print struct with more effective memory layout or not, false by default
suggest-new: true
nolintlint:
allow-unused: false
allow-leading-space: true
Expand Down
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ lint-fix:
.PHONY: lint lint-fix

format:
find . -name '*.go' -type f -not -path "./vendor*" -not -path "*.git*" -not -name '*.pb.go' -not -path "./venv" | xargs gofmt -w -s
find . -name '*.go' -type f -not -path "./vendor*" -not -path "*.git*" -not -name '*.pb.go' -not -path "./venv" | xargs misspell -w
find . -name '*.go' -type f -not -path "./vendor*" -not -path "*.git*" -not -name '*.pb.go' -not -path "./venv" | xargs goimports -w -local github.com/desmos-labs/djuno
find . -name '*.go' -type f -not -path "*.git*" -not -name '*.pb.go' | xargs gofmt -w -s
find . -name '*.go' -type f -not -path "*.git*" -not -name '*.pb.go' | xargs misspell -w
find . -name '*.go' -type f -not -path "*.git*" -not -name '*.pb.go' | xargs goimports -w -local github.com/desmos-labs/hephaestus
.PHONY: format

.PHONY: lint lint-fix format
22 changes: 14 additions & 8 deletions bot/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,24 @@ func (bot *Bot) Start() {

log.Info().Timestamp().Msg("starting bot")

// Create a middleware that only accepts messages with a "ping" prefix
// tip: use this to identify bot commands
// Create a handler that handles all command for generic utilities such as auto ban
msgHandler := bot.discord.Gateway()
msgHandler.MessageCreate(
bot.AutoBanMessage,
)

// Create a middleware that only accepts messages with a given prefix
filter, _ := std.NewMsgFilter(context.Background(), bot.discord)
filter.SetPrefix(bot.cfg.Prefix)

handler := bot.discord.Gateway().
// Create a handler that handles specific bot commands
cmdHandler := bot.discord.Gateway().
WithMiddleware(
filter.NotByBot, // Ignore hephaestus messages
filter.HasPrefix, // Message must have the given prefix
filter.StripPrefix, // Remove the command prefix from the message
)
handler.MessageCreate(
cmdHandler.MessageCreate(
bot.NewCmdHandler(types.CmdHelp, bot.HandleHelp),
bot.NewCmdHandler(types.CmdDocs, bot.HandleDocs),
bot.NewCmdHandler(types.CmdSend, bot.HandleSendTokens),
Expand Down Expand Up @@ -113,16 +119,16 @@ func (bot *Bot) Reply(msg *disgord.Message, s disgord.Session, message string) {
}

// React allows to react with the provided emoji to the given message
func (bot *Bot) React(msg *disgord.Message, s disgord.Session, emoji interface{}, flags ...disgord.Flag) {
err := msg.React(context.Background(), s, emoji, flags...)
func (bot *Bot) React(msg *disgord.Message, s disgord.Session, emoji interface{}) {
err := msg.React(context.Background(), s, emoji)
if err != nil {
log.Error().Err(err).Msg("failed to react to message")
}
}

// Unreact allows to unreact with the provided emoji to the given message
func (bot *Bot) Unreact(msg *disgord.Message, s disgord.Session, emoji interface{}, flags ...disgord.Flag) {
err := msg.Unreact(context.Background(), s, emoji, flags...)
func (bot *Bot) Unreact(msg *disgord.Message, s disgord.Session, emoji interface{}) {
err := msg.Unreact(context.Background(), s, emoji)
if err != nil {
log.Error().Err(err).Msg("failed to unreact to message")
}
Expand Down
36 changes: 36 additions & 0 deletions bot/msg_autoban.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package bot

import (
"strings"

"github.com/andersfylling/disgord"
"github.com/rs/zerolog/log"

"github.com/desmos-labs/hephaestus/types"
)

// AutoBanMessage bans the user if the message contains banned words
func (b *Bot) AutoBanMessage(s disgord.Session, data *disgord.MessageCreate) {
if !b.containsBannedWords(data.Message.Content) {
return
}

// Ban the user
err := s.Guild(data.Message.GuildID).Member(data.Message.Author.ID).Ban(&disgord.BanMember{
DeleteMessageDays: 7,
Reason: "Scam",
})
if err != nil {
log.Error().Err(err).Str(types.LogUser, data.Message.Author.Username).Msg("error while banning user")
return
}
}

func (b *Bot) containsBannedWords(msg string) bool {
for _, bannedWord := range b.cfg.BannedWords {
if strings.Contains(strings.ToLower(msg), strings.ToLower(bannedWord)) {
return true
}
}
return false
}
Loading

0 comments on commit 734b0d9

Please sign in to comment.