public async Task Trophies(IUser user = null) { if (user is null) { user = Context.User; } UnlockedTrophyInfo[] unlocked = await Global.TrophyRegistry.GetUnlockedTrophiesAsync(user.Id); Array.Sort(unlocked, (x, y) => x.timestamp.CompareTo(y.timestamp)); EmbedBuilder embed = new EmbedBuilder(); embed.WithTitle(string.Format("{0}'s Trophies ({1:0.#}%)", user.Username, await Global.TrophyRegistry.GetUserCompletionRateAsync(user.Id))); embed.WithColor(new Color(255, 204, 77)); StringBuilder description_builder = new StringBuilder(); description_builder.AppendLine(string.Format("See a list of all available trophies with `{0}trophylist`.", OurFoodChainBot.Instance.Config.Prefix)); foreach (UnlockedTrophyInfo info in unlocked) { Trophy trophy = await Global.TrophyRegistry.GetTrophyByIdentifierAsync(info.identifier); if (trophy is null) { continue; } description_builder.AppendLine(string.Format("{0} **{1}** - Earned {2} ({3:0.#}%)", trophy.GetIcon(), trophy.GetName(), DateUtils.TimestampToShortDateString(info.timestamp), await Global.TrophyRegistry.GetCompletionRateAsync(trophy) )); } embed.WithDescription(description_builder.ToString()); await ReplyAsync("", false, embed.Build()); }
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()); }