2023-07-18 15:25:02 +03:00
|
|
|
using Boyfriend.Data;
|
|
|
|
using Boyfriend.Services;
|
|
|
|
using DiffPlex.DiffBuilder;
|
|
|
|
using JetBrains.Annotations;
|
|
|
|
using Remora.Discord.API.Abstractions.Gateway.Events;
|
|
|
|
using Remora.Discord.API.Abstractions.Objects;
|
|
|
|
using Remora.Discord.API.Abstractions.Rest;
|
|
|
|
using Remora.Discord.Caching;
|
|
|
|
using Remora.Discord.Caching.Services;
|
|
|
|
using Remora.Discord.Extensions.Embeds;
|
|
|
|
using Remora.Discord.Gateway.Responders;
|
|
|
|
using Remora.Results;
|
|
|
|
|
|
|
|
namespace Boyfriend.Responders;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Handles logging the difference between an edited message's old and new content
|
|
|
|
/// to a guild's <see cref="GuildSettings.PrivateFeedbackChannel" /> if one is set.
|
|
|
|
/// </summary>
|
|
|
|
[UsedImplicitly]
|
2023-08-02 23:51:16 +03:00
|
|
|
public class MessageEditedResponder : IResponder<IMessageUpdate>
|
|
|
|
{
|
|
|
|
private readonly CacheService _cacheService;
|
2023-07-18 15:25:02 +03:00
|
|
|
private readonly IDiscordRestChannelAPI _channelApi;
|
2023-08-02 23:51:16 +03:00
|
|
|
private readonly GuildDataService _guildData;
|
|
|
|
private readonly IDiscordRestUserAPI _userApi;
|
2023-07-18 15:25:02 +03:00
|
|
|
|
|
|
|
public MessageEditedResponder(
|
2023-08-02 23:51:16 +03:00
|
|
|
CacheService cacheService, IDiscordRestChannelAPI channelApi, GuildDataService guildData,
|
|
|
|
IDiscordRestUserAPI userApi)
|
|
|
|
{
|
2023-07-18 15:25:02 +03:00
|
|
|
_cacheService = cacheService;
|
|
|
|
_channelApi = channelApi;
|
2023-08-02 23:51:16 +03:00
|
|
|
_guildData = guildData;
|
2023-07-18 15:25:02 +03:00
|
|
|
_userApi = userApi;
|
|
|
|
}
|
|
|
|
|
2023-08-02 23:51:16 +03:00
|
|
|
public async Task<Result> RespondAsync(IMessageUpdate gatewayEvent, CancellationToken ct = default)
|
|
|
|
{
|
2023-07-18 15:25:02 +03:00
|
|
|
if (!gatewayEvent.GuildID.IsDefined(out var guildId))
|
2023-08-02 23:51:16 +03:00
|
|
|
{
|
2023-07-18 15:25:02 +03:00
|
|
|
return Result.FromSuccess();
|
2023-08-02 23:51:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var cfg = await _guildData.GetSettings(guildId, ct);
|
2023-07-18 15:25:02 +03:00
|
|
|
if (GuildSettings.PrivateFeedbackChannel.Get(cfg).Empty())
|
2023-08-02 23:51:16 +03:00
|
|
|
{
|
2023-07-18 15:25:02 +03:00
|
|
|
return Result.FromSuccess();
|
2023-08-02 23:51:16 +03:00
|
|
|
}
|
|
|
|
|
2023-07-18 15:25:02 +03:00
|
|
|
if (!gatewayEvent.Content.IsDefined(out var newContent))
|
2023-08-02 23:51:16 +03:00
|
|
|
{
|
2023-07-18 15:25:02 +03:00
|
|
|
return Result.FromSuccess();
|
2023-08-02 23:51:16 +03:00
|
|
|
}
|
|
|
|
|
2023-07-18 15:25:02 +03:00
|
|
|
if (!gatewayEvent.EditedTimestamp.IsDefined(out var timestamp))
|
2023-08-02 23:51:16 +03:00
|
|
|
{
|
2023-07-18 15:25:02 +03:00
|
|
|
return Result.FromSuccess(); // The message wasn't actually edited
|
2023-08-02 23:51:16 +03:00
|
|
|
}
|
2023-07-18 15:25:02 +03:00
|
|
|
|
|
|
|
if (!gatewayEvent.ChannelID.IsDefined(out var channelId))
|
2023-08-02 23:51:16 +03:00
|
|
|
{
|
2023-07-28 19:58:55 +03:00
|
|
|
return new ArgumentNullError(nameof(gatewayEvent.ChannelID));
|
2023-08-02 23:51:16 +03:00
|
|
|
}
|
|
|
|
|
2023-07-18 15:25:02 +03:00
|
|
|
if (!gatewayEvent.ID.IsDefined(out var messageId))
|
2023-08-02 23:51:16 +03:00
|
|
|
{
|
2023-07-28 19:58:55 +03:00
|
|
|
return new ArgumentNullError(nameof(gatewayEvent.ID));
|
2023-08-02 23:51:16 +03:00
|
|
|
}
|
2023-07-18 15:25:02 +03:00
|
|
|
|
|
|
|
var cacheKey = new KeyHelpers.MessageCacheKey(channelId, messageId);
|
|
|
|
var messageResult = await _cacheService.TryGetValueAsync<IMessage>(
|
|
|
|
cacheKey, ct);
|
2023-08-02 23:51:16 +03:00
|
|
|
if (!messageResult.IsDefined(out var message))
|
|
|
|
{
|
|
|
|
return Result.FromError(messageResult);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (message.Content == newContent)
|
|
|
|
{
|
|
|
|
return Result.FromSuccess();
|
|
|
|
}
|
2023-07-18 15:25:02 +03:00
|
|
|
|
|
|
|
// Custom event responders are called earlier than responders responsible for message caching
|
|
|
|
// This means that subsequent edit logs may contain the wrong content
|
|
|
|
// We can work around this by evicting the message from the cache
|
|
|
|
await _cacheService.EvictAsync<IMessage>(cacheKey, ct);
|
|
|
|
// However, since we evicted the message, subsequent edits won't have a cached instance to work with
|
|
|
|
// Getting the message will put it back in the cache, resolving all issues
|
|
|
|
// We don't need to await this since the result is not needed
|
|
|
|
// NOTE: Because this is not awaited, there may be a race condition depending on how fast clients are able to edit their messages
|
|
|
|
// NOTE: Awaiting this might not even solve this if the same responder is called asynchronously
|
|
|
|
_ = _channelApi.GetChannelMessageAsync(channelId, messageId, ct);
|
|
|
|
|
|
|
|
var currentUserResult = await _userApi.GetCurrentUserAsync(ct);
|
2023-08-02 23:51:16 +03:00
|
|
|
if (!currentUserResult.IsDefined(out var currentUser))
|
|
|
|
{
|
|
|
|
return Result.FromError(currentUserResult);
|
|
|
|
}
|
2023-07-18 15:25:02 +03:00
|
|
|
|
|
|
|
var diff = InlineDiffBuilder.Diff(message.Content, newContent);
|
|
|
|
|
|
|
|
Messages.Culture = GuildSettings.Language.Get(cfg);
|
|
|
|
|
|
|
|
var embed = new EmbedBuilder()
|
|
|
|
.WithSmallTitle(string.Format(Messages.CachedMessageEdited, message.Author.GetTag()), message.Author)
|
|
|
|
.WithDescription($"https://discord.com/channels/{guildId}/{channelId}/{messageId}\n{diff.AsMarkdown()}")
|
|
|
|
.WithUserFooter(currentUser)
|
|
|
|
.WithTimestamp(timestamp.Value)
|
|
|
|
.WithColour(ColorsList.Yellow)
|
|
|
|
.Build();
|
2023-08-02 23:51:16 +03:00
|
|
|
if (!embed.IsDefined(out var built))
|
|
|
|
{
|
|
|
|
return Result.FromError(embed);
|
|
|
|
}
|
2023-07-18 15:25:02 +03:00
|
|
|
|
|
|
|
return (Result)await _channelApi.CreateMessageAsync(
|
|
|
|
GuildSettings.PrivateFeedbackChannel.Get(cfg), embeds: new[] { built },
|
|
|
|
allowedMentions: Boyfriend.NoMentions, ct: ct);
|
|
|
|
}
|
|
|
|
}
|