Skip to content

Commit

Permalink
refactor: File Scoped Namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
sabihoshi committed Dec 8, 2021
1 parent 81fa0da commit 4457143
Show file tree
Hide file tree
Showing 225 changed files with 8,214 additions and 8,550 deletions.
5 changes: 2 additions & 3 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

[*.{appxmanifest,asax,ascx,aspx,axaml,build,cg,cginc,compute,cs,cshtml,dtd,fs,fsi,fsscript,fsx,hlsl,hlsli,hlslinc,master,ml,mli,nuspec,paml,razor,resw,resx,shader,skin,usf,ush,vb,xaml,xamlx,xoml,xsd}]
indent_style = space
indent_size = 4
Expand All @@ -21,7 +20,7 @@ dotnet_naming_rule.unity_serialized_field_rule.style = lower_camel_case_style
dotnet_naming_rule.unity_serialized_field_rule.symbols = unity_serialized_field_symbols
dotnet_naming_style.lower_camel_case_style.capitalization = camel_case
dotnet_naming_symbols.unity_serialized_field_symbols.applicable_accessibilities = *
dotnet_naming_symbols.unity_serialized_field_symbols.applicable_kinds =
dotnet_naming_symbols.unity_serialized_field_symbols.applicable_kinds =
dotnet_naming_symbols.unity_serialized_field_symbols.resharper_applicable_kinds = unity_serialised_field
dotnet_naming_symbols.unity_serialized_field_symbols.resharper_required_modifiers = instance
dotnet_style_parentheses_in_arithmetic_binary_operators = never_if_unnecessary:none
Expand Down Expand Up @@ -91,4 +90,4 @@ resharper_suspicious_type_conversion_global_highlighting = error
resharper_use_null_propagation_when_possible_highlighting = suggestion
resharper_web_config_module_not_resolved_highlighting = warning
resharper_web_config_type_not_resolved_highlighting = warning
resharper_web_config_wrong_module_highlighting = warning
resharper_web_config_wrong_module_highlighting = warning
79 changes: 39 additions & 40 deletions Zhongli.Bot/Behaviors/CensorBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,56 +12,55 @@
using Zhongli.Services.Moderation;
using Zhongli.Services.Utilities;

namespace Zhongli.Bot.Behaviors
namespace Zhongli.Bot.Behaviors;

public class CensorBehavior :
INotificationHandler<MessageReceivedNotification>,
INotificationHandler<MessageUpdatedNotification>
{
public class CensorBehavior :
INotificationHandler<MessageReceivedNotification>,
INotificationHandler<MessageUpdatedNotification>
{
private readonly ModerationService _moderation;
private readonly ZhongliContext _db;
private readonly ModerationService _moderation;
private readonly ZhongliContext _db;

public CensorBehavior(ModerationService moderation, ZhongliContext db)
{
_moderation = moderation;
_db = db;
}
public CensorBehavior(ModerationService moderation, ZhongliContext db)
{
_moderation = moderation;
_db = db;
}

public Task Handle(MessageReceivedNotification notification, CancellationToken cancellationToken)
=> ProcessMessage(notification.Message, cancellationToken);
public Task Handle(MessageReceivedNotification notification, CancellationToken cancellationToken)
=> ProcessMessage(notification.Message, cancellationToken);

public Task Handle(MessageUpdatedNotification notification, CancellationToken cancellationToken)
=> ProcessMessage(notification.NewMessage, cancellationToken);
public Task Handle(MessageUpdatedNotification notification, CancellationToken cancellationToken)
=> ProcessMessage(notification.NewMessage, cancellationToken);

private async Task ProcessMessage(SocketMessage message, CancellationToken cancellationToken = default)
{
var author = message.Author;
if (author.IsBot || author.IsWebhook
|| author is not IGuildUser user
|| message.Channel is not ITextChannel channel)
return;
private async Task ProcessMessage(SocketMessage message, CancellationToken cancellationToken = default)
{
var author = message.Author;
if (author.IsBot || author.IsWebhook
|| author is not IGuildUser user
|| message.Channel is not ITextChannel channel)
return;

var guild = channel.Guild;
var guildEntity = await _db.Guilds.FindByIdAsync(guild.Id, cancellationToken);
if (guildEntity is null || cancellationToken.IsCancellationRequested)
return;
var guild = channel.Guild;
var guildEntity = await _db.Guilds.FindByIdAsync(guild.Id, cancellationToken);
if (guildEntity is null || cancellationToken.IsCancellationRequested)
return;

if (guildEntity.ModerationRules.CensorExclusions.Any(e => e.Judge(channel, user)))
return;
if (guildEntity.ModerationRules.CensorExclusions.Any(e => e.Judge(channel, user)))
return;

await _db.Users.TrackUserAsync(user, cancellationToken);
var currentUser = await guild.GetCurrentUserAsync();
await _db.Users.TrackUserAsync(user, cancellationToken);
var currentUser = await guild.GetCurrentUserAsync();

foreach (var censor in guildEntity.ModerationRules.Triggers.OfType<Censor>()
.Where(c => c.IsActive)
.Where(c => c.Exclusions.All(e => !e.Judge(channel, user)))
.Where(c => c.Regex().IsMatch(message.Content)))
{
var details = new ReprimandDetails(user, currentUser, "[Censor Triggered]", censor);
var length = guildEntity.ModerationRules.CensorTimeRange;
foreach (var censor in guildEntity.ModerationRules.Triggers.OfType<Censor>()
.Where(c => c.IsActive)
.Where(c => c.Exclusions.All(e => !e.Judge(channel, user)))
.Where(c => c.Regex().IsMatch(message.Content)))
{
var details = new ReprimandDetails(user, currentUser, "[Censor Triggered]", censor);
var length = guildEntity.ModerationRules.CensorTimeRange;

await _moderation.CensorAsync(message, length, details, cancellationToken);
}
await _moderation.CensorAsync(message, length, details, cancellationToken);
}
}
}
43 changes: 21 additions & 22 deletions Zhongli.Bot/Behaviors/GuildConfigureBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,32 @@
using Zhongli.Services.Core.Messages;
using Zhongli.Services.Utilities;

namespace Zhongli.Bot.Behaviors
namespace Zhongli.Bot.Behaviors;

public class GuildConfigureBehavior :
INotificationHandler<GuildAvailableNotification>,
INotificationHandler<JoinedGuildNotification>
{
public class GuildConfigureBehavior :
INotificationHandler<GuildAvailableNotification>,
INotificationHandler<JoinedGuildNotification>
{
private readonly AuthorizationService _auth;
private readonly ZhongliContext _db;
private readonly AuthorizationService _auth;
private readonly ZhongliContext _db;

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

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

public Task Handle(JoinedGuildNotification 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)
{
await _db.Guilds.TrackGuildAsync(guild, cancellationToken);
await _db.SaveChangesAsync(cancellationToken);
private async Task ConfigureGuildAsync(IGuild guild, CancellationToken cancellationToken)
{
await _db.Guilds.TrackGuildAsync(guild, cancellationToken);
await _db.SaveChangesAsync(cancellationToken);

await _auth.AutoConfigureGuild(guild, cancellationToken);
}
await _auth.AutoConfigureGuild(guild, cancellationToken);
}
}
45 changes: 22 additions & 23 deletions Zhongli.Bot/Behaviors/MessageLoggingBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,32 @@
using Zhongli.Services.Core.Messages;
using Zhongli.Services.Logging;

namespace Zhongli.Bot.Behaviors
namespace Zhongli.Bot.Behaviors;

public class MessageLoggingBehavior :
INotificationHandler<MessageDeletedNotification>,
INotificationHandler<MessagesBulkDeletedNotification>,
INotificationHandler<MessageReceivedNotification>,
INotificationHandler<MessageUpdatedNotification>
{
public class MessageLoggingBehavior :
INotificationHandler<MessageDeletedNotification>,
INotificationHandler<MessagesBulkDeletedNotification>,
INotificationHandler<MessageReceivedNotification>,
INotificationHandler<MessageUpdatedNotification>
{
private readonly LoggingService _logging;
private readonly ZhongliContext _db;
private readonly LoggingService _logging;
private readonly ZhongliContext _db;

public MessageLoggingBehavior(LoggingService logging, ZhongliContext db)
{
_logging = logging;
_db = db;
}
public MessageLoggingBehavior(LoggingService logging, ZhongliContext db)
{
_logging = logging;
_db = db;
}

public Task Handle(MessageDeletedNotification notification, CancellationToken cancellationToken)
=> _logging.LogAsync(notification, cancellationToken);
public Task Handle(MessageDeletedNotification notification, CancellationToken cancellationToken)
=> _logging.LogAsync(notification, cancellationToken);

public Task Handle(MessageReceivedNotification notification, CancellationToken cancellationToken)
=> _logging.LogAsync(notification, cancellationToken);
public Task Handle(MessageReceivedNotification notification, CancellationToken cancellationToken)
=> _logging.LogAsync(notification, cancellationToken);

public Task Handle(MessagesBulkDeletedNotification notification, CancellationToken cancellationToken)
=> _logging.LogAsync(notification, cancellationToken);
public Task Handle(MessagesBulkDeletedNotification notification, CancellationToken cancellationToken)
=> _logging.LogAsync(notification, cancellationToken);

public Task Handle(MessageUpdatedNotification notification, CancellationToken cancellationToken)
=> _logging.LogAsync(notification, cancellationToken);
}
public Task Handle(MessageUpdatedNotification notification, CancellationToken cancellationToken)
=> _logging.LogAsync(notification, cancellationToken);
}
23 changes: 11 additions & 12 deletions Zhongli.Bot/Behaviors/ReactionLoggingBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,19 @@
using Zhongli.Services.Core.Messages;
using Zhongli.Services.Logging;

namespace Zhongli.Bot.Behaviors
namespace Zhongli.Bot.Behaviors;

public class ReactionLoggingBehavior :
INotificationHandler<ReactionAddedNotification>,
INotificationHandler<ReactionRemovedNotification>
{
public class ReactionLoggingBehavior :
INotificationHandler<ReactionAddedNotification>,
INotificationHandler<ReactionRemovedNotification>
{
private readonly LoggingService _logging;
private readonly LoggingService _logging;

public ReactionLoggingBehavior(LoggingService logging) { _logging = logging; }
public ReactionLoggingBehavior(LoggingService logging) { _logging = logging; }

public Task Handle(ReactionAddedNotification notification, CancellationToken cancellationToken)
=> _logging.LogAsync(notification, cancellationToken);
public Task Handle(ReactionAddedNotification notification, CancellationToken cancellationToken)
=> _logging.LogAsync(notification, cancellationToken);

public Task Handle(ReactionRemovedNotification notification, CancellationToken cancellationToken)
=> _logging.LogAsync(notification, cancellationToken);
}
public Task Handle(ReactionRemovedNotification notification, CancellationToken cancellationToken)
=> _logging.LogAsync(notification, cancellationToken);
}
47 changes: 23 additions & 24 deletions Zhongli.Bot/Behaviors/UserTrackingBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,35 @@
using Zhongli.Services.Core.Messages;
using Zhongli.Services.Utilities;

namespace Zhongli.Bot.Behaviors
namespace Zhongli.Bot.Behaviors;

public class UserTrackingBehavior :
INotificationHandler<GuildMemberUpdatedNotification>,
INotificationHandler<MessageReceivedNotification>,
INotificationHandler<UserJoinedNotification>
{
public class UserTrackingBehavior :
INotificationHandler<GuildMemberUpdatedNotification>,
INotificationHandler<MessageReceivedNotification>,
INotificationHandler<UserJoinedNotification>
{
private readonly ZhongliContext _db;
private readonly ZhongliContext _db;

public UserTrackingBehavior(ZhongliContext db) { _db = db; }
public UserTrackingBehavior(ZhongliContext db) { _db = db; }

public async Task Handle(GuildMemberUpdatedNotification notification, CancellationToken cancellationToken)
{
await _db.Users.TrackUserAsync(notification.NewMember, cancellationToken);
await _db.SaveChangesAsync(cancellationToken);
}

public async Task Handle(MessageReceivedNotification notification, CancellationToken cancellationToken)
{
if (notification.Message.Author is IGuildUser { Guild: { } } author)
{
await _db.Users.TrackUserAsync(author, cancellationToken);
await _db.SaveChangesAsync(cancellationToken);
}
}
public async Task Handle(GuildMemberUpdatedNotification notification, CancellationToken cancellationToken)
{
await _db.Users.TrackUserAsync(notification.NewMember, cancellationToken);
await _db.SaveChangesAsync(cancellationToken);
}

public async Task Handle(UserJoinedNotification notification, CancellationToken cancellationToken)
public async Task Handle(MessageReceivedNotification notification, CancellationToken cancellationToken)
{
if (notification.Message.Author is IGuildUser { Guild: { } } author)
{
await _db.Users.TrackUserAsync(notification.GuildUser, cancellationToken);
await _db.Users.TrackUserAsync(author, cancellationToken);
await _db.SaveChangesAsync(cancellationToken);
}
}

public async Task Handle(UserJoinedNotification notification, CancellationToken cancellationToken)
{
await _db.Users.TrackUserAsync(notification.GuildUser, cancellationToken);
await _db.SaveChangesAsync(cancellationToken);
}
}
Loading

0 comments on commit 4457143

Please sign in to comment.