Skip to content

Commit

Permalink
cleanup member chunking filter
Browse files Browse the repository at this point in the history
  • Loading branch information
topi314 committed May 16, 2022
1 parent 61e0242 commit aa18999
Showing 1 changed file with 22 additions and 21 deletions.
43 changes: 22 additions & 21 deletions bot/member_chunking_filter.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package bot

import "github.com/disgoorg/snowflake/v2"
import (
"github.com/disgoorg/snowflake/v2"
"golang.org/x/exp/slices"
)

// MemberChunkingFilter is a filter that can be used to filter from which guilds to request members from.
type MemberChunkingFilter func(guildID snowflake.ID) bool
Expand All @@ -10,30 +13,28 @@ var (
MemberChunkingFilterNone MemberChunkingFilter = func(_ snowflake.ID) bool { return false }
)

// Include includes the given guilds from being chunked.
func (f MemberChunkingFilter) Include(guildIDs ...snowflake.ID) MemberChunkingFilter {
func MemberChunkingFilterIncludeGuildIDs(guildIDs ...snowflake.ID) MemberChunkingFilter {
return func(guildID snowflake.ID) bool {
return slices.Contains(guildIDs, guildID)
}
}

func MemberChunkingFilterExcludeGuildIDs(guildIDs ...snowflake.ID) MemberChunkingFilter {
return func(guildID snowflake.ID) bool {
return slices.Contains(guildIDs, guildID)
}
}

// Or allows you to combine the MemberChunkingFilter with another, meaning either of them needs to be true for the guild to be chunked.
func (f MemberChunkingFilter) Or(filter MemberChunkingFilter) MemberChunkingFilter {
return func(guildID snowflake.ID) bool {
return f(guildID) || func(guildID snowflake.ID) bool {
for _, gID := range guildIDs {
if gID == guildID {
return true
}
}
return false
}(guildID)
return f(guildID) || filter(guildID)
}
}

// Exclude excludes the given guilds from being chunked.
func (f MemberChunkingFilter) Exclude(guildIDs ...snowflake.ID) MemberChunkingFilter {
// And allows you to require both MemberChunkingFilter(s) to be true for the guild to be chunked
func (f MemberChunkingFilter) And(filter MemberChunkingFilter) MemberChunkingFilter {
return func(guildID snowflake.ID) bool {
return f(guildID) || func(guildID snowflake.ID) bool {
for _, gID := range guildIDs {
if gID == guildID {
return false
}
}
return true
}(guildID)
return f(guildID) && filter(guildID)
}
}

0 comments on commit aa18999

Please sign in to comment.