2023-09-29 18:36:16 +03:00
|
|
|
using System.Text;
|
2023-07-18 15:25:02 +03:00
|
|
|
using DiffPlex.DiffBuilder;
|
|
|
|
using JetBrains.Annotations;
|
2023-09-30 16:58:32 +03:00
|
|
|
using Octobot.Data;
|
2023-10-12 18:37:25 +03:00
|
|
|
using Octobot.Extensions;
|
2023-09-30 16:58:32 +03:00
|
|
|
using Octobot.Services;
|
2023-07-18 15:25:02 +03:00
|
|
|
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;
|
|
|
|
|
2023-09-30 16:58:32 +03:00
|
|
|
namespace Octobot.Responders;
|
2023-07-18 15:25:02 +03:00
|
|
|
|
|
|
|
/// <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;
|
2023-07-18 15:25:02 +03:00
|
|
|
|
|
|
|
public MessageEditedResponder(
|
2023-09-29 18:36:16 +03:00
|
|
|
CacheService cacheService, IDiscordRestChannelAPI channelApi, GuildDataService guildData)
|
2023-08-02 23:51:16 +03:00
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
2023-08-02 23:51:16 +03:00
|
|
|
public async Task<Result> RespondAsync(IMessageUpdate gatewayEvent, CancellationToken ct = default)
|
|
|
|
{
|
2023-12-17 20:02:50 +03:00
|
|
|
if (!gatewayEvent.ID.IsDefined(out var messageId))
|
2023-08-02 23:51:16 +03:00
|
|
|
{
|
2023-12-17 20:02:50 +03:00
|
|
|
return new ArgumentNullError(nameof(gatewayEvent.ID));
|
2023-08-02 23:51:16 +03:00
|
|
|
}
|
|
|
|
|
2023-12-17 20:02:50 +03:00
|
|
|
if (!gatewayEvent.ChannelID.IsDefined(out var channelId))
|
|
|
|
{
|
|
|
|
return new ArgumentNullError(nameof(gatewayEvent.ChannelID));
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-12-17 20:02:50 +03:00
|
|
|
if (gatewayEvent.Author.IsDefined(out var author) && author.IsBot.OrDefault(false))
|
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
|
|
|
|
2023-12-17 20:02:50 +03:00
|
|
|
if (!gatewayEvent.Content.IsDefined(out var newContent))
|
2023-08-02 23:51:16 +03:00
|
|
|
{
|
2023-12-17 20:02:50 +03:00
|
|
|
return Result.FromSuccess();
|
2023-08-02 23:51:16 +03:00
|
|
|
}
|
|
|
|
|
2023-12-17 20:02:50 +03:00
|
|
|
var cfg = await _guildData.GetSettings(guildId, ct);
|
|
|
|
if (GuildSettings.PrivateFeedbackChannel.Get(cfg).Empty())
|
2023-08-02 23:51:16 +03:00
|
|
|
{
|
2023-12-17 20:02:50 +03:00
|
|
|
return Result.FromSuccess();
|
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))
|
|
|
|
{
|
2023-11-22 11:29:27 +03:00
|
|
|
_ = _channelApi.GetChannelMessageAsync(channelId, messageId, ct);
|
|
|
|
return Result.FromSuccess();
|
2023-08-02 23:51:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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 diff = InlineDiffBuilder.Diff(message.Content, newContent);
|
|
|
|
|
|
|
|
Messages.Culture = GuildSettings.Language.Get(cfg);
|
|
|
|
|
2024-03-20 20:34:22 +03:00
|
|
|
var builder = new StringBuilder()
|
|
|
|
.AppendLine(diff.AsMarkdown())
|
|
|
|
.AppendLine(string.Format(Messages.DescriptionActionJumpToMessage,
|
|
|
|
$"https://discord.com/channels/{guildId}/{channelId}/{messageId}")
|
|
|
|
);
|
2023-09-29 18:36:16 +03:00
|
|
|
|
2023-07-18 15:25:02 +03:00
|
|
|
var embed = new EmbedBuilder()
|
|
|
|
.WithSmallTitle(string.Format(Messages.CachedMessageEdited, message.Author.GetTag()), message.Author)
|
2023-09-29 18:36:16 +03:00
|
|
|
.WithDescription(builder.ToString())
|
2023-07-18 15:25:02 +03:00
|
|
|
.WithTimestamp(timestamp.Value)
|
|
|
|
.WithColour(ColorsList.Yellow)
|
|
|
|
.Build();
|
|
|
|
|
2023-12-17 19:47:52 +03:00
|
|
|
return await _channelApi.CreateMessageWithEmbedResultAsync(
|
|
|
|
GuildSettings.PrivateFeedbackChannel.Get(cfg), embedResult: embed,
|
2023-09-30 16:58:32 +03:00
|
|
|
allowedMentions: Octobot.NoMentions, ct: ct);
|
2023-07-18 15:25:02 +03:00
|
|
|
}
|
|
|
|
}
|