2023-09-29 18:36:16 +03:00
|
|
|
using System.Text;
|
2023-07-18 15:25:02 +03:00
|
|
|
using JetBrains.Annotations;
|
2023-09-30 16:58:32 +03:00
|
|
|
using Octobot.Data;
|
|
|
|
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.Extensions.Embeds;
|
|
|
|
using Remora.Discord.Extensions.Formatting;
|
|
|
|
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 contents of a deleted message and the user who deleted the message
|
|
|
|
/// to a guild's <see cref="GuildSettings.PrivateFeedbackChannel" /> if one is set.
|
|
|
|
/// </summary>
|
|
|
|
[UsedImplicitly]
|
2023-08-02 23:51:16 +03:00
|
|
|
public class MessageDeletedResponder : IResponder<IMessageDelete>
|
|
|
|
{
|
2023-07-18 15:25:02 +03:00
|
|
|
private readonly IDiscordRestAuditLogAPI _auditLogApi;
|
2023-08-02 23:51:16 +03:00
|
|
|
private readonly IDiscordRestChannelAPI _channelApi;
|
|
|
|
private readonly GuildDataService _guildData;
|
|
|
|
private readonly IDiscordRestUserAPI _userApi;
|
2023-07-18 15:25:02 +03:00
|
|
|
|
|
|
|
public MessageDeletedResponder(
|
|
|
|
IDiscordRestAuditLogAPI auditLogApi, IDiscordRestChannelAPI channelApi,
|
2023-08-02 23:51:16 +03:00
|
|
|
GuildDataService guildData, IDiscordRestUserAPI userApi)
|
|
|
|
{
|
2023-07-18 15:25:02 +03:00
|
|
|
_auditLogApi = auditLogApi;
|
|
|
|
_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(IMessageDelete gatewayEvent, CancellationToken ct = default)
|
|
|
|
{
|
|
|
|
if (!gatewayEvent.GuildID.IsDefined(out var guildId))
|
|
|
|
{
|
|
|
|
return Result.FromSuccess();
|
|
|
|
}
|
2023-07-18 15:25:02 +03:00
|
|
|
|
2023-08-02 23:51:16 +03:00
|
|
|
var cfg = await _guildData.GetSettings(guildId, ct);
|
|
|
|
if (GuildSettings.PrivateFeedbackChannel.Get(cfg).Empty())
|
|
|
|
{
|
|
|
|
return Result.FromSuccess();
|
|
|
|
}
|
2023-07-18 15:25:02 +03:00
|
|
|
|
|
|
|
var messageResult = await _channelApi.GetChannelMessageAsync(gatewayEvent.ChannelID, gatewayEvent.ID, ct);
|
2023-08-02 23:51:16 +03:00
|
|
|
if (!messageResult.IsDefined(out var message))
|
|
|
|
{
|
|
|
|
return Result.FromError(messageResult);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(message.Content))
|
|
|
|
{
|
|
|
|
return Result.FromSuccess();
|
|
|
|
}
|
2023-07-18 15:25:02 +03:00
|
|
|
|
|
|
|
var auditLogResult = await _auditLogApi.GetGuildAuditLogAsync(
|
|
|
|
guildId, actionType: AuditLogEvent.MessageDelete, limit: 1, ct: ct);
|
2023-08-02 23:51:16 +03:00
|
|
|
if (!auditLogResult.IsDefined(out var auditLogPage))
|
|
|
|
{
|
|
|
|
return Result.FromError(auditLogResult);
|
|
|
|
}
|
2023-07-18 15:25:02 +03:00
|
|
|
|
|
|
|
var auditLog = auditLogPage.AuditLogEntries.Single();
|
|
|
|
|
2023-10-04 18:21:10 +03:00
|
|
|
var deleterResult = Result<IUser>.FromSuccess(message.Author);
|
2023-08-02 23:51:16 +03:00
|
|
|
if (auditLog.UserID is not null
|
|
|
|
&& auditLog.Options.Value.ChannelID == gatewayEvent.ChannelID
|
2023-07-20 10:36:21 +03:00
|
|
|
&& DateTimeOffset.UtcNow.Subtract(auditLog.ID.Timestamp).TotalSeconds <= 2)
|
2023-08-02 23:51:16 +03:00
|
|
|
{
|
2023-10-04 18:21:10 +03:00
|
|
|
deleterResult = await _userApi.GetUserAsync(auditLog.UserID.Value, ct);
|
2023-08-02 23:51:16 +03:00
|
|
|
}
|
2023-07-20 10:36:21 +03:00
|
|
|
|
2023-10-04 18:21:10 +03:00
|
|
|
if (!deleterResult.IsDefined(out var deleter))
|
2023-08-02 23:51:16 +03:00
|
|
|
{
|
2023-10-04 18:21:10 +03:00
|
|
|
return Result.FromError(deleterResult);
|
2023-08-02 23:51:16 +03:00
|
|
|
}
|
2023-07-18 15:25:02 +03:00
|
|
|
|
|
|
|
Messages.Culture = GuildSettings.Language.Get(cfg);
|
|
|
|
|
2023-09-29 18:36:16 +03:00
|
|
|
var builder = new StringBuilder().AppendLine(
|
|
|
|
string.Format(Messages.DescriptionActionJumpToChannel,
|
|
|
|
Mention.Channel(gatewayEvent.ChannelID)))
|
|
|
|
.AppendLine(message.Content.InBlockCode());
|
|
|
|
|
2023-07-18 15:25:02 +03:00
|
|
|
var embed = new EmbedBuilder()
|
|
|
|
.WithSmallTitle(
|
|
|
|
string.Format(
|
|
|
|
Messages.CachedMessageDeleted,
|
|
|
|
message.Author.GetTag()), message.Author)
|
2023-09-29 18:36:16 +03:00
|
|
|
.WithDescription(builder.ToString())
|
2023-10-04 18:21:10 +03:00
|
|
|
.WithActionFooter(deleter)
|
2023-07-18 15:25:02 +03:00
|
|
|
.WithTimestamp(message.Timestamp)
|
|
|
|
.WithColour(ColorsList.Red)
|
|
|
|
.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 },
|
2023-09-30 16:58:32 +03:00
|
|
|
allowedMentions: Octobot.NoMentions, ct: ct);
|
2023-07-18 15:25:02 +03:00
|
|
|
}
|
|
|
|
}
|