Ejemplo n.º 1
0
    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.");
        }

        var isDelete = ctx.Match("delete") || ctx.MatchFlag("delete");

        var message = await ctx.Database.Execute(c => ctx.Repository.GetMessage(c, messageId.Value));

        if (message == null)
        {
            if (isDelete)
            {
                await DeleteCommandMessage(ctx, messageId.Value);

                return;
            }

            throw Errors.MessageNotFound(messageId.Value);
        }

        var showContent        = true;
        var noShowContentError = "Message deleted or inaccessible.";

        var channel = await _rest.GetChannelOrNull(message.Message.Channel);

        if (channel == null)
        {
            showContent = false;
        }
        else if (!await ctx.CheckPermissionsInGuildChannel(channel, PermissionSet.ViewChannel))
        {
            showContent = false;
        }

        if (ctx.MatchRaw())
        {
            var discordMessage = await _rest.GetMessageOrNull(message.Message.Channel, message.Message.Mid);

            if (discordMessage == null || !showContent)
            {
                throw new PKError(noShowContentError);
            }

            var content = discordMessage.Content;
            if (content == null || content == "")
            {
                await ctx.Reply("No message content found in that message.");

                return;
            }

            await ctx.Reply($"```{content}```");

            if (Regex.IsMatch(content, "```.*```", RegexOptions.Singleline))
            {
                var stream = new MemoryStream(Encoding.UTF8.GetBytes(content));
                await ctx.Rest.CreateMessage(
                    ctx.Channel.Id,
                    new MessageRequest
                {
                    Content = $"{Emojis.Warn} Message contains codeblocks, raw source sent as an attachment."
                },
                    new[] { new MultipartFile("message.txt", stream, null) });
            }

            return;
        }

        if (isDelete)
        {
            if (!showContent)
            {
                throw new PKError(noShowContentError);
            }

            if (message.System?.Id != ctx.System.Id && message.Message.Sender != ctx.Author.Id)
            {
                throw new PKError("You can only delete your own messages.");
            }

            await ctx.Rest.DeleteMessage(message.Message.Channel, message.Message.Mid);

            if (ctx.Channel.Id == message.Message.Channel)
            {
                await ctx.Rest.DeleteMessage(ctx.Message);
            }
            else
            {
                await ctx.Rest.CreateReaction(ctx.Message.ChannelId, ctx.Message.Id,
                                              new Emoji { Name = Emojis.Success });
            }

            return;
        }

        if (ctx.Match("author") || ctx.MatchFlag("author"))
        {
            var user = await _rest.GetUser(message.Message.Sender);

            var eb = new EmbedBuilder()
                     .Author(new Embed.EmbedAuthor(
                                 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})*",
                eb.Build());

            return;
        }

        await ctx.Reply(embed : await _embeds.CreateMessageInfoEmbed(message, showContent));
    }