Пример #1
0
    /// <summary>
    /// Refresh the message
    /// </summary>
    /// <param name="configurationId">Id of the configuration</param>
    /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
    public async Task RefreshMessageAsync(long configurationId)
    {
        using (var dbFactory = RepositoryFactory.CreateInstance())
        {
            var currentRaidPoints = dbFactory.GetRepository <RaidCurrentUserPointsRepository>()
                                    .GetQuery()
                                    .Select(obj => obj);

            var appointment = dbFactory.GetRepository <RaidAppointmentRepository>()
                              .GetQuery()
                              .Where(obj => obj.ConfigurationId == configurationId &&
                                     obj.IsCommitted == false)
                              .OrderByDescending(obj => obj.TimeStamp)
                              .Select(obj => new
            {
                obj.TimeStamp,
                ChannelId = obj.RaidDayConfiguration.DiscordChannelId,
                MessageId = obj.RaidDayConfiguration.DiscordMessageId,
                obj.RaidDayTemplate.Thumbnail,
                obj.RaidDayTemplate.Title,
                obj.RaidDayTemplate.Description,
                obj.GroupCount,

                ExperienceLevels = obj.RaidDayTemplate
                                   .RaidExperienceAssignments
                                   .Select(obj2 => new
                {
                    RaidExperienceLevelId = obj2.RaidExperienceLevel.Id,
                    obj2.RaidExperienceLevel.DiscordEmoji,
                    obj2.RaidExperienceLevel.Description,
                    obj2.RaidExperienceLevel.Rank,
                    obj2.Count
                })
                                   .ToList(),

                Registrations = obj.RaidRegistrations
                                .Select(obj3 => new
                {
                    UserId = obj3.User
                             .DiscordAccounts
                             .Select(obj4 => obj4.Id)
                             .FirstOrDefault(),
                    Points = currentRaidPoints.Where(obj4 => obj4.UserId == obj3.UserId)
                             .Select(obj4 => obj4.Points).FirstOrDefault(),
                    obj3.LineupExperienceLevelId,
                    ExperienceLevelId           = (int?)obj3.User.RaidExperienceLevel.Id,
                    ExperienceLevelDiscordEmoji = (ulong?)obj3.User.RaidExperienceLevel.DiscordEmoji,
                    Roles = obj3.RaidRegistrationRoleAssignments
                            .Select(obj4 => new
                    {
                        MainRoleEmoji = obj4.MainRaidRole.DiscordEmojiId,
                        SubRoleEmoji  = (ulong?)obj4.SubRaidRole.DiscordEmojiId
                    })
                            .ToList()
                })
                                .ToList()
            })
                              .FirstOrDefault();

            if (appointment != null)
            {
                var builder = new DiscordEmbedBuilder();

                var channel = await _client.GetChannelAsync(appointment.ChannelId)
                              .ConfigureAwait(false);

                if (channel != null)
                {
                    var message = await channel.GetMessageAsync(appointment.MessageId)
                                  .ConfigureAwait(false);

                    if (message != null)
                    {
                        var fieldBuilder = new StringBuilder();

                        int fieldCounter;

                        // Building the message
                        foreach (var slot in appointment.ExperienceLevels
                                 .OrderBy(obj => obj.Rank))
                        {
                            fieldCounter = 1;

                            var registrations = appointment.Registrations
                                                .Where(obj => obj.LineupExperienceLevelId == slot.RaidExperienceLevelId)
                                                .OrderByDescending(obj => obj.Points)
                                                .ToList();

                            foreach (var registration in registrations)
                            {
                                var discordUser = await _client.GetUserAsync(registration.UserId)
                                                  .ConfigureAwait(false);

                                var lineBuilder = new StringBuilder();
                                lineBuilder.Append(" > ");

                                if (registration.Roles.Count > 0)
                                {
                                    var first = true;

                                    foreach (var role in registration.Roles)
                                    {
                                        if (first == false)
                                        {
                                            lineBuilder.Append(", ");
                                        }
                                        else
                                        {
                                            first = false;
                                        }

                                        lineBuilder.Append(DiscordEmojiService.GetGuildEmoji(_client, role.MainRoleEmoji));

                                        if (role.SubRoleEmoji != null)
                                        {
                                            lineBuilder.Append(DiscordEmojiService.GetGuildEmoji(_client, role.SubRoleEmoji.Value));
                                        }
                                    }
                                }
                                else
                                {
                                    lineBuilder.Append(DiscordEmojiService.GetQuestionMarkEmoji(_client));
                                }

                                lineBuilder.Append($" {discordUser.Mention}");

                                if (registration.LineupExperienceLevelId != registration.ExperienceLevelId &&
                                    registration.ExperienceLevelDiscordEmoji != null)
                                {
                                    lineBuilder.Append(' ');
                                    lineBuilder.Append(DiscordEmojiService.GetGuildEmoji(_client, registration.ExperienceLevelDiscordEmoji.Value));
                                }

                                lineBuilder.Append('\n');

                                if (lineBuilder.Length + fieldBuilder.Length > 1024)
                                {
                                    builder.AddField($"{DiscordEmojiService.GetGuildEmoji(_client, slot.DiscordEmoji)} {slot.Description} ({registrations.Count}/{slot.Count * appointment.GroupCount}) #{fieldCounter}", fieldBuilder.ToString());
                                    fieldBuilder = new StringBuilder();
                                    fieldCounter++;
                                }

                                fieldBuilder.Append(lineBuilder);
                            }

                            fieldBuilder.Append('\u200B');

                            var fieldName = $"{DiscordEmojiService.GetGuildEmoji(_client, slot.DiscordEmoji)} {slot.Description} ({registrations.Count}/{slot.Count * appointment.GroupCount})";
                            if (fieldCounter > 1)
                            {
                                fieldName = $"{fieldName} #{fieldCounter}";
                            }

                            builder.AddField(fieldName, fieldBuilder.ToString());

                            fieldBuilder.Clear();
                        }

                        fieldCounter = 1;

                        foreach (var entry in appointment.Registrations
                                 .Where(obj => obj.LineupExperienceLevelId == null)
                                 .OrderByDescending(obj => obj.Points))
                        {
                            var discordUser = await _client.GetUserAsync(entry.UserId)
                                              .ConfigureAwait(false);

                            var lineBuilder = new StringBuilder();
                            lineBuilder.Append(" > ");

                            if (entry.Roles?.Count > 0)
                            {
                                var first = true;

                                foreach (var role in entry.Roles)
                                {
                                    if (first == false)
                                    {
                                        lineBuilder.Append(", ");
                                    }
                                    else
                                    {
                                        first = false;
                                    }

                                    lineBuilder.Append(DiscordEmojiService.GetGuildEmoji(_client, role.MainRoleEmoji));

                                    if (role.SubRoleEmoji != null)
                                    {
                                        lineBuilder.Append(DiscordEmojiService.GetGuildEmoji(_client, role.SubRoleEmoji.Value));
                                    }
                                }
                            }
                            else
                            {
                                lineBuilder.Append(DiscordEmojiService.GetQuestionMarkEmoji(_client));
                            }

                            lineBuilder.AppendLine($" {discordUser.Mention} {(entry.ExperienceLevelDiscordEmoji != null ? DiscordEmojiService.GetGuildEmoji(_client, entry.ExperienceLevelDiscordEmoji.Value) : null)}");

                            if (lineBuilder.Length + fieldBuilder.Length > 1024)
                            {
                                builder.AddField($"{LocalizationGroup.GetText("SubstitutesBench", "Substitutes bench")} #{fieldCounter}", fieldBuilder.ToString());
                                fieldBuilder = new StringBuilder();
                                fieldCounter++;
                            }

                            fieldBuilder.Append(lineBuilder);
                        }

                        fieldBuilder.Append('\u200B');

                        var substitutesBenchFieldName = LocalizationGroup.GetText("SubstitutesBench", "Substitutes bench");
                        if (fieldCounter > 1)
                        {
                            substitutesBenchFieldName = $"{substitutesBenchFieldName} #{fieldCounter}";
                        }

                        builder.AddField(substitutesBenchFieldName, fieldBuilder.ToString());

                        builder.WithTitle($"{appointment.Title} - {appointment.TimeStamp.ToString("g", LocalizationGroup.CultureInfo)}");
                        builder.WithDescription(appointment.Description);
                        builder.WithThumbnail(appointment.Thumbnail);
                        builder.WithColor(DiscordColor.Green);
                        builder.WithFooter("Scruffy", "https://cdn.discordapp.com/app-icons/838381119585648650/823930922cbe1e5a9fa8552ed4b2a392.png?size=64");
                        builder.WithTimestamp(DateTime.Now);

                        await message.ModifyAsync(null, builder.Build())
                        .ConfigureAwait(false);
                    }
                }
            }
        }
    }