Skip to content

Commit

Permalink
Refactor and add checkSpamUsername and tests
Browse files Browse the repository at this point in the history
Check for Announcement username spam
  • Loading branch information
jyap808 committed Jul 14, 2024
1 parent 808fe21 commit c8ae4fc
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 13 deletions.
48 changes: 35 additions & 13 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,19 +322,7 @@ func guildMemberAdd(s *discordgo.Session, m *discordgo.GuildMemberAdd) {
return
}

faqUsernameMatched, _ := regexp.MatchString(`(?i)FAQ`, m.User.Username)
helpdeskUsernameMatched, _ := regexp.MatchString(`(?i)Helpdesk`, m.User.Username)
supportUsernameMatched, _ := regexp.MatchString(`(?i)Support`, m.User.Username)
adminUsernameMatched, _ := regexp.MatchString(`(?i)Admin`, m.User.Username)
captchaUsernameMatched, _ := regexp.MatchString(`(?i)[CС][aа][pр]t[cс]h[aа]`, m.User.Username)
giveawayUsernameMatched, _ := regexp.MatchString(`(?i)Giveaway`, m.User.Username)
mee6UsernameMatched, _ := regexp.MatchString(`(?i)MEE6`, m.User.Username)
managerUsernameMatched, _ := regexp.MatchString(`(?i)Manager`, m.User.Username)
if (faqUsernameMatched || helpdeskUsernameMatched || supportUsernameMatched ||
adminUsernameMatched || captchaUsernameMatched || giveawayUsernameMatched ||
mee6UsernameMatched || managerUsernameMatched) &&
len(m.Member.Roles) == 0 {
go terminateMember(s, m.GuildID, m.User.ID, "Username spam")
if checkSpamUsername(s, m) {
return
}

Expand Down Expand Up @@ -392,3 +380,37 @@ func guildMemberAdd(s *discordgo.Session, m *discordgo.GuildMemberAdd) {
turdTimer(s, m)
}()
}

// Check if the username matches any of the given patterns
func isUsernameSpam(username string, patterns []string) bool {
for _, pattern := range patterns {
matched, _ := regexp.MatchString(pattern, username)
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 {
patterns := []string{
`(?i)Admin`,
`(?i)Announcement`,
`(?i)[CС][aа][pр]t[cс]h[aа]`,
`(?i)FAQ`,
`(?i)Giveaway`,
`(?i)Helpdesk`,
`(?i)Manager`,
`(?i)MEE6`,
`(?i)Support`,
}

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

return false
}
35 changes: 35 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,38 @@ func Test_convertIDtoCreationTime(t *testing.T) {
})
}
}

func Test_isUsernameSpam(t *testing.T) {
patterns := []string{
`(?i)Announcement`,
`(?i)FAQ`,
}

type args struct {
username string
patterns []string
}
tests := []struct {
name string
args args
want bool
}{
{
name: "Ham - janedoe",
args: args{"janedoe", patterns},
want: false,
},
{
name: "Spam - announcements23",
args: args{"announcements23", patterns},
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := isUsernameSpam(tt.args.username, tt.args.patterns); got != tt.want {
t.Errorf("isUsernameSpam() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit c8ae4fc

Please sign in to comment.