Beispiel #1
0
        public async Task Trophy(string name)
        {
            // Find the trophy with this name.
            Trophy trophy = await Global.TrophyRegistry.GetTrophyByNameAsync(name);

            // If no such trophy exists, return an error.

            if (trophy is null)
            {
                await BotUtils.ReplyAsync_Error(Context, "No such trophy exists.");

                return;
            }

            // Show trophy information.

            double completion_rate = await Global.TrophyRegistry.GetCompletionRateAsync(trophy);

            bool hide_description = trophy.Flags.HasFlag(TrophyFlags.Hidden) && completion_rate <= 0.0;

            string embed_title       = string.Format("{0} {1} ({2:0.#}%)", trophy.GetIcon(), trophy.GetName(), completion_rate);
            string embed_description = string.Format("_{0}_", hide_description ? OurFoodChain.Trophies.Trophy.HIDDEN_TROPHY_DESCRIPTION : trophy.GetDescription());
            long   times_unlocked    = await Global.TrophyRegistry.GetTimesUnlockedAsync(trophy);

            embed_description += string.Format("\n\nThis trophy has been earned by **{0}** user{1} ({2:0.#}%).",
                                               times_unlocked,
                                               times_unlocked == 1 ? "" : "s",
                                               completion_rate);

            EmbedBuilder embed = new EmbedBuilder();

            embed.WithTitle(embed_title);
            embed.WithDescription(embed_description);
            embed.WithColor(new Color(255, 204, 77));

            // Show first/latest earners.

            TrophyUser[] earners     = (await Global.TrophyRegistry.GetUsersUnlockedAsync(trophy)).OrderBy(x => x.EarnedTimestamp).ToArray();
            string       date_format = "MMMM dd, yyyy";

            if (!(Context.Guild is null))
            {
                foreach (TrophyUser trophy_user in earners)
                {
                    IUser user = await Context.Guild.GetUserAsync(trophy_user.UserId);

                    if (!(user is null))
                    {
                        embed.AddField("First earned", string.Format("**{0}** ({1})", user.Username, trophy_user.EarnedDate.ToString(date_format)), inline: true);

                        break;
                    }
                }

                foreach (TrophyUser trophy_user in earners.Reverse())
                {
                    IUser user = await Context.Guild.GetUserAsync(trophy_user.UserId);

                    if (!(user is null))
                    {
                        embed.AddField("Latest earned", string.Format("**{0}** ({1})", user.Username, trophy_user.EarnedDate.ToString(date_format)), inline: true);

                        break;
                    }
                }
            }

            await ReplyAsync("", false, embed.Build());
        }