Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make it possible to also match sticker sets #133

Merged
merged 1 commit into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified site-configs/patterns.toml
Binary file not shown.
13 changes: 11 additions & 2 deletions src/patterns.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type opPatterns struct {
Username []opPatternAction `toml:"username"`
Bio []opPatternAction `toml:"bio"`
Message []opPatternAction `toml:"message"`
Sticker []opPatternAction `toml:"sticker"`
}

// opMatchPattern contains the data we will use when matching.
Expand All @@ -51,8 +52,9 @@ type opMatchPattern struct {
Nickname string // This is the first + last name.
Username string
Bio string
// This will be matched in regular messages.
// These will be matched in regular messages.
Message string
Sticker string
}

// This is a way to obtain specific information not available with the current
Expand Down Expand Up @@ -134,6 +136,9 @@ func getMatchPattern(bot *tgbotapi.BotAPI, update tgbotapi.Update) (opMatchPatte
// Media messages won't have a "Text" attribute, but may have a
// "Caption", so let's match against it.
matchPattern.Message = update.Message.Caption
case update.Message.Sticker != nil && len(update.Message.Sticker.SetName) > 0:
// Let's match against the name of the sticker set.
matchPattern.Sticker = update.Message.Sticker.SetName
default:
// By default, we only get the actual message to match against.
matchPattern.Message = update.Message.Text
Expand All @@ -142,7 +147,7 @@ func getMatchPattern(bot *tgbotapi.BotAPI, update tgbotapi.Update) (opMatchPatte
}

// stringTomlToPatterns() converts the toml patterns from string to the Patterns
// type, that ca be used for the matching.
// type, that can be used for the matching.
func stringTomlToPatterns(sp string) (opPatterns, error) {
newPatterns := opPatterns{}
if _, err := toml.Decode(sp, &newPatterns); err != nil {
Expand Down Expand Up @@ -205,6 +210,10 @@ func (p *opPatterns) matchPattern(m opMatchPattern) (bool, opMatchAction) {
// Common case; match against actual message.
return performGroupMatch(p.Message, m.Message)
}
if len(m.Sticker) > 0 {
// Let's check whether this sticker set is allowed.
return performGroupMatch(p.Sticker, m.Sticker)
}

// We are matching against the data from a new user who just joined.

Expand Down
25 changes: 25 additions & 0 deletions src/patterns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ action = "ban"

[[message]]
pattern = "foobar.example.com"
action = "ban"

[[sticker]]
pattern = "(foobar|nsfw)"
action = "ban"`
)

Expand Down Expand Up @@ -100,6 +104,27 @@ func TestPatternMatch(t *testing.T) {
expected: true,
expectedAction: opKick,
},
{
mp: opMatchPattern{
Sticker: "bar",
},
expected: false,
expectedAction: opNoAction,
},
{
mp: opMatchPattern{
Sticker: "foobar",
},
expected: true,
expectedAction: opBan,
},
{
mp: opMatchPattern{
Sticker: "nsfw_sticker_set",
},
expected: true,
expectedAction: opBan,
},
}

p, _ := stringTomlToPatterns(patterns)
Expand Down
Loading