Beispiel #1
0
        /// <summary>
        /// Posts the archived roleplay to the guild's archive channel, if it has one.
        /// </summary>
        /// <param name="guild">The guild.</param>
        /// <param name="serverService">The server service.</param>
        /// <param name="serverSettings">The server settings service.</param>
        /// <param name="roleplay">The roleplay.</param>
        /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
        private async Task PostArchivedRoleplayAsync
        (
            SocketGuild guild,
            ServerService serverService,
            RoleplayServerSettingsService serverSettings,
            Roleplay roleplay
        )
        {
            var getServer = await serverService.GetOrRegisterServerAsync(guild);

            if (!getServer.IsSuccess)
            {
                this.Log.LogWarning("Failed to get the server for the current guild. Bug?");
                return;
            }

            var server = getServer.Entity;

            var getSettings = await serverSettings.GetOrCreateServerRoleplaySettingsAsync(server);

            if (!getSettings.IsSuccess)
            {
                this.Log.LogWarning("Failed to get the server settings for the current guild. Bug?");
                return;
            }

            var settings = getSettings.Entity;

            if (settings.ArchiveChannel is null)
            {
                return;
            }

            var archiveChannel = guild.GetTextChannel((ulong)settings.ArchiveChannel);

            if (archiveChannel is null)
            {
                this.Log.LogWarning("Failed to get the archive channel for the current guild. Deleted?");
                return;
            }

            var exporter = new PDFRoleplayExporter(guild);

            using var exportedRoleplay = await exporter.ExportAsync(roleplay);

            var eb = _feedback.CreateEmbedBase();

            eb.WithTitle($"{exportedRoleplay.Title} - Archived");
            eb.WithDescription(roleplay.Summary);
            eb.WithFooter($"Archived on {DateTime.Now:d}.");

            await archiveChannel.SendFileAsync
            (
                exportedRoleplay.Data,
                $"{exportedRoleplay.Title}.{exportedRoleplay.Format.GetFileExtension()}",
                string.Empty,
                embed : eb.Build()
            );
        }
    private static async Task <Result> PostArchivedRoleplayAsync
    (
        IServiceProvider services,
        FeedbackService feedback,
        RoleplayServerSettingsService serverSettings,
        Roleplay roleplay
    )
    {
        var channelAPI = services.GetRequiredService <IDiscordRestChannelAPI>();

        var getSettings = await serverSettings.GetOrCreateServerRoleplaySettingsAsync(roleplay.Server.DiscordID);

        if (!getSettings.IsSuccess)
        {
            return(Result.FromError(getSettings));
        }

        var settings = getSettings.Entity;

        if (settings.ArchiveChannel is null)
        {
            return(new UserError("No archive channel has been set."));
        }

        var exporter = new PDFRoleplayExporter();

        using var exportedRoleplay = await exporter.ExportAsync(services, roleplay);

        var embed = new Embed
        {
            Colour      = feedback.Theme.Secondary,
            Title       = $"{exportedRoleplay.Title} - Archived",
            Description = roleplay.GetSummaryOrDefault(),
            Footer      = new EmbedFooter($"Archived on {DateTimeOffset.UtcNow:d}.")
        };

        var fileData = new FileData
                       (
            $"{exportedRoleplay.Title}.{exportedRoleplay.Format.GetFileExtension()}",
            exportedRoleplay.Data
                       );

        var send = await channelAPI.CreateMessageAsync
                   (
            settings.ArchiveChannel.Value,
            embeds : new[] { embed },
            attachments : new List <OneOf <FileData, IPartialAttachment> > {
            fileData
        }
                   );

        return(send.IsSuccess
            ? Result.FromSuccess()
            : Result.FromError(send));
    }