feat: add role create/update responders
This commit is contained in:
parent
b5e34d517a
commit
48649feb14
5 changed files with 167 additions and 19 deletions
|
|
@ -0,0 +1,43 @@
|
||||||
|
using Catalogger.Backend.Cache.InMemoryCache;
|
||||||
|
using Catalogger.Backend.Database;
|
||||||
|
using Catalogger.Backend.Database.Queries;
|
||||||
|
using Catalogger.Backend.Extensions;
|
||||||
|
using Catalogger.Backend.Services;
|
||||||
|
using Remora.Discord.API.Abstractions.Gateway.Events;
|
||||||
|
using Remora.Discord.Extensions.Embeds;
|
||||||
|
using Remora.Discord.Gateway.Responders;
|
||||||
|
using Remora.Results;
|
||||||
|
|
||||||
|
namespace Catalogger.Backend.Bot.Responders.Roles;
|
||||||
|
|
||||||
|
public class RoleCreateResponder(
|
||||||
|
ILogger logger,
|
||||||
|
DatabaseContext db,
|
||||||
|
RoleCache roleCache,
|
||||||
|
WebhookExecutorService webhookExecutor) : IResponder<IGuildRoleCreate>
|
||||||
|
{
|
||||||
|
private readonly ILogger _logger = logger.ForContext<RoleCreateResponder>();
|
||||||
|
|
||||||
|
public async Task<Result> RespondAsync(IGuildRoleCreate evt, CancellationToken ct = default)
|
||||||
|
{
|
||||||
|
_logger.Debug("Received new role {RoleId} in guild {GuildId}", evt.Role.ID, evt.GuildID);
|
||||||
|
roleCache.Set(evt.Role, evt.GuildID);
|
||||||
|
|
||||||
|
var guildConfig = await db.GetGuildAsync(evt.GuildID, ct);
|
||||||
|
|
||||||
|
var embed = new EmbedBuilder()
|
||||||
|
.WithTitle("Role created")
|
||||||
|
.WithColour(DiscordUtils.Green)
|
||||||
|
.WithDescription($"**Name:** {evt.Role.Name}\n**Colour:** {evt.Role.Colour.ToPrettyString()}" +
|
||||||
|
$"\n**Mentionable:** {evt.Role.IsMentionable}\n**Shown separately:** {evt.Role.IsHoisted}");
|
||||||
|
|
||||||
|
if (!evt.Role.Permissions.Value.IsZero)
|
||||||
|
{
|
||||||
|
embed.AddField("Permissions", evt.Role.Permissions.ToPrettyString(), inline: false);
|
||||||
|
}
|
||||||
|
|
||||||
|
webhookExecutor.QueueLog(guildConfig, LogChannelType.GuildRoleCreate, embed.Build().GetOrThrow());
|
||||||
|
|
||||||
|
return Result.Success;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,96 @@
|
||||||
|
using Catalogger.Backend.Cache.InMemoryCache;
|
||||||
|
using Catalogger.Backend.Database;
|
||||||
|
using Catalogger.Backend.Database.Queries;
|
||||||
|
using Catalogger.Backend.Extensions;
|
||||||
|
using Catalogger.Backend.Services;
|
||||||
|
using Humanizer;
|
||||||
|
using Remora.Discord.API.Abstractions.Gateway.Events;
|
||||||
|
using Remora.Discord.API.Abstractions.Objects;
|
||||||
|
using Remora.Discord.Extensions.Embeds;
|
||||||
|
using Remora.Discord.Gateway.Responders;
|
||||||
|
using Remora.Results;
|
||||||
|
|
||||||
|
namespace Catalogger.Backend.Bot.Responders.Roles;
|
||||||
|
|
||||||
|
public class RoleUpdateResponder(ILogger logger,
|
||||||
|
DatabaseContext db,
|
||||||
|
RoleCache roleCache,
|
||||||
|
WebhookExecutorService webhookExecutor) : IResponder<IGuildRoleUpdate>
|
||||||
|
{
|
||||||
|
private readonly ILogger _logger = logger.ForContext<RoleUpdateResponder>();
|
||||||
|
|
||||||
|
public async Task<Result> RespondAsync(IGuildRoleUpdate evt, CancellationToken ct = default)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var newRole = evt.Role;
|
||||||
|
|
||||||
|
if (!roleCache.TryGet(evt.Role.ID, out var oldRole))
|
||||||
|
{
|
||||||
|
_logger.Information("Received role update event for {RoleId} but it wasn't cached, ignoring", evt.Role.ID);
|
||||||
|
return Result.Success;
|
||||||
|
}
|
||||||
|
|
||||||
|
var embed = new EmbedBuilder()
|
||||||
|
.WithTitle($"Role \"{evt.Role.Name}\" updated")
|
||||||
|
.WithColour(DiscordUtils.Blue)
|
||||||
|
.WithFooter($"ID: {evt.Role.ID}")
|
||||||
|
.WithCurrentTimestamp();
|
||||||
|
|
||||||
|
if (newRole.Name != oldRole.Name)
|
||||||
|
{
|
||||||
|
embed.AddField("Name", $"**Before:** {oldRole.Name}\n**After:** {newRole.Name}");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (newRole.IsHoisted != oldRole.IsHoisted || newRole.IsMentionable != oldRole.IsMentionable)
|
||||||
|
{
|
||||||
|
embed.AddField(
|
||||||
|
"\u200b", $"**Mentionable:** {newRole.IsMentionable}\n**Shown separately:** {newRole.IsHoisted}");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (newRole.Colour != oldRole.Colour)
|
||||||
|
{
|
||||||
|
embed.AddField("Colour",
|
||||||
|
$"**Before:** {oldRole.Colour.ToPrettyString()}\n**After:** {newRole.Colour.ToPrettyString()}");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (newRole.Permissions.Value != oldRole.Permissions.Value)
|
||||||
|
{
|
||||||
|
var diff = string.Join("\n", PermissionUpdate(oldRole.Permissions, newRole.Permissions));
|
||||||
|
embed.AddField("Permissions", $"```diff\n{diff}\n```");
|
||||||
|
}
|
||||||
|
|
||||||
|
// All updates are shown in fields. If there are no fields, there were no updates we care about
|
||||||
|
// (we don't care about position, for example, because it's not actually useful)
|
||||||
|
if (embed.Fields.Count == 0)
|
||||||
|
{
|
||||||
|
_logger.Debug("We don't care about update of role {RoleId}, ignoring", evt.Role.ID);
|
||||||
|
return Result.Success;
|
||||||
|
}
|
||||||
|
|
||||||
|
var guildConfig = await db.GetGuildAsync(evt.GuildID, ct);
|
||||||
|
webhookExecutor.QueueLog(guildConfig, LogChannelType.GuildRoleUpdate, embed.Build().GetOrThrow());
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
roleCache.Set(evt.Role, evt.GuildID);
|
||||||
|
}
|
||||||
|
|
||||||
|
return Result.Success;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static IEnumerable<string> PermissionUpdate(IDiscordPermissionSet oldValue, IDiscordPermissionSet newValue)
|
||||||
|
{
|
||||||
|
foreach (var perm in Enum.GetValues<DiscordPermission>())
|
||||||
|
{
|
||||||
|
if (!oldValue.HasPermission(perm) && newValue.HasPermission(perm))
|
||||||
|
{
|
||||||
|
yield return $"+ {perm.Humanize(LetterCasing.Title)}";
|
||||||
|
}
|
||||||
|
else if (oldValue.HasPermission(perm) && !newValue.HasPermission(perm))
|
||||||
|
{
|
||||||
|
yield return $"- {perm.Humanize(LetterCasing.Title)}";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -26,8 +26,8 @@
|
||||||
<PackageReference Include="Polly.RateLimiting" Version="8.4.2"/>
|
<PackageReference Include="Polly.RateLimiting" Version="8.4.2"/>
|
||||||
<PackageReference Include="prometheus-net" Version="8.2.1"/>
|
<PackageReference Include="prometheus-net" Version="8.2.1"/>
|
||||||
<PackageReference Include="prometheus-net.AspNetCore" Version="8.2.1"/>
|
<PackageReference Include="prometheus-net.AspNetCore" Version="8.2.1"/>
|
||||||
|
<PackageReference Include="Remora.Sdk" Version="3.1.2"/>
|
||||||
<PackageReference Include="Remora.Discord" Version="2024.3.0"/>
|
<PackageReference Include="Remora.Discord" Version="2024.3.0"/>
|
||||||
<!-- <ProjectReference Include="..\..\Remora.Discord\Remora.Discord\Remora.Discord.csproj" />-->
|
|
||||||
<PackageReference Include="Serilog" Version="4.0.2"/>
|
<PackageReference Include="Serilog" Version="4.0.2"/>
|
||||||
<PackageReference Include="Serilog.AspNetCore" Version="8.0.2"/>
|
<PackageReference Include="Serilog.AspNetCore" Version="8.0.2"/>
|
||||||
<PackageReference Include="Serilog.Extensions.Hosting" Version="8.0.0"/>
|
<PackageReference Include="Serilog.Extensions.Hosting" Version="8.0.0"/>
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
using System.Drawing;
|
||||||
using Humanizer;
|
using Humanizer;
|
||||||
using OneOf;
|
using OneOf;
|
||||||
using Remora.Discord.API.Abstractions.Objects;
|
using Remora.Discord.API.Abstractions.Objects;
|
||||||
|
|
@ -48,6 +49,15 @@ public static class DiscordExtensions
|
||||||
return snowflake.Value.Value;
|
return snowflake.Value.Value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static string ToPrettyString(this Color color)
|
||||||
|
{
|
||||||
|
var r = color.R.ToString("X2");
|
||||||
|
var g = color.G.ToString("X2");
|
||||||
|
var b = color.B.ToString("X2");
|
||||||
|
|
||||||
|
return $"#{r}{g}{b}";
|
||||||
|
}
|
||||||
|
|
||||||
public static bool Is(this Optional<Snowflake> s1, Snowflake s2) => s1.IsDefined(out var value) && value == s2;
|
public static bool Is(this Optional<Snowflake> s1, Snowflake s2) => s1.IsDefined(out var value) && value == s2;
|
||||||
public static bool Is(this Optional<Snowflake> s1, ulong s2) => s1.IsDefined(out var value) && value == s2;
|
public static bool Is(this Optional<Snowflake> s1, ulong s2) => s1.IsDefined(out var value) && value == s2;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,6 @@ using Remora.Discord.API.Abstractions.Gateway.Commands;
|
||||||
using Remora.Discord.Commands.Extensions;
|
using Remora.Discord.Commands.Extensions;
|
||||||
using Remora.Discord.Extensions.Extensions;
|
using Remora.Discord.Extensions.Extensions;
|
||||||
using Remora.Discord.Gateway;
|
using Remora.Discord.Gateway;
|
||||||
using Remora.Discord.Hosting.Extensions;
|
|
||||||
using Remora.Discord.Interactivity.Extensions;
|
using Remora.Discord.Interactivity.Extensions;
|
||||||
using Remora.Discord.Pagination.Extensions;
|
using Remora.Discord.Pagination.Extensions;
|
||||||
using Serilog;
|
using Serilog;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue