public DiscordCommandResult GetUserInfo([Remainder] CachedUser user = null) { user ??= Context.User; return(Success(embed: new LocalEmbedBuilder() .WithColor(Color.Green) .WithThumbnailUrl(user.GetAvatarUrl()) .WithTitle($"Information for user {user}") .AddField("ID", user.Id) .AddField("Joined Discord", user.CreatedAt.ToString("g")) .Build())); }
public MemoryStream GetProfilePicture(CachedUser user) { using var client = new WebClient(); return(new MemoryStream(buffer: client.DownloadData(user.GetAvatarUrl()))); }
public async Task <Stream> CreateXpImageAsync(AdminCommandContext context, CachedUser target) { var output = new MemoryStream(); try { var user = await context.Database.GetOrCreateGlobalUserAsync(target.Id); var globalUsers = await context.Database.GlobalUsers.OrderByDescending(x => x.TotalXp).ToListAsync(); GuildUser guildUser = null; Guild guild = null; Stream guildIcon = null; var guildPosition = 1; if (target is CachedMember guildTarget) { guildUser = await context.Database.GetOrCreateGuildUserAsync(target.Id, guildTarget.Guild.Id); guild = await context.Database.GetOrCreateGuildAsync(guildTarget.Guild.Id); guildIcon = await _http.GetStreamAsync(guildTarget.Guild.GetIconUrl()); var guildUsers = await context.Database.GuildUsers.OrderByDescending(x => x.TotalXp).ToListAsync(); guildPosition = guildUsers.IndexOf(guildUser) + 1; } var guildOffset = guild?.Settings.HasFlag(GuildSettings.XpTracking) == true ? 45 : 0; using var background = Image.Load <Rgba32>(new FileStream("./Data/Images/01.png", FileMode.Open)); using var avatarStream = await _http.GetStreamAsync(target.GetAvatarUrl()); using var avatar = Image.Load <Rgba32>(avatarStream); using var canvas = new Image <Rgba32>(Configuration.Default, 450, 300); using var currentLevelStream = await _http.GetStreamAsync(EmojiTools.GetUrl(GetLevelEmoji(user))); using var currentLevel = Image.Load <Rgba32>(currentLevelStream); currentLevel.Mutate(x => x.Resize(45 / currentLevel.Height * currentLevel.Width, 45)); Stream currentGuildLevelStream = null; if (guildUser is { }) { currentGuildLevelStream = await _http.GetStreamAsync(EmojiTools.GetUrl(GetLevelEmoji(guildUser))); } canvas.Mutate(cnvs => { var sb = new StringBuilder(); foreach (var c in target.Name) { if (char.IsLetterOrDigit(c)) { sb.Append(c); } } sb.Append($"#{target.Discriminator}"); var userFontSize = 20; var box = TextMeasurer.MeasureBounds(sb.ToString(), new RendererOptions(ImageTools.Fonts.TF2(userFontSize))); while (box.Width > 420 || box.Height > 20) { box = TextMeasurer.MeasureBounds(sb.ToString(), new RendererOptions(ImageTools.Fonts.TF2(--userFontSize))); } // Draw XP image (background) cnvs.DrawImage(background, PixelColorBlendingMode.Normal, 1); // Draw outer bounding box cnvs.FillPolygon(ImageTools.Colors.DarkButTransparent, new PointF(10, 190 - guildOffset), new PointF(440, 190 - guildOffset), /* * new PointF(440, 210 - guildReduction), * new PointF(10, 210 - guildReduction), * new PointF(440, 210 - guildReduction), */ new PointF(440, 290 - guildOffset), new PointF(10, 290 - guildOffset)); // Draw avatar bounding box cnvs.FillPolygon(ImageTools.Colors.Blurple, new PointF(385, 215 - guildOffset), new PointF(435, 215 - guildOffset), new PointF(435, 265 - guildOffset), new PointF(385, 265 - guildOffset)); // Draw avatar cnvs.DrawImage(avatar.Clone(x => x.Resize(50, 50)), new Point(385, 215 - guildOffset), PixelColorBlendingMode.Normal, PixelAlphaCompositionMode.SrcOver, 1); // Draw avatar bounding box outline cnvs.DrawPolygon(Rgba32.WhiteSmoke, 2, new PointF(386, 216 - guildOffset), new PointF(434, 216 - guildOffset), new PointF(434, 264 - guildOffset), new PointF(386, 264 - guildOffset)); // Write username cnvs.DrawText(sb.ToString().Trim(), ImageTools.Fonts.TF2(userFontSize), Rgba32.WhiteSmoke, new PointF(15, 195 - guildOffset)); /* Write user info * if (!string.IsNullOrWhiteSpace(global.Info)) * { * cnvs.DrawText(new TextGraphicsOptions { WrapTextWidth = 350 }, * global.Info, * ImageTools.Fonts.TF2Secondary(13), * Rgba32.WhiteSmoke, * new PointF(15, 215 - guildOffset)); * }*/ // Draw inner box (XP bar outline) cnvs.FillPolygon(ImageTools.Colors.LessDark, new PointF(75, 272 - guildOffset), new PointF(435, 272 - guildOffset), new PointF(435, 285 - guildOffset), new PointF(75, 285 - guildOffset)); // Draw current XP bar cnvs.FillPolygon(ImageTools.Colors.XpBar, new PointF(77, 274 - guildOffset), new PointF(356F * ((float)user.CurrentLevelXp / user.NextLevelXp) + 77, 274 - guildOffset), new PointF(356F * ((float)user.CurrentLevelXp / user.NextLevelXp) + 77, 283 - guildOffset), new PointF(77, 283 - guildOffset)); // Write current level text cnvs.DrawText(new TextGraphicsOptions { HorizontalAlignment = HorizontalAlignment.Center }, $"Tier {user.Tier}, Level {user.Level} ({user.Grade} Grade)", ImageTools.Fonts.TF2(13), ImageTools.Colors.GetGradeColor(user.Grade), new PointF(255, 259 - guildOffset)); // Write current XP text cnvs.DrawText(new TextGraphicsOptions { HorizontalAlignment = HorizontalAlignment.Center }, $"{user.TotalXp} / {user.NextLevelTotalXp} XP", ImageTools.Fonts.TF2(13), Rgba32.WhiteSmoke, new PointF(255, 273 - guildOffset)); // Draw current level cnvs.DrawImage( currentLevel.Clone(x => x.Resize(45 / currentLevel.Height * currentLevel.Width, 45)), ImageTools.Justify(new Point(45, 285 - guildOffset), currentLevel, Justification.BottomCenter), PixelColorBlendingMode.Normal, PixelAlphaCompositionMode.SrcOver, 1); // Write current global position cnvs.DrawText(new TextGraphicsOptions { HorizontalAlignment = HorizontalAlignment.Center }, $"Global position #{globalUsers.IndexOf(user) + 1}", ImageTools.Fonts.TF2(11), Rgba32.WhiteSmoke, new PointF(255, 248 - guildOffset)); if (guildUser is { } && guildOffset > 0) { // Draw guild bounding box // 270 cnvs.FillPolygon(ImageTools.Colors.DarkButTransparent, new PointF(10, 250), new PointF(440, 250), new PointF(440, 295), new PointF(10, 295)); // Draw guild XP bar outline cnvs.FillPolygon(ImageTools.Colors.LessDark, new PointF(75, 277), new PointF(435, 277), new PointF(435, 290), new PointF(75, 290)); // Draw guild XP bar cnvs.FillPolygon(ImageTools.Colors.XpBar, new PointF(77, 279), new PointF(356F * ((float)guildUser.CurrentLevelXp / guildUser.NextLevelXp) + 77, 279), new PointF(356F * ((float)guildUser.CurrentLevelXp / guildUser.NextLevelXp) + 77, 288), new PointF(77, 288)); // Write current guild level text cnvs.DrawText(new TextGraphicsOptions { HorizontalAlignment = HorizontalAlignment.Center }, $"Tier {guildUser.Tier}, Level {guildUser.Level} ({guildUser.Grade} Grade)", ImageTools.Fonts.TF2(13), ImageTools.Colors.GetGradeColor(guildUser.Grade), new PointF(255, 264)); // Write current guild XP text cnvs.DrawText(new TextGraphicsOptions { HorizontalAlignment = HorizontalAlignment.Center }, $"{guildUser.TotalXp} / {guildUser.NextLevelTotalXp} XP", ImageTools.Fonts.TF2(13), Rgba32.WhiteSmoke, new PointF(255, 278)); // using (var guildLevel = Image.Load(guildLevelStream)) using var guildImage = Image.Load <Rgba32>(guildIcon); using var currentGuildLevel = Image.Load <Rgba32>(currentGuildLevelStream); currentGuildLevel.Mutate(x => x.Resize(40 / currentGuildLevel.Height * currentGuildLevel.Width, 40)); // Draw current guild level cnvs.DrawImage(currentGuildLevel, ImageTools.Justify(new Point(45, 292), currentGuildLevel, Justification.BottomCenter), PixelColorBlendingMode.Normal, PixelAlphaCompositionMode.SrcOver, 1); // Draw current guild icon guildImage.Mutate(img => img.Resize(18, 18)); cnvs.DrawImage(guildImage, ImageTools.Justify(new Point(435, 255), guildImage, Justification.TopRight), PixelColorBlendingMode.Normal, PixelAlphaCompositionMode.SrcOver, 1f); // Write current guild position cnvs.DrawText(new TextGraphicsOptions { HorizontalAlignment = HorizontalAlignment.Center }, $"Guild position #{guildPosition}", ImageTools.Fonts.TF2(11), Rgba32.WhiteSmoke, new PointF(255, 253)); } });
private async Task AddAvatarAndUsernameAsync(MagickImage image, CachedUser user, ProfileInfo?profileInfo = null) { await using var avatarStream = await _httpClient.GetStreamAsync(user.GetAvatarUrl()); using var avatarImage = new MagickImage(avatarStream); avatarImage.Resize(new MagickGeometry { Width = 100, Height = 100 }); using var avatarLayer = new MagickImage(MagickColors.Transparent, 100, 100); avatarLayer.Draw(new Drawables().RoundRectangle(0, 0, avatarLayer.Width, avatarLayer.Height, 15, 15) .FillColor(MagickColors.White)); avatarLayer.Composite(avatarImage, CompositeOperator.Atop); image.Draw(new DrawableComposite(30, 120, CompositeOperator.Over, avatarLayer)); var usernameSettings = new MagickReadSettings { BackgroundColor = MagickColors.Transparent, FillColor = MagickColors.White, Font = _meiryoFontPath, FontWeight = FontWeight.Bold, Width = 250, Height = 50 }; using var usernameImage = new MagickImage($"caption:{user}", usernameSettings); image.Draw(new DrawableComposite(150, 130, CompositeOperator.Over, usernameImage)); if (profileInfo is null) { return; } var badgeSettings = new MagickReadSettings { BackgroundColor = MagickColors.Transparent, FillColor = profileInfo.Color, Font = _arialFontPath, FontPointsize = 15 }; if (profileInfo.PatreonTier == 0 && user.Id != Credentials.MasterId) { return; } var xBadge = user.Id == Credentials.MasterId ? "Master" : "Supporter"; if (profileInfo.Badges is null) { profileInfo.Badges = new List <string>() { xBadge } } ; else if (user.Id != Credentials.MasterId) { if (profileInfo.PatreonTier < PatreonService.ProfileThirdBadgeTier && profileInfo.Badges.Count > 2) { profileInfo.Badges.RemoveAt(2); } if (profileInfo.PatreonTier < PatreonService.ProfileSecondBadgeTier && profileInfo.Badges.Count > 1) { profileInfo.Badges.RemoveAt(1); } if (profileInfo.PatreonTier < PatreonService.ProfileFirstBadgeTier && profileInfo.Badges.Count != 0) { profileInfo.Badges.RemoveAt(0); } } if (profileInfo.Badges.Count == 0) { profileInfo.Badges.Add(xBadge); } var x = 150.0; foreach (var badge in profileInfo.Badges) { AddBadge(image, profileInfo, badgeSettings, badge, ref x); } }