Exemplo n.º 1
0
    public async Task EditMessage(Context ctx)
    {
        var msg = await GetMessageToEdit(ctx);

        if (ctx.System.Id != msg.System?.Id)
        {
            throw new PKError("Can't edit a message sent by a different system.");
        }

        if (!ctx.HasNext())
        {
            throw new PKSyntaxError("You need to include the message to edit in.");
        }

        var newContent = ctx.RemainderOrNull().NormalizeLineEndSpacing();

        if (newContent.Length > 2000)
        {
            throw new PKError("PluralKit cannot proxy messages over 2000 characters in length.");
        }

        var originalMsg = await _rest.GetMessageOrNull(msg.Message.Channel, msg.Message.Mid);

        if (originalMsg == null)
        {
            throw new PKError("Could not edit message.");
        }

        try
        {
            var editedMsg =
                await _webhookExecutor.EditWebhookMessage(msg.Message.Channel, msg.Message.Mid, newContent);

            if (ctx.Guild == null)
            {
                await _rest.CreateReaction(ctx.Channel.Id, ctx.Message.Id, new Emoji { Name = Emojis.Success });
            }

            if ((await ctx.BotPermissions).HasFlag(PermissionSet.ManageMessages))
            {
                await _rest.DeleteMessage(ctx.Channel.Id, ctx.Message.Id);
            }

            await _logChannel.LogMessage(ctx.MessageContext, msg.Message, ctx.Message, editedMsg,
                                         originalMsg !.Content !);
        }
        catch (NotFoundException)
        {
            throw new PKError("Could not edit message.");
        }
    }
Exemplo n.º 2
0
    public async Task EditMessage(Context ctx)
    {
        var msg = await GetMessageToEdit(ctx, EditTimeout, false);

        if (ctx.System.Id != msg.System?.Id)
        {
            throw new PKError("Can't edit a message sent by a different system.");
        }

        if (!ctx.HasNext())
        {
            throw new PKSyntaxError("You need to include the message to edit in.");
        }

        var originalMsg = await _rest.GetMessageOrNull(msg.Message.Channel, msg.Message.Mid);

        if (originalMsg == null)
        {
            throw new PKError("Could not edit message.");
        }

        // Check if we should append or prepend
        var append  = ctx.MatchFlag("append");
        var prepend = ctx.MatchFlag("prepend");

        // Grab the original message content and new message content
        var originalContent = originalMsg.Content;
        var newContent      = ctx.RemainderOrNull().NormalizeLineEndSpacing();

        // Append or prepend the new content to the original message content if needed.
        // If no flag is supplied, the new contents will completly overwrite the old contents
        // If both flags are specified. the message will be prepended AND appended
        if (append && prepend)
        {
            newContent = $"{newContent} {originalContent} {newContent}";
        }
        else if (append)
        {
            newContent = originalContent + " " + newContent;
        }
        else if (prepend)
        {
            newContent = newContent + " " + originalContent;
        }

        if (newContent.Length > 2000)
        {
            throw new PKError("PluralKit cannot proxy messages over 2000 characters in length.");
        }

        try
        {
            var editedMsg =
                await _webhookExecutor.EditWebhookMessage(msg.Message.Channel, msg.Message.Mid, newContent);

            if (ctx.Guild == null)
            {
                await _rest.CreateReaction(ctx.Channel.Id, ctx.Message.Id, new Emoji { Name = Emojis.Success });
            }

            if ((await ctx.BotPermissions).HasFlag(PermissionSet.ManageMessages))
            {
                await _rest.DeleteMessage(ctx.Channel.Id, ctx.Message.Id);
            }

            await _logChannel.LogMessage(ctx.MessageContext, msg.Message, ctx.Message, editedMsg,
                                         originalMsg !.Content !);
        }
        catch (NotFoundException)
        {
            throw new PKError("Could not edit message.");
        }
    }