Skip to content

Commit

Permalink
Fixing multiple args for max and pattern matching
Browse files Browse the repository at this point in the history
  • Loading branch information
STollenaar committed May 5, 2023
1 parent 1b6acb7 commit 467ec9f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 37 deletions.
3 changes: 2 additions & 1 deletion lib/commands/commandHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package commands

import (
"context"
"fmt"
"sort"
"statsisticsbot/lib"
"statsisticsbot/util"
Expand Down Expand Up @@ -122,7 +123,7 @@ func CountFilterOccurences(guildID string, filter bson.D, wordFilter string) (me
primitive.E{
Key: "$regex",
Value: primitive.Regex{
Pattern: wordFilter,
Pattern: fmt.Sprintf("^%s$", wordFilter),
Options: "i",
},
},
Expand Down
4 changes: 2 additions & 2 deletions lib/commands/countCommand.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func FindSpecificWordOccurences(args *CommandParsed) int {
channelFilter = bson.D{
primitive.E{
Key: "$eq",
Value: args.ChannelTarget,
Value: args.ChannelTarget.ID,
},
}
} else {
Expand All @@ -63,7 +63,7 @@ func FindSpecificWordOccurences(args *CommandParsed) int {
primitive.E{
Key: "$regex",
Value: primitive.Regex{
Pattern: args.Word,
Pattern: fmt.Sprintf("^%s$", args.Word),
Options: "i",
},
},
Expand Down
62 changes: 28 additions & 34 deletions lib/commands/maxCommand.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,57 +59,51 @@ func FindAllWordOccurences(arguments *CommandParsed) util.CountGrouped {
}

func getFilter(arguments *CommandParsed) (result bson.D, wordFilter string) {
var expressions bson.D
if arguments.isNotEmpty() {

if user := arguments.UserTarget; user != nil {
// Filtering based on author
result = append(result,
bson.E{
Key: "$match",
Value: bson.D{
primitive.E{
Key: "Author",
Value: user.ID,
},
},
})
expressions = append(expressions,
primitive.E{
Key: "Author",
Value: user.ID,
},
)
}
if channel := arguments.ChannelTarget; channel != nil {
// Filtering based on channelID
result = append(result,
bson.E{
Key: "$match",
Value: bson.D{
primitive.E{
Key: "ChannelID",
Value: channel.ID,
},
},
})
expressions = append(expressions,
primitive.E{
Key: "ChannelID",
Value: channel.ID,
},
)
}

if word := arguments.Word; word != "" {
wordFilter = word
result = append(result,
bson.E{
Key: "$match",
expressions = append(expressions,
primitive.E{
Key: "Content",
Value: bson.D{
primitive.E{
Key: "Content",
Value: bson.D{
primitive.E{
Key: "$regex",
Value: primitive.Regex{
Pattern: word,
Options: "i",
},
},
Key: "$regex",
Value: primitive.Regex{
Pattern: fmt.Sprintf("^%s$", word),
Options: "i",
},
},
},
})
},
)
}
}
result = bson.D{
primitive.E{
Key: "$match",
Value: expressions,
},
}

return result, wordFilter
}

0 comments on commit 467ec9f

Please sign in to comment.