Skip to content

Commit

Permalink
Improve spam name handling
Browse files Browse the repository at this point in the history
  • Loading branch information
jyap808 committed Dec 12, 2024
1 parent 1bf8387 commit 9506df6
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 16 deletions.
26 changes: 11 additions & 15 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,7 @@ func btcPrice() *string {
}

lastPrice := ticker.Last

if err != nil {
log.Println(err)
message = "Error retrieving price from remote API's"
} else {
message = fmt.Sprintf("```Gemini BTC price: %.2f```", lastPrice)
}
message = fmt.Sprintf("```Gemini BTC price: %.2f```", lastPrice)

return &message
}
Expand Down Expand Up @@ -322,7 +316,7 @@ func guildMemberAdd(s *discordgo.Session, m *discordgo.GuildMemberAdd) {
return
}

if checkSpamUsername(s, m) {
if checkSpamName(s, m) {
return
}

Expand Down Expand Up @@ -381,19 +375,19 @@ func guildMemberAdd(s *discordgo.Session, m *discordgo.GuildMemberAdd) {
}()
}

// Check if the username matches any of the given patterns
func isUsernameSpam(username string, patterns []string) bool {
// Check if the name matches any of the given patterns
func isNameSpam(name string, patterns []string) bool {
for _, pattern := range patterns {
matched, _ := regexp.MatchString(pattern, username)
matched, _ := regexp.MatchString(pattern, name)
if matched {
return true
}
}
return false
}

// Check for spam usernames and terminate member if conditions are met
func checkSpamUsername(s *discordgo.Session, m *discordgo.GuildMemberAdd) bool {
// Check for spam user or display name and terminate member if conditions are met
func checkSpamName(s *discordgo.Session, m *discordgo.GuildMemberAdd) bool {
patterns := []string{
`(?i)Admin`,
`(?i)Announcement`,
Expand All @@ -404,11 +398,13 @@ func checkSpamUsername(s *discordgo.Session, m *discordgo.GuildMemberAdd) bool {
`(?i)Manager`,
`(?i)MEE6`,
`(?i)Support`,
`\d{4}$`,
}

username := m.User.Username
if isUsernameSpam(username, patterns) && len(m.Roles) == 0 {
go terminateMember(s, m.GuildID, m.User.ID, "Username spam")
displayName := m.User.GlobalName
if (isNameSpam(username, patterns) || isNameSpam(displayName, patterns)) && len(m.Roles) == 0 {
go terminateMember(s, m.GuildID, m.User.ID, "Name spam")
return true
}

Expand Down
13 changes: 12 additions & 1 deletion main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func Test_isUsernameSpam(t *testing.T) {
patterns := []string{
`(?i)Announcement`,
`(?i)FAQ`,
`\d{4}$`,
}

type args struct {
Expand All @@ -59,10 +60,20 @@ func Test_isUsernameSpam(t *testing.T) {
args: args{"announcements23", patterns},
want: true,
},
{
name: "Spam - spammer1234",
args: args{"spammer1234", patterns},
want: true,
},
{
name: "Ham - user123",
args: args{"user123", patterns},
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := isUsernameSpam(tt.args.username, tt.args.patterns); got != tt.want {
if got := isNameSpam(tt.args.username, tt.args.patterns); got != tt.want {
t.Errorf("isUsernameSpam() = %v, want %v", got, tt.want)
}
})
Expand Down

0 comments on commit 9506df6

Please sign in to comment.