2021-08-27 15:03:47 +00:00
|
|
|
#nullable enable
|
2021-09-30 01:51:38 +00:00
|
|
|
using System;
|
2021-05-03 10:33:30 +00:00
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
2021-08-25 18:36:13 +00:00
|
|
|
using Myriad.Builders;
|
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
|
|
|
|
{
|
2021-08-25 18:36:13 +00:00
|
|
|
public class ProxiedMessage
|
2021-05-03 10:33:30 +00:00
|
|
|
{
|
|
|
|
private static readonly Duration EditTimeout = Duration.FromMinutes(10);
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2021-05-03 10:33:30 +00:00
|
|
|
private readonly IDatabase _db;
|
|
|
|
private readonly ModelRepository _repo;
|
2021-08-25 18:36:13 +00:00
|
|
|
private readonly EmbedService _embeds;
|
2021-05-03 10:33:30 +00:00
|
|
|
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-08-25 18:36:13 +00:00
|
|
|
public ProxiedMessage(IDatabase db, ModelRepository repo, EmbedService embeds, IClock clock, DiscordApiClient rest,
|
|
|
|
WebhookExecutorService webhookExecutor, LogChannelService logChannel, IDiscordCache cache)
|
2021-05-03 10:33:30 +00:00
|
|
|
{
|
|
|
|
_db = db;
|
|
|
|
_repo = repo;
|
2021-08-25 18:36:13 +00:00
|
|
|
_embeds = embeds;
|
2021-05-03 10:33:30 +00:00
|
|
|
_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-08-27 15:03:47 +00:00
|
|
|
|
2021-09-13 05:31:15 +00:00
|
|
|
var newContent = ctx.RemainderOrNull().NormalizeLineEndSpacing();
|
2021-05-03 10:33:30 +00:00
|
|
|
|
2021-07-27 15:39:37 +00:00
|
|
|
var originalMsg = await _rest.GetMessage(msg.Message.Channel, msg.Message.Mid);
|
2021-08-04 04:41:51 +00:00
|
|
|
if (originalMsg == null)
|
|
|
|
throw new PKError("Could not edit message.");
|
2021-05-07 16:35:09 +00:00
|
|
|
|
2021-05-03 10:33:30 +00:00
|
|
|
try
|
|
|
|
{
|
2021-08-04 04:41:51 +00:00
|
|
|
var editedMsg = await _webhookExecutor.EditWebhookMessage(msg.Message.Channel, msg.Message.Mid, newContent);
|
2021-08-27 15:03:47 +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
|
|
|
|
2021-08-04 04:41:51 +00:00
|
|
|
await _logChannel.LogMessage(ctx.MessageContext, msg.Message, ctx.Message, editedMsg, originalMsg!.Content!);
|
2021-05-03 10:33:30 +00:00
|
|
|
}
|
|
|
|
catch (NotFoundException)
|
|
|
|
{
|
|
|
|
throw new PKError("Could not edit message.");
|
|
|
|
}
|
|
|
|
}
|
2021-08-27 15:03:47 +00:00
|
|
|
|
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-08-04 01:06:14 +00:00
|
|
|
var (referencedMessage, _) = ctx.MatchMessage(false);
|
2021-05-03 10:33:30 +00:00
|
|
|
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);
|
2021-08-08 19:56:24 +00:00
|
|
|
if (msg == null)
|
|
|
|
throw new PKError("Could not find a recent message to edit.");
|
2021-07-27 15:39:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return msg;
|
2021-05-03 10:33:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private async Task<PKMessage?> FindRecentMessage(Context ctx)
|
|
|
|
{
|
2021-09-30 01:51:38 +00:00
|
|
|
var lastMessage = await _repo.GetLastMessage(ctx.Guild.Id, ctx.Channel.Id, ctx.Author.Id);
|
2021-05-03 10:33:30 +00:00
|
|
|
if (lastMessage == null)
|
|
|
|
return null;
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2021-05-03 10:33:30 +00:00
|
|
|
var timestamp = DiscordUtils.SnowflakeToInstant(lastMessage.Mid);
|
|
|
|
if (_clock.GetCurrentInstant() - timestamp > EditTimeout)
|
|
|
|
return null;
|
|
|
|
|
|
|
|
return lastMessage;
|
|
|
|
}
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2021-08-25 18:36:13 +00:00
|
|
|
public async Task GetMessage(Context ctx)
|
|
|
|
{
|
|
|
|
var (messageId, _) = ctx.MatchMessage(true);
|
|
|
|
if (messageId == null)
|
|
|
|
{
|
|
|
|
if (!ctx.HasNext())
|
|
|
|
throw new PKSyntaxError("You must pass a message ID or link.");
|
|
|
|
throw new PKSyntaxError($"Could not parse {ctx.PeekArgument().AsCode()} as a message ID or link.");
|
|
|
|
}
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2021-09-27 02:49:43 +00:00
|
|
|
var isDelete = ctx.Match("delete") || ctx.MatchFlag("delete");
|
|
|
|
|
2021-08-25 18:36:13 +00:00
|
|
|
var message = await _db.Execute(c => _repo.GetMessage(c, messageId.Value));
|
2021-09-27 02:49:43 +00:00
|
|
|
if (message == null)
|
|
|
|
{
|
|
|
|
if (isDelete)
|
|
|
|
{
|
|
|
|
await DeleteCommandMessage(ctx, messageId.Value);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
throw Errors.MessageNotFound(messageId.Value);
|
|
|
|
}
|
2021-08-25 18:36:13 +00:00
|
|
|
|
2021-09-27 02:49:43 +00:00
|
|
|
if (isDelete)
|
2021-08-25 18:36:13 +00:00
|
|
|
{
|
|
|
|
if (message.System.Id != ctx.System.Id)
|
|
|
|
throw new PKError("You can only delete your own messages.");
|
2021-09-22 17:48:34 +00:00
|
|
|
|
2021-08-25 18:36:13 +00:00
|
|
|
await ctx.Rest.DeleteMessage(message.Message.Channel, message.Message.Mid);
|
2021-09-13 06:21:39 +00:00
|
|
|
|
|
|
|
if (ctx.Guild != null)
|
|
|
|
await ctx.Rest.DeleteMessage(ctx.Message);
|
2021-10-06 23:46:49 +00:00
|
|
|
else
|
|
|
|
await ctx.Rest.CreateReaction(ctx.Message.ChannelId, ctx.Message.Id, new() { Name = Emojis.Success });
|
|
|
|
|
2021-08-25 18:36:13 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (ctx.Match("author") || ctx.MatchFlag("author"))
|
|
|
|
{
|
|
|
|
var user = await _cache.GetOrFetchUser(_rest, message.Message.Sender);
|
|
|
|
var eb = new EmbedBuilder()
|
|
|
|
.Author(new(user != null ? $"{user.Username}#{user.Discriminator}" : $"Deleted user ${message.Message.Sender}", IconUrl: user != null ? user.AvatarUrl() : null))
|
|
|
|
.Description(message.Message.Sender.ToString());
|
|
|
|
|
|
|
|
await ctx.Reply(user != null ? $"{user.Mention()} ({user.Id})" : $"*(deleted user {message.Message.Sender})*", embed: eb.Build());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
await ctx.Reply(embed: await _embeds.CreateMessageInfoEmbed(message));
|
|
|
|
}
|
2021-09-27 02:49:43 +00:00
|
|
|
|
|
|
|
private async Task DeleteCommandMessage(Context ctx, ulong messageId)
|
|
|
|
{
|
2021-09-30 01:51:38 +00:00
|
|
|
var message = await _repo.GetCommandMessage(messageId);
|
2021-09-27 02:49:43 +00:00
|
|
|
if (message == null)
|
|
|
|
throw Errors.MessageNotFound(messageId);
|
|
|
|
|
|
|
|
if (message.AuthorId != ctx.Author.Id)
|
|
|
|
throw new PKError("You can only delete command messages queried by this account.");
|
|
|
|
|
|
|
|
await ctx.Rest.DeleteMessage(message.ChannelId, message.MessageId);
|
|
|
|
|
|
|
|
if (ctx.Guild != null)
|
|
|
|
await ctx.Rest.DeleteMessage(ctx.Message);
|
|
|
|
else
|
|
|
|
await ctx.Rest.CreateReaction(ctx.Message.ChannelId, ctx.Message.Id, new() { Name = Emojis.Success });
|
|
|
|
}
|
2021-05-03 10:33:30 +00:00
|
|
|
}
|
2021-08-27 15:03:47 +00:00
|
|
|
}
|