Skip to content

Commit

Permalink
feat: Auto Moderation
Browse files Browse the repository at this point in the history
fix: Concurrency Issues in AutoModeration
  • Loading branch information
sabihoshi committed Jul 23, 2022
1 parent fce393e commit d3d9ff6
Show file tree
Hide file tree
Showing 101 changed files with 3,888 additions and 693 deletions.
30 changes: 3 additions & 27 deletions HuTao.Bot/Behaviors/GuildConfigureBehavior.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
using System.Threading;
using System.Threading.Tasks;
using Discord;
using HuTao.Data;
using HuTao.Services.Core;
using HuTao.Services.Core.Messages;
using HuTao.Services.Utilities;
using MediatR;

namespace HuTao.Bot.Behaviors;
Expand All @@ -13,38 +11,16 @@ public class GuildConfigureBehavior :
INotificationHandler<GuildAvailableNotification>,
INotificationHandler<JoinedGuildNotification>
{
private static readonly SemaphoreSlim Semaphore = new(1, 1);
private readonly AuthorizationService _auth;
private readonly HuTaoContext _db;

public GuildConfigureBehavior(AuthorizationService auth, HuTaoContext db)
{
_auth = auth;
_db = db;
}
public GuildConfigureBehavior(AuthorizationService auth) { _auth = auth; }

public Task Handle(GuildAvailableNotification notification, CancellationToken cancellationToken)
=> ConfigureGuildAsync(notification.Guild, cancellationToken);

public Task Handle(JoinedGuildNotification notification, CancellationToken cancellationToken)
=> ConfigureGuildAsync(notification.Guild, cancellationToken);

private async Task ConfigureGuildAsync(IGuild? guild, CancellationToken cancellationToken)
{
if (guild is null) return;

try
{
await Semaphore.WaitAsync(cancellationToken);

await _db.Guilds.TrackGuildAsync(guild, cancellationToken);
await _db.SaveChangesAsync(cancellationToken);

await _auth.AutoConfigureGuild(guild, cancellationToken);
}
finally
{
Semaphore.Release();
}
}
private Task ConfigureGuildAsync(IGuild guild, CancellationToken cancellationToken)
=> _auth.AutoConfigureGuild(guild, cancellationToken);
}
8 changes: 4 additions & 4 deletions HuTao.Bot/Bot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ public class Bot
public static async Task Main() => await new Bot().StartAsync();

private static ServiceProvider ConfigureServices() =>
new ServiceCollection().AddHttpClient().AddMemoryCache().AddHangfireServer()
.AddDbContext<HuTaoContext>(ContextOptions)
.AddMediatR(c => c.Using<HuTaoMediator>(),
typeof(Bot), typeof(DiscordSocketListener))
new ServiceCollection()
.AddLogging(l => l.AddSerilog())
.AddHttpClient().AddMemoryCache().AddHangfireServer()
.AddDbContext<HuTaoContext>(ContextOptions)
.AddMediatR(c => c.Using<HuTaoMediator>(), typeof(Bot), typeof(DiscordSocketListener))
.AddSingleton(new DiscordSocketConfig
{
AlwaysDownloadUsers = HuTaoConfig.Configuration.AlwaysDownloadUsers,
Expand Down
230 changes: 230 additions & 0 deletions HuTao.Bot/Modules/AutoModeration/AutoConfigurationOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
using System;
using Discord.Commands;
using HuTao.Data.Models.Moderation;
using HuTao.Data.Models.Moderation.Auto.Configurations;
using HuTao.Data.Models.Moderation.Infractions.Triggers;
using HuTao.Services.CommandHelp;
using static HuTao.Data.Models.Moderation.Auto.Configurations.DuplicateConfiguration;
using static HuTao.Data.Models.Moderation.Auto.Configurations.IAutoConfigurationOptions;

namespace HuTao.Bot.Modules.AutoModeration;

[NamedArgumentType]
public class AutoConfigurationOptions : IAutoConfigurationOptions
{
[HelpSummary(DeleteMessagesSummary)]
public bool DeleteMessages { get; set; } = true;

[HelpSummary(GlobalFilterSummary)]
public bool GlobalFilter { get; set; } = false;

[HelpSummary(MentionCountDuplicatesSummary)]
public bool MentionCountDuplicates { get; set; }

[HelpSummary(MentionCountInvalidSummary)]
public bool MentionCountInvalid { get; set; }

[HelpSummary(MentionCountRoleMembersSummary)]
public bool MentionCountRoleMembers { get; set; } = false;

[HelpSummary(NewLineBlankOnlySummary)]
public bool NewLineBlankOnly { get; set; }

[HelpSummary(DuplicatePercentageSummary)]
public double DuplicatePercentage { get; set; }

[HelpSummary(DuplicateTypeSummary)]
public DuplicateType DuplicateType { get; set; } = DuplicateType.Message;

[HelpSummary(DuplicateToleranceSummary)]
public int DuplicateTolerance { get; set; }

[HelpSummary(MinimumLengthSummary)]
public int MinimumLength { get; set; }

[HelpSummary(ReasonSummary)]
public string? Reason { get; set; }

[HelpSummary(TimePeriodSummary)]
public TimeSpan TimePeriod { get; set; }

[HelpSummary(CooldownSummary)]
public TimeSpan? Cooldown { get; set; }

[HelpSummary(CategorySummary)]
public ModerationCategory? Category { get; set; }

[HelpSummary(ModeSummary)]
public TriggerMode Mode { get; set; } = TriggerMode.Retroactive;

[HelpSummary(AmountSummary)]
public uint Amount { get; set; } = 1;
}

[NamedArgumentType]
public class MuteConfigurationOptions : IAutoConfigurationOptions
{
[HelpSummary("The length of time to mute the user for.")]
public TimeSpan? MuteLength { get; set; }

[HelpSummary(DeleteMessagesSummary)]
public bool DeleteMessages { get; set; } = true;

[HelpSummary(GlobalFilterSummary)]
public bool GlobalFilter { get; set; } = false;

[HelpSummary(MentionCountDuplicatesSummary)]
public bool MentionCountDuplicates { get; set; }

[HelpSummary(MentionCountInvalidSummary)]
public bool MentionCountInvalid { get; set; }

[HelpSummary(MentionCountRoleMembersSummary)]
public bool MentionCountRoleMembers { get; set; } = false;

[HelpSummary(NewLineBlankOnlySummary)]
public bool NewLineBlankOnly { get; set; }

[HelpSummary(DuplicatePercentageSummary)]
public double DuplicatePercentage { get; set; }

[HelpSummary(DuplicateTypeSummary)]
public DuplicateType DuplicateType { get; set; } = DuplicateType.Message;

[HelpSummary(DuplicateToleranceSummary)]
public int DuplicateTolerance { get; set; }

[HelpSummary(MinimumLengthSummary)]
public int MinimumLength { get; set; }

[HelpSummary(ReasonSummary)]
public string? Reason { get; set; }

[HelpSummary(TimePeriodSummary)]
public TimeSpan TimePeriod { get; set; }

[HelpSummary(CooldownSummary)]
public TimeSpan? Cooldown { get; set; }

[HelpSummary(CategorySummary)]
public ModerationCategory? Category { get; set; }

[HelpSummary(ModeSummary)]
public TriggerMode Mode { get; set; } = TriggerMode.Retroactive;

[HelpSummary(AmountSummary)]
public uint Amount { get; set; } = 1;
}

[NamedArgumentType]
public class WarningConfigurationOptions : IAutoConfigurationOptions
{
[HelpSummary("The amount of warnings that will be given.")]
public uint WarnCount { get; set; }

[HelpSummary(DeleteMessagesSummary)]
public bool DeleteMessages { get; set; } = true;

[HelpSummary(GlobalFilterSummary)]
public bool GlobalFilter { get; set; } = false;

[HelpSummary(MentionCountDuplicatesSummary)]
public bool MentionCountDuplicates { get; set; }

[HelpSummary(MentionCountInvalidSummary)]
public bool MentionCountInvalid { get; set; }

[HelpSummary(MentionCountRoleMembersSummary)]
public bool MentionCountRoleMembers { get; set; } = false;

[HelpSummary(NewLineBlankOnlySummary)]
public bool NewLineBlankOnly { get; set; }

[HelpSummary(DuplicatePercentageSummary)]
public double DuplicatePercentage { get; set; }

[HelpSummary(DuplicateTypeSummary)]
public DuplicateType DuplicateType { get; set; } = DuplicateType.Message;

[HelpSummary(DuplicateToleranceSummary)]
public int DuplicateTolerance { get; set; }

[HelpSummary(MinimumLengthSummary)]
public int MinimumLength { get; set; }

[HelpSummary(ReasonSummary)]
public string? Reason { get; set; }

[HelpSummary(TimePeriodSummary)]
public TimeSpan TimePeriod { get; set; }

[HelpSummary(CooldownSummary)]
public TimeSpan? Cooldown { get; set; }

[HelpSummary(CategorySummary)]
public ModerationCategory? Category { get; set; }

[HelpSummary(ModeSummary)]
public TriggerMode Mode { get; set; } = TriggerMode.Retroactive;

[HelpSummary(AmountSummary)]
public uint Amount { get; set; } = 1;
}

[NamedArgumentType]
public class BanConfigurationOptions : IAutoConfigurationOptions
{
[HelpSummary("The length of time to ban the user for.")]
public TimeSpan? BanLength { get; set; }

[HelpSummary("The amount of days to delete messages when the user is banned. Defaults to `1`.")]
public uint DeleteDays { get; set; } = 1;

[HelpSummary(DeleteMessagesSummary)]
public bool DeleteMessages { get; set; } = true;

[HelpSummary(GlobalFilterSummary)]
public bool GlobalFilter { get; set; } = false;

[HelpSummary(MentionCountDuplicatesSummary)]
public bool MentionCountDuplicates { get; set; }

[HelpSummary(MentionCountInvalidSummary)]
public bool MentionCountInvalid { get; set; }

[HelpSummary(MentionCountRoleMembersSummary)]
public bool MentionCountRoleMembers { get; set; } = false;

[HelpSummary(NewLineBlankOnlySummary)]
public bool NewLineBlankOnly { get; set; }

[HelpSummary(DuplicatePercentageSummary)]
public double DuplicatePercentage { get; set; }

[HelpSummary(DuplicateTypeSummary)]
public DuplicateType DuplicateType { get; set; } = DuplicateType.Message;

[HelpSummary(DuplicateToleranceSummary)]
public int DuplicateTolerance { get; set; }

[HelpSummary(MinimumLengthSummary)]
public int MinimumLength { get; set; }

[HelpSummary(ReasonSummary)]
public string? Reason { get; set; }

[HelpSummary(TimePeriodSummary)]
public TimeSpan TimePeriod { get; set; }

[HelpSummary(CooldownSummary)]
public TimeSpan? Cooldown { get; set; }

[HelpSummary(CategorySummary)]
public ModerationCategory? Category { get; set; }

[HelpSummary(ModeSummary)]
public TriggerMode Mode { get; set; } = TriggerMode.Retroactive;

[HelpSummary(AmountSummary)]
public uint Amount { get; set; } = 1;
}
Loading

0 comments on commit d3d9ff6

Please sign in to comment.