Skip to content

Commit

Permalink
feat: Add user default category
Browse files Browse the repository at this point in the history
  • Loading branch information
sabihoshi committed Dec 7, 2022
1 parent bb60a49 commit 300a7d8
Show file tree
Hide file tree
Showing 20 changed files with 3,281 additions and 88 deletions.
19 changes: 9 additions & 10 deletions HuTao.Bot/Modules/InteractiveUserModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,12 @@ public async Task SlashAvatarAsync(
=> await _user.ReplyAvatarAsync(Context, user, ephemeral);

[SlashCommand("history", "View a history of a user's infractions")]
[RequireAuthorization(History, Group = nameof(History))]
[RequireCategoryAuthorization(History, Group = nameof(History))]
public async Task SlashHistoryAsync(
[Summary(description: "The user to show history of")] IUser user,
[Summary(description: "Leave empty to show warnings and notices")]
LogReprimandType type = LogReprimandType.None,
[Autocomplete(typeof(CategoryAutocomplete))] ModerationCategory? category = null,
[Autocomplete(typeof(CategoryAutocomplete))] [CheckCategory(History)]
ModerationCategory? category = null,
[Summary(description: "False to let other users see the message")]
bool ephemeral = false)
=> await _user.ReplyHistoryAsync(Context, category, type, user, false, ephemeral);
Expand Down Expand Up @@ -78,15 +77,15 @@ public async Task SlashWhoIsAsync(
public Task ComponentInformationAsync(IUser user) => SlashInformationAsync(user);

[ComponentInteraction("reprimand:*:*")]
[RequireAuthorization(History, Group = nameof(History))]
[RequireCategoryAuthorization(History, Group = nameof(History))]
public Task ComponentReprimandsAsync(string id, ModerationCategory? category, LogReprimandType[] types)
=> ComponentReprimandsAsync(id, InfractionTypeBitwise.Or(types), new[] { category });
public Task ComponentReprimandsAsync(string id,
[CheckCategory(History)] ModerationCategory? category, LogReprimandType[] types)
=> ComponentReprimandsAsync(
id, InfractionTypeBitwise.Or(types),
new[] { category ?? ModerationCategory.None });

[ComponentInteraction("category:*:*")]
[RequireAuthorization(History, Group = nameof(History))]
[RequireCategoryAuthorization(History, Group = nameof(History))]
public async Task ComponentReprimandsAsync(string id, LogReprimandType type, ModerationCategory?[] categories)
public async Task ComponentReprimandsAsync(string id, LogReprimandType type,
[CheckCategory(History)] ModerationCategory[] categories)
{
var category = categories.FirstOrDefault();
var user = await Context.Client.Rest.GetUserAsync(ulong.Parse(id));
Expand Down
44 changes: 35 additions & 9 deletions HuTao.Bot/Modules/Moderation/InteractiveReprimandsModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,26 @@ await ModifyReprimandAsync(reprimand, ephemeral, (r, d, t)
=> _moderation.TryExpireReprimandAsync(r, Pardoned, d, t), reason);
}

[SlashCommand("default-category", "Sets the default category for reprimands.")]
public async Task SetDefaultCategoryAsync(
[Summary(description: "The category to set as the default.")]
[Autocomplete(typeof(CategoryAutocomplete))]
[CheckCategory(History)]
ModerationCategory? category = null)
{
if (category == ModerationCategory.All)
{
await RespondAsync("You cannot set the default category to `All`.");
return;
}

var user = await _db.Users.TrackUserAsync(Context.User, Context.Guild);
user.DefaultCategory = category == ModerationCategory.None ? null : category;
await _db.SaveChangesAsync();

await ReplyAsync($"Default reprimand category set to `{user.DefaultCategory?.Name ?? "None"}`.");
}

[SlashCommand("update", "Update a reprimand's reason.")]
public async Task UpdateReprimandAsync(
[Autocomplete(typeof(ReprimandAutocomplete))] string id,
Expand All @@ -61,28 +81,34 @@ public async Task UpdateReprimandAsync(
}

[SlashCommand("history", "Views the entire reprimand history of the server.")]
[RequireAuthorization(History, Group = nameof(History))]
[RequireCategoryAuthorization(History, Group = nameof(History))]
public async Task ViewHistoryAsync(
[Autocomplete(typeof(CategoryAutocomplete))] ModerationCategory? category = null,
[Autocomplete(typeof(CategoryAutocomplete))] [CheckCategory(History)]
ModerationCategory? category = null,
LogReprimandType type = LogReprimandType.All,
[RequireEphemeralScope] bool ephemeral = false)
{
await DeferAsync(ephemeral);
var collection = await GetCollectionAsync();
await PagedViewAsync(collection.OfType(type).OfCategory(category).OrderByDescending(h => h.Action?.Date));
await PagedViewAsync(collection.OfType(type)
.OfCategory(category ?? ModerationCategory.None)
.OrderByDescending(h => h.Action?.Date));
}

[SlashCommand("view", "View the details of the reprimand.")]
[RequireAuthorization(History, Group = nameof(History))]
[RequireCategoryAuthorization(History, Group = nameof(History))]
public async Task ViewReprimandAsync(
[Autocomplete(typeof(ReprimandAutocomplete))] string id,
bool ephemeral = false)
{
var reprimand = await TryFindEntityAsync(id);
if (reprimand == null)
{
await RespondAsync(EmptyMatchMessage, ephemeral: true);
return;
}

var authorized = await _auth.IsCategoryAuthorizedAsync(Context, History, reprimand.Category);
if (!authorized)
await RespondAsync(NotAuthorizedMessage, ephemeral: true);
else
{
await RespondAsync(ephemeral: ephemeral,
Expand All @@ -92,7 +118,7 @@ await RespondAsync(ephemeral: ephemeral,
}

[ComponentInteraction("reprimand-delete:*:*", true)]
[SlashCommand("delete", "Delete a reprimand. this completely removes the data.")]
[SlashCommand("delete", "Delete a reprimand. This completely removes the data.")]
protected override Task RemoveEntityAsync(
[Autocomplete(typeof(ReprimandAutocomplete))] string id,
[RequireEphemeralScope] bool ephemeral = false)
Expand Down Expand Up @@ -120,7 +146,7 @@ public Task UpdateReprimandMenuAsync(string id, bool ephemeral)

protected override async Task RemoveEntityAsync(Reprimand entity, bool ephemeral)
{
var authorized = await _auth.IsAuthorizedAsync(Context, Scope, entity.Category);
var authorized = await _auth.IsCategoryAuthorizedAsync(Context, Scope, entity.Category);

if (!authorized)
await FollowupAsync(EmptyMatchMessage, ephemeral: true);
Expand All @@ -138,7 +164,7 @@ private async Task ModifyReprimandAsync(
Reprimand? reprimand, bool ephemeral,
UpdateReprimandDelegate update, string? reason = null)
{
var authorized = await _auth.IsAuthorizedAsync(Context, Scope, reprimand?.Category);
var authorized = await _auth.IsCategoryAuthorizedAsync(Context, Scope, reprimand?.Category);

if (!authorized)
await FollowupAsync(NotAuthorizedMessage, ephemeral: true);
Expand Down
28 changes: 27 additions & 1 deletion HuTao.Bot/Modules/Moderation/ModerationCategoryModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
using HuTao.Data.Models.Criteria;
using HuTao.Data.Models.Moderation;
using HuTao.Services.CommandHelp;
using HuTao.Services.Core.Listeners;
using HuTao.Services.Core.Preconditions.Commands;
using HuTao.Services.Interactive;
using HuTao.Services.Moderation;
using HuTao.Services.Utilities;
Expand All @@ -19,9 +21,14 @@ namespace HuTao.Bot.Modules.Moderation;
[Group("category")]
public class ModerationCategoryModule : InteractiveEntity<ModerationCategory>
{
private readonly CommandErrorHandler _error;
private readonly HuTaoContext _db;

public ModerationCategoryModule(HuTaoContext db) { _db = db; }
public ModerationCategoryModule(CommandErrorHandler error, HuTaoContext db)
{
_error = error;
_db = db;
}

[Command("add")]
[Summary("Add a new moderation category.")]
Expand All @@ -47,6 +54,25 @@ public async Task AddModerationCategoryAsync(string name, ModerationCategoryOpti
await ReplyAsync(embed: EntityViewer(category).WithColor(Color.Green).Build());
}

[Command("default")]
[Summary("Sets the default category for reprimands.")]
public async Task SetDefaultCategoryAsync(
[Summary("The category to set as the default.")] [CheckCategory(AuthorizationScope.History)]
ModerationCategory? category = null)
{
if (category == ModerationCategory.All)
{
await _error.AssociateError(Context, "You cannot set the default category to `All`.");
return;
}

var user = await _db.Users.TrackUserAsync(Context.User, Context.Guild);
user.DefaultCategory = category == ModerationCategory.None ? null : category;
await _db.SaveChangesAsync();

await ReplyAsync($"Default reprimand category set to `{user.DefaultCategory?.Name ?? "None"}`.");
}

[Command("remove")]
[Alias("delete", "del")]
[Summary("Remove a moderation category.")]
Expand Down
21 changes: 13 additions & 8 deletions HuTao.Bot/Modules/Moderation/ModifyReprimandsModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,25 +63,30 @@ public async Task UpdateReprimandAsync(string id, [Remainder] string? reason = n
[Command("history all")]
[Alias("reprimand all", "reprimands all")]
[Summary("Views the entire reprimand history of the server.")]
[RequireAuthorization(History, Group = nameof(History))]
[RequireCategoryAuthorization(History, Group = nameof(History))]
public async Task ViewHistoryAsync(
ModerationCategory? category = null,
[CheckCategory(History)] ModerationCategory? category = null,
[Summary("Leave empty to show everything.")] LogReprimandType type = LogReprimandType.All)
{
var collection = await GetCollectionAsync();
await PagedViewAsync(collection.OfType(type).OfCategory(category).OrderByDescending(h => h.Action?.Date));
await PagedViewAsync(collection.OfType(type)
.OfCategory(category ?? ModerationCategory.None)
.OrderByDescending(h => h.Action?.Date));
}

[Command("reprimand")]
[Summary("View the details of the reprimand.")]
[RequireAuthorization(History, Group = nameof(History))]
[RequireCategoryAuthorization(History, Group = nameof(History))]
public async Task ViewReprimandAsync(string id)
{
var reprimand = await TryFindEntityAsync(id);
if (reprimand == null)
{
await _error.AssociateError(Context.Message, EmptyMatchMessage);
return;
}

var authorized = await _auth.IsCategoryAuthorizedAsync(Context, History, reprimand.Category);
if (!authorized)
await _error.AssociateError(Context, NotAuthorizedMessage);
else
{
await ReplyAsync(
Expand All @@ -101,7 +106,7 @@ await ReplyAsync(

protected override async Task RemoveEntityAsync(Reprimand entity)
{
var authorized = await _auth.IsAuthorizedAsync(Context, Scope, entity.Category);
var authorized = await _auth.IsCategoryAuthorizedAsync(Context, Scope, entity.Category);

if (!authorized)
await _error.AssociateError(Context.Message, NotAuthorizedMessage);
Expand All @@ -118,7 +123,7 @@ protected override async Task<ICollection<Reprimand>> GetCollectionAsync()
private async Task ModifyReprimandAsync(Reprimand? reprimand,
UpdateReprimandDelegate update, string? reason = null)
{
var authorized = await _auth.IsAuthorizedAsync(Context, Scope, reprimand?.Category);
var authorized = await _auth.IsCategoryAuthorizedAsync(Context, Scope, reprimand?.Category);

if (!authorized)
await _error.AssociateError(Context.Message, NotAuthorizedMessage);
Expand Down
13 changes: 10 additions & 3 deletions HuTao.Bot/Modules/UserModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,21 @@ public Task AvatarAsync([Summary("The mention, username or ID of the user.")] IU
[Command("history")]
[Alias("infraction", "infractions", "reprimand", "reprimands", "warnlist")]
[Summary("View a specific history of a user's infractions.")]
[RequireAuthorization(History, Group = nameof(History))]
[RequireCategoryAuthorization(History, Group = nameof(History))]
public Task InfractionsAsync(
[Summary("The user to show the infractions of.")] IUser? user = null,
[Summary("Leave empty to show warnings.")] LogReprimandType type = LogReprimandType.None,
ModerationCategory? category = null)
[CheckCategory(History)] ModerationCategory? category = null)
=> _user.ReplyHistoryAsync(Context, category, type, user ?? Context.User, false);

[Command("history")]
[Alias("infraction", "infractions", "reprimand", "reprimands", "warnlist")]
[Summary("View a specific history of a user's infractions.")]
[RequireAuthorization(History)]
public Task InfractionsAsync(
[Summary("The user to show the infractions of.")] IUser? user = null,
[CheckCategory(History)] ModerationCategory? category = null)
=> InfractionsAsync(user, LogReprimandType.None, category);

[Command("user")]
[Alias("whois")]
[Summary("Views the information of a user. Leave blank to view self.")]
Expand Down
Loading

0 comments on commit 300a7d8

Please sign in to comment.