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

feat: Add advanced filter to Role Module #43

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Changes from 2 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
119 changes: 115 additions & 4 deletions Zhongli.Bot/Modules/RoleModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,12 @@ public async Task AddRolesAsync(IReadOnlyCollection<IRole> roles, RoleFilters op
}
catch (HttpException e) when (e.HttpCode == HttpStatusCode.Forbidden)
{
// Ignored
fails += 1;
}
}

var embed = new EmbedBuilder()
.WithDescription($"Added {Format.Bold(roles.Humanize())} to {filtered.Count() - fails} user(s).")
.WithDescription($"Added {Format.Bold(roles.Humanize())} to {filtered.Count() - fails} user(s). ({fails} failed)")
.WithColor(Color.Green);

await ReplyAsync(embed: embed.Build());
Expand Down Expand Up @@ -255,13 +255,13 @@ public async Task RemoveRolesAsync(IReadOnlyCollection<IRole> roles, RoleFilters
}
catch (HttpException e) when (e.HttpCode == HttpStatusCode.Forbidden)
{
// Ignored
fails += 1;
}
}
}

var embed = new EmbedBuilder()
.WithDescription($"Removed {Format.Bold(roles.Humanize())} from {filtered.Count() - fails} user(s).")
.WithDescription($"Removed {Format.Bold(roles.Humanize())} from {filtered.Count() - fails} user(s). ({fails} failed)")
.WithColor(Color.DarkRed);

await ReplyAsync(embed: embed.Build());
Expand Down Expand Up @@ -413,11 +413,17 @@ public enum FilterType
[HelpSummary("Include users who have a nickname.")]
public bool? IsNicknameSet { get; set; }

[HelpSummary("Include users who boost the server.")]
public bool? IsBooster { get; set; }

[HelpSummary("Defaults to 'Any'.")] public FilterType FilterMode { get; set; }

[HelpSummary("Include users that contain these roles.")]
public IEnumerable<IRole>? Roles { get; set; }

[HelpSummary("Include users that contain these permissions.")]
public IEnumerable<GuildPermission>? Permissions { get; set; }

[HelpSummary("Include users whose username contains this string.")]
public string? UserContains { get; set; }

Expand Down Expand Up @@ -454,6 +460,51 @@ public enum FilterType
[HelpSummary("Include users whose nickname matches this regex pattern, and if they don't have one, their username. Ignores case.")]
public string? NameRegexPattern { get; set; }

[HelpSummary("Include users who joined the server after the specified datetime.")]
public string? JoinedAfter { get; set; }

[HelpSummary("Include users who joined the server before the specified datetime.")]
public string? JoinedBefore { get; set; }

[HelpSummary("Include users who joined the server at the specified datetime.")]
public string? JoinedAt { get; set; }

[HelpSummary("Include users who joined the server at or after the specified datetime.")]
public string? JoinedAtAfter { get; set; }

[HelpSummary("Include users who joined the server at or before the specified datetime.")]
public string? JoinedAtBefore { get; set; }

[HelpSummary("Include users whose Discord account was created after the specified datetime.")]
public string? CreatedAfter { get; set; }

[HelpSummary("Include users whose Discord account was created before the specified datetime.")]
public string? CreatedBefore { get; set; }

[HelpSummary("Include users whose Discord account was created at the specified datetime.")]
public string? CreatedAt { get; set; }

[HelpSummary("Include users whose Discord account was created at or after the specified datetime.")]
public string? CreatedAtAfter { get; set; }

[HelpSummary("Include users whose Discord account was created at or before the specified datetime.")]
public string? CreatedAtBefore { get; set; }

[HelpSummary("Include users who boosted the server after the specified datetime.")]
public string? BoosterAfter { get; set; }

[HelpSummary("Include users who boosted the server before the specified datetime.")]
public string? BoosterBefore { get; set; }

[HelpSummary("Include users who boosted the server at the specified datetime.")]
public string? BoosterAt { get; set; }

[HelpSummary("Include users who boosted the server at or after the specified datetime.")]
public string? BoosterAtAfter { get; set; }

[HelpSummary("Include users who boosted the server at or before the specified datetime.")]
public string? BoosterAtBefore { get; set; }
andygunawan96 marked this conversation as resolved.
Show resolved Hide resolved

public IEnumerable<Func<IGuildUser, bool>> GetRules()
{
if (IsBot is not null)
Expand All @@ -474,12 +525,27 @@ public IEnumerable<Func<IGuildUser, bool>> GetRules()
};
}

if (IsBooster is not null)
{
yield return u =>
{
var isBooster = u.PremiumSince is not null;
return IsBooster.Value == isBooster;
};
}

if (Roles is not null)
{
yield return u =>
Roles.Select(r => r.Id).Intersect(u.RoleIds).Any();
}

if (Permissions is not null)
{
yield return u =>
Permissions.Select(p => p).Intersect(u.GuildPermissions.ToList()).Any();
}

if (NameContains is not null)
yield return u =>
{
Expand Down Expand Up @@ -564,6 +630,51 @@ public IEnumerable<Func<IGuildUser, bool>> GetRules()
RegexOptions.Compiled | RegexOptions.IgnoreCase, TimeSpan.FromSeconds(1));
};
}

if (JoinedAfter is not null)
yield return u => u.JoinedAt > DateTimeOffset.Parse(JoinedAfter);

if (JoinedBefore is not null)
yield return u => u.JoinedAt < DateTimeOffset.Parse(JoinedBefore);

if (JoinedAt is not null)
yield return u => u.JoinedAt == DateTimeOffset.Parse(JoinedAt);

if (JoinedAtAfter is not null)
yield return u => u.JoinedAt >= DateTimeOffset.Parse(JoinedAtAfter);

if (JoinedAtBefore is not null)
yield return u => u.JoinedAt <= DateTimeOffset.Parse(JoinedAtBefore);

if (CreatedAfter is not null)
yield return u => u.CreatedAt > DateTimeOffset.Parse(CreatedAfter);

if (CreatedBefore is not null)
yield return u => u.CreatedAt < DateTimeOffset.Parse(CreatedBefore);

if (CreatedAt is not null)
yield return u => u.CreatedAt == DateTimeOffset.Parse(CreatedAt);

if (CreatedAtAfter is not null)
yield return u => u.CreatedAt >= DateTimeOffset.Parse(CreatedAtAfter);

if (CreatedAtBefore is not null)
yield return u => u.CreatedAt <= DateTimeOffset.Parse(CreatedAtBefore);

if (BoosterAfter is not null)
yield return u => u.PremiumSince > DateTimeOffset.Parse(BoosterAfter);

if (BoosterBefore is not null)
yield return u => u.PremiumSince < DateTimeOffset.Parse(BoosterBefore);

if (BoosterAt is not null)
yield return u => u.PremiumSince == DateTimeOffset.Parse(BoosterAt);

if (BoosterAtAfter is not null)
yield return u => u.PremiumSince >= DateTimeOffset.Parse(BoosterAtAfter);

if (BoosterAtBefore is not null)
yield return u => u.PremiumSince <= DateTimeOffset.Parse(BoosterAtBefore);
}
}
}
Expand Down