Esempio n. 1
0
        /// <inheritdoc />
        public override async Task <ExportedRoleplay> ExportAsync(Roleplay roleplay)
        {
            var owner = await this.Context.Guild.GetUserAsync((ulong)roleplay.Owner.DiscordID);

            var filePath = Path.GetTempFileName();

            using (var of = File.Create(filePath))
            {
                using (var ofw = new StreamWriter(of))
                {
                    await ofw.WriteLineAsync($"Roleplay name: {roleplay.Name}");

                    await ofw.WriteLineAsync($"Owner: {owner.Username}");

                    var joinedUsers = await Task.WhenAll(roleplay.JoinedUsers.Select(p => this.Context.Guild.GetUserAsync((ulong)p.User.DiscordID)));

                    await ofw.WriteLineAsync("Participants:");

                    foreach (var participant in joinedUsers)
                    {
                        await ofw.WriteLineAsync(participant.Username);
                    }

                    await ofw.WriteLineAsync();

                    await ofw.WriteLineAsync();

                    var messages = roleplay.Messages.OrderBy(m => m.Timestamp).DistinctBy(m => m.Contents);
                    foreach (var message in messages)
                    {
                        await ofw.WriteLineAsync($"{message.AuthorNickname}: \n{message.Contents}");

                        await ofw.WriteLineAsync();
                    }
                }
            }

            var resultFile = File.OpenRead(filePath);
            var exported   = new ExportedRoleplay(roleplay.Name, ExportFormat.Plaintext, resultFile);

            return(exported);
        }
        /// <inheritdoc />
        public override async Task <ExportedRoleplay> ExportAsync(Roleplay roleplay)
        {
            // Create our document
            var pdfDoc = new Document();

            var filePath = Path.GetTempFileName();

            using (var of = File.Create(filePath))
            {
                var writer = PdfWriter.GetInstance(pdfDoc, of);
                writer.Open();
                pdfDoc.Open();

                var owner = await this.Guild.GetUserAsync((ulong)roleplay.Owner.DiscordID);

                pdfDoc.AddAuthor(owner.Nickname);
                pdfDoc.AddCreationDate();
                pdfDoc.AddCreator("DIGOS Ambassador");
                pdfDoc.AddTitle(roleplay.Name);

                var joinedUsers = await Task.WhenAll
                                  (
                    roleplay.JoinedUsers.Select
                    (
                        async p =>
                {
                    var guildUser = await this.Guild.GetUserAsync((ulong)p.User.DiscordID);
                    if (guildUser is null)
                    {
                        var messageByUser = roleplay.Messages.FirstOrDefault
                                            (
                            m => m.AuthorDiscordID == p.User.DiscordID
                                            );

                        if (messageByUser is null)
                        {
                            return($"Unknown user ({p.User.DiscordID})");
                        }

                        return(messageByUser.AuthorNickname);
                    }

                    return(guildUser.Username);
                }
                    )
                                  );

                pdfDoc.Add(CreateTitle(roleplay.Name));
                pdfDoc.Add(CreateParticipantList(joinedUsers));

                pdfDoc.NewPage();

                var messages = roleplay.Messages.OrderBy(m => m.Timestamp).DistinctBy(m => m.Contents);
                foreach (var message in messages)
                {
                    pdfDoc.Add(CreateMessage(message.AuthorNickname, message.Contents));
                }

                pdfDoc.Close();
                writer.Flush();
                writer.Close();
            }

            var resultFile = File.OpenRead(filePath);
            var exported   = new ExportedRoleplay(roleplay.Name, ExportFormat.PDF, resultFile);

            return(exported);
        }