Skip to content

Commit

Permalink
Merge branch 'v5.6.0' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverbooth committed Jul 22, 2023
2 parents 0f5383d + f707123 commit 025e005
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 22 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [5.6.0] - 2023-07-22

### Added
- Infractions now store the rule text to avoid breaking changes to server rule list.

## [5.5.2] - 2023-06-09

### Added
Expand Down
6 changes: 1 addition & 5 deletions Hammer/Commands/Infractions/InfractionCommand.View.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,13 @@ public async Task ViewAsync(InteractionContext context,
}
else
{
Rule? rule = null;
if (infraction.RuleId is { } ruleId)
rule = _ruleService.GetRuleById(context.Guild, ruleId);

embed.WithColor(DiscordColor.Orange);
embed.WithTitle($"Infraction {infraction.Id}");
embed.AddField("User", MentionUtility.MentionUser(infraction.UserId), true);
embed.AddField("Type", infraction.Type.Humanize(), true);
embed.AddField("Staff Member", MentionUtility.MentionUser(infraction.StaffMemberId), true);
embed.AddField("Issued", Formatter.Timestamp(infraction.IssuedAt), true);
embed.AddFieldIf(rule is not null, "Rule Broken", () => $"{rule!.Id} - {rule.Brief ?? rule.Description}", true);
embed.AddFieldIf(infraction.RuleId.HasValue, "Rule Broken", () => $"{infraction.RuleId} - {infraction.RuleText}", true);
embed.AddFieldIf(infraction.Reason is not null, "Reason", () => infraction.Reason);
embed.AddFieldIf(!string.IsNullOrWhiteSpace(infraction.AdditionalInformation), "Additional Information", () => infraction.AdditionalInformation);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@ public void Configure(EntityTypeBuilder<Infraction> builder)
builder.Property(e => e.Reason);
builder.Property(e => e.AdditionalInformation);
builder.Property(e => e.RuleId);
builder.Property(e => e.RuleText);
}
}
6 changes: 6 additions & 0 deletions Hammer/Data/Infraction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ public Infraction(Infraction other)
/// <value>The broken rule ID.</value>
public int? RuleId { get; internal set; }

/// <summary>
/// Gets the text of the rule which was broken.
/// </summary>
/// <value>The broken rule text.</value>
public string? RuleText { get; internal set; }

/// <summary>
/// Gets the ID of the staff member who issued the infraction.
/// </summary>
Expand Down
3 changes: 2 additions & 1 deletion Hammer/Data/InfractionBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ public Infraction Build()
StaffMemberId = StaffMember.Id,
Reason = Reason,
IssuedAt = IssuedAt,
RuleId = Rule?.Id
RuleId = Rule?.Id,
RuleText = Rule?.Brief ?? Rule?.Description
};
}

Expand Down
14 changes: 7 additions & 7 deletions Hammer/Hammer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
<VersionPrefix>5.5.2</VersionPrefix>
<VersionPrefix>5.6.0</VersionPrefix>
</PropertyGroup>

<PropertyGroup Condition="'$(VersionSuffix)' != '' And '$(BuildNumber)' == ''">
Expand All @@ -28,18 +28,18 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="DSharpPlus" Version="4.4.1"/>
<PackageReference Include="DSharpPlus.Interactivity" Version="4.4.1"/>
<PackageReference Include="DSharpPlus.SlashCommands" Version="4.4.1"/>
<PackageReference Include="DSharpPlus" Version="4.4.2"/>
<PackageReference Include="DSharpPlus.Interactivity" Version="4.4.2"/>
<PackageReference Include="DSharpPlus.SlashCommands" Version="4.4.2"/>
<PackageReference Include="Humanizer.Core" Version="2.14.1"/>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.5">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="7.0.5"/>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="7.0.9"/>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.1"/>
<PackageReference Include="NLog" Version="5.2.0"/>
<PackageReference Include="NLog.Extensions.Logging" Version="5.3.0"/>
<PackageReference Include="NLog" Version="5.2.2"/>
<PackageReference Include="NLog.Extensions.Logging" Version="5.3.2"/>
<PackageReference Include="SmartFormat.NET" Version="3.2.1"/>
<PackageReference Include="X10D" Version="3.2.2"/>
<PackageReference Include="X10D.DSharpPlus" Version="3.2.2"/>
Expand Down
27 changes: 27 additions & 0 deletions Hammer/Services/InfractionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -832,9 +832,36 @@ private void LoadGuildInfractions(DiscordGuild guild)
_logger.LogInformation("Retrieved {Count} infractions for {Guild}", cache.Count, guild);
}

private void UpdateInfractionRules(DiscordGuild guild)
{
if (!_infractionCache.TryGetValue(guild.Id, out List<Infraction>? cache))
return;

var updated = new List<Infraction>();
foreach (Infraction infraction in cache)
{
if (!infraction.RuleId.HasValue || !string.IsNullOrWhiteSpace(infraction.RuleText)) continue;
if (!_ruleService.GuildHasRule(infraction.GuildId, infraction.RuleId.Value)) continue;

Rule rule = _ruleService.GetRuleById(infraction.GuildId, infraction.RuleId.Value);
infraction.RuleText = rule.Brief ?? rule.Description;
updated.Add(infraction);
}

if (updated.Count > 0)
{
_logger.LogInformation("Updating {Count} infraction rules for {Guild}", updated.Count, guild);

using HammerContext context = _dbContextFactory.CreateDbContext();
context.UpdateRange(updated);
context.SaveChanges();
}
}

private Task OnGuildAvailable(DiscordClient sender, GuildCreateEventArgs e)
{
LoadGuildInfractions(e.Guild);
UpdateInfractionRules(e.Guild);
return Task.CompletedTask;
}

Expand Down
11 changes: 2 additions & 9 deletions Hammer/Services/MailmanService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,13 @@ namespace Hammer.Services;
internal sealed class MailmanService
{
private readonly DiscordClient _discordClient;
private readonly RuleService _ruleService;

/// <summary>
/// Initializes a new instance of the <see cref="MailmanService" /> class.
/// </summary>
public MailmanService(DiscordClient discordClient, RuleService ruleService)
public MailmanService(DiscordClient discordClient)
{
_discordClient = discordClient;
_ruleService = ruleService;
}

/// <summary>
Expand Down Expand Up @@ -67,11 +65,6 @@ public MailmanService(DiscordClient discordClient, RuleService ruleService)

string? description = infraction.Type.GetEmbedMessage();
string reason = infraction.Reason.WithWhiteSpaceAlternative(Formatter.Italic("No reason given."));
Rule? rule = null;

if (infraction.RuleId.HasValue)
rule = _ruleService.GetRuleById(guild, infraction.RuleId.Value);

var embed = new DiscordEmbedBuilder();

embed.WithColor(0xFF0000);
Expand All @@ -82,7 +75,7 @@ public MailmanService(DiscordClient discordClient, RuleService ruleService)
embed.WithThumbnail(guild.IconUrl);
embed.WithFooter(guild.Name, guild.IconUrl);
embed.AddField("Reason", reason);
embed.AddFieldIf(rule is not null, "Rule Broken", () => $"{rule!.Id} - {rule.Brief ?? rule.Description}", true);
embed.AddFieldIf(infraction.RuleId.HasValue, "Rule Broken", () => $"{infraction.RuleId} - {infraction.RuleText}", true);

switch (infraction.Type)
{
Expand Down

0 comments on commit 025e005

Please sign in to comment.