2021-07-28 06:23:01 +00:00
|
|
|
|
#nullable enable
|
2021-05-03 10:33:30 +00:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
2021-07-28 06:23:01 +00:00
|
|
|
|
using Myriad.Cache;
|
|
|
|
|
using Myriad.Extensions;
|
2021-05-03 10:33:30 +00:00
|
|
|
|
using Myriad.Rest;
|
|
|
|
|
using Myriad.Rest.Exceptions;
|
|
|
|
|
using Myriad.Types;
|
|
|
|
|
|
|
|
|
|
using NodaTime;
|
|
|
|
|
|
|
|
|
|
using PluralKit.Core;
|
|
|
|
|
|
|
|
|
|
namespace PluralKit.Bot
|
|
|
|
|
{
|
|
|
|
|
public class MessageEdit
|
|
|
|
|
{
|
|
|
|
|
private static readonly Duration EditTimeout = Duration.FromMinutes(10);
|
|
|
|
|
|
|
|
|
|
private readonly IDatabase _db;
|
|
|
|
|
private readonly ModelRepository _repo;
|
|
|
|
|
private readonly IClock _clock;
|
|
|
|
|
private readonly DiscordApiClient _rest;
|
|
|
|
|
private readonly WebhookExecutorService _webhookExecutor;
|
2021-05-07 16:35:09 +00:00
|
|
|
|
private readonly LogChannelService _logChannel;
|
2021-07-28 06:23:01 +00:00
|
|
|
|
private readonly IDiscordCache _cache;
|
2021-05-03 10:33:30 +00:00
|
|
|
|
|
2021-07-28 06:23:01 +00:00
|
|
|
|
public MessageEdit(IDatabase db, ModelRepository repo, IClock clock, DiscordApiClient rest, WebhookExecutorService webhookExecutor, LogChannelService logChannel, IDiscordCache cache)
|
2021-05-03 10:33:30 +00:00
|
|
|
|
{
|
|
|
|
|
_db = db;
|
|
|
|
|
_repo = repo;
|
|
|
|
|
_clock = clock;
|
|
|
|
|
_rest = rest;
|
|
|
|
|
_webhookExecutor = webhookExecutor;
|
2021-05-07 16:35:09 +00:00
|
|
|
|
_logChannel = logChannel;
|
2021-07-28 06:23:01 +00:00
|
|
|
|
_cache = cache;
|
2021-05-03 10:33:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task EditMessage(Context ctx)
|
|
|
|
|
{
|
|
|
|
|
var msg = await GetMessageToEdit(ctx);
|
|
|
|
|
if (!ctx.HasNext())
|
|
|
|
|
throw new PKSyntaxError("You need to include the message to edit in.");
|
|
|
|
|
|
2021-07-27 15:39:37 +00:00
|
|
|
|
if (ctx.System.Id != msg.System.Id)
|
2021-07-28 06:19:37 +00:00
|
|
|
|
throw new PKError("Can't edit a message sent by a different system.");
|
2021-05-03 10:33:30 +00:00
|
|
|
|
|
2021-07-28 06:23:01 +00:00
|
|
|
|
if (_cache.GetRootChannel(msg.Message.Channel).Id != msg.Message.Channel)
|
|
|
|
|
throw new PKError("PluralKit cannot edit messages in threads.");
|
|
|
|
|
|
2021-05-03 10:33:30 +00:00
|
|
|
|
var newContent = ctx.RemainderOrNull();
|
|
|
|
|
|
2021-07-27 15:39:37 +00:00
|
|
|
|
var originalMsg = await _rest.GetMessage(msg.Message.Channel, msg.Message.Mid);
|
2021-05-07 16:35:09 +00:00
|
|
|
|
|
2021-05-03 10:33:30 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
2021-07-27 15:39:37 +00:00
|
|
|
|
await _webhookExecutor.EditWebhookMessage(msg.Message.Channel, msg.Message.Mid, newContent);
|
2021-05-03 10:33:30 +00:00
|
|
|
|
|
2021-07-18 00:39:12 +00:00
|
|
|
|
if (ctx.Guild == null)
|
|
|
|
|
await _rest.CreateReaction(ctx.Channel.Id, ctx.Message.Id, new() { Name = Emojis.Success });
|
|
|
|
|
|
2021-05-03 10:33:30 +00:00
|
|
|
|
if (ctx.BotPermissions.HasFlag(PermissionSet.ManageMessages))
|
|
|
|
|
await _rest.DeleteMessage(ctx.Channel.Id, ctx.Message.Id);
|
2021-05-07 16:35:09 +00:00
|
|
|
|
|
|
|
|
|
await _logChannel.LogEditedMessage(ctx.MessageContext, msg, ctx.Message, originalMsg!, newContent);
|
2021-05-03 10:33:30 +00:00
|
|
|
|
}
|
|
|
|
|
catch (NotFoundException)
|
|
|
|
|
{
|
|
|
|
|
throw new PKError("Could not edit message.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-27 15:39:37 +00:00
|
|
|
|
private async Task<FullMessage> GetMessageToEdit(Context ctx)
|
2021-05-03 10:33:30 +00:00
|
|
|
|
{
|
2021-07-27 15:39:37 +00:00
|
|
|
|
await using var conn = await _db.Obtain();
|
|
|
|
|
FullMessage? msg = null;
|
|
|
|
|
|
2021-05-03 10:33:30 +00:00
|
|
|
|
var referencedMessage = ctx.MatchMessage(false);
|
|
|
|
|
if (referencedMessage != null)
|
|
|
|
|
{
|
2021-07-27 15:39:37 +00:00
|
|
|
|
msg = await _repo.GetMessage(conn, referencedMessage.Value);
|
2021-05-03 10:33:30 +00:00
|
|
|
|
if (msg == null)
|
|
|
|
|
throw new PKError("This is not a message proxied by PluralKit.");
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-27 15:39:37 +00:00
|
|
|
|
if (msg == null)
|
|
|
|
|
{
|
|
|
|
|
if (ctx.Guild == null)
|
|
|
|
|
throw new PKError("You must use a message link to edit messages in DMs.");
|
2021-05-07 21:31:43 +00:00
|
|
|
|
|
2021-07-27 15:39:37 +00:00
|
|
|
|
var recent = await FindRecentMessage(ctx);
|
|
|
|
|
if (recent == null)
|
|
|
|
|
throw new PKError("Could not find a recent message to edit.");
|
|
|
|
|
|
|
|
|
|
msg = await _repo.GetMessage(conn, recent.Mid);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return msg;
|
2021-05-03 10:33:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task<PKMessage?> FindRecentMessage(Context ctx)
|
|
|
|
|
{
|
|
|
|
|
await using var conn = await _db.Obtain();
|
|
|
|
|
var lastMessage = await _repo.GetLastMessage(conn, ctx.Guild.Id, ctx.Channel.Id, ctx.Author.Id);
|
|
|
|
|
if (lastMessage == null)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
var timestamp = DiscordUtils.SnowflakeToInstant(lastMessage.Mid);
|
|
|
|
|
if (_clock.GetCurrentInstant() - timestamp > EditTimeout)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
return lastMessage;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-07-18 00:39:12 +00:00
|
|
|
|
}
|