|
| 1 | +using GitcgNetCord.MainApp.Commands.Autocompletes; |
| 2 | +using GitcgNetCord.MainApp.Extensions; |
| 3 | +using GitcgNetCord.MainApp.Infrastructure.HoyolabServices; |
| 4 | +using HoyolabHttpClient; |
| 5 | +using NetCord; |
| 6 | +using NetCord.Rest; |
| 7 | +using NetCord.Services.ApplicationCommands; |
| 8 | +using System.Collections.Immutable; |
| 9 | +using System.Text; |
| 10 | +using System.Text.RegularExpressions; |
| 11 | +using Color = System.Drawing.Color; |
| 12 | + |
| 13 | +namespace GitcgNetCord.MainApp.Commands.Slash; |
| 14 | + |
| 15 | +public partial class TcgCardSlashCommand |
| 16 | +{ |
| 17 | + public static async Task ExecuteAsync( |
| 18 | + HoyolabHttpClientService hoyolab, |
| 19 | + IServiceProvider serviceProvider, |
| 20 | + ApplicationCommandContext context, |
| 21 | + [ |
| 22 | + SlashCommandParameter( |
| 23 | + Description = "Card ID.", |
| 24 | + AutocompleteProviderType = typeof(CardAutocompleteProvider) |
| 25 | + ) |
| 26 | + ] |
| 27 | + int cardId, |
| 28 | + [ |
| 29 | + SlashCommandParameter( |
| 30 | + Description = "Language. Default: `en-us`.", |
| 31 | + AutocompleteProviderType = typeof(LanguageAutocompleteProvider) |
| 32 | + ) |
| 33 | + ] |
| 34 | + string lang = "en-us" |
| 35 | + ) |
| 36 | + { |
| 37 | + var cardActionService = serviceProvider |
| 38 | + .GetRequiredService<HoyolabCardActionService>(); |
| 39 | + |
| 40 | + await context.Interaction.SendResponseAsync( |
| 41 | + callback: InteractionCallback.DeferredMessage() |
| 42 | + ); |
| 43 | + |
| 44 | + HoyolabHttpClient.Responses.ActionSkill.Data data; |
| 45 | + |
| 46 | + try |
| 47 | + { |
| 48 | + data = await hoyolab.GetActionSkillAsync(cardId, lang); |
| 49 | + } |
| 50 | + catch (Exception e) |
| 51 | + { |
| 52 | + await context.Interaction.ModifyResponseAsync(message => |
| 53 | + { |
| 54 | + //message.WithFlags(MessageFlags.IsComponentsV2); |
| 55 | + message.AddEmbeds(new EmbedProperties() |
| 56 | + .WithTitle("Error") |
| 57 | + .WithDescription(e.Message) |
| 58 | + .WithColor(Color.Red.ToNetCordColor()) |
| 59 | + ); |
| 60 | + }); |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + var appEmojis = await context.Client.Rest |
| 65 | + .GetApplicationEmojisAsync(context.Client.Id); |
| 66 | + var emojis = appEmojis.ToImmutableDictionary(x => x.Name); |
| 67 | + |
| 68 | + var cardActions = await cardActionService |
| 69 | + .GetCardActionsAsync(lang); |
| 70 | + var action = cardActions.Actions |
| 71 | + .First(x => x.Basic.ItemId == cardId); |
| 72 | + |
| 73 | + await context.Interaction.ModifyResponseAsync(message => |
| 74 | + { |
| 75 | + message.WithFlags(MessageFlags.IsComponentsV2); |
| 76 | + message.AddComponents([new ComponentContainerProperties() |
| 77 | + .WithAccentColor(Color.Purple.ToNetCordColor()) |
| 78 | + .AddComponents( |
| 79 | + new TextDisplayProperties( |
| 80 | + $""" |
| 81 | + ## {GetName(cardId)} |
| 82 | + """ |
| 83 | + ), |
| 84 | + new MediaGalleryProperties().AddItems( |
| 85 | + new MediaGalleryItemProperties( |
| 86 | + new ComponentMediaProperties(action.Basic.Icon)) |
| 87 | + ), |
| 88 | + new TextDisplayProperties( |
| 89 | + $"{GetDesc(data.Desc)}" |
| 90 | + ) |
| 91 | + ) |
| 92 | + ]); |
| 93 | + }); |
| 94 | + |
| 95 | + return; |
| 96 | + |
| 97 | + string GetName(int cardId) |
| 98 | + { |
| 99 | + var builder = new StringBuilder(); |
| 100 | + if (data.Cost2Raw > 0) |
| 101 | + { |
| 102 | + builder.Append($"<img src='{data.Cost2TypeIcon}' /> - "); |
| 103 | + } |
| 104 | + builder.Append($"{data.Cost1Raw} "); |
| 105 | + builder.Append($"<img src='{data.Cost1TypeIcon}' /> "); |
| 106 | + builder.Append($"- {action.Basic.Name} "); |
| 107 | + |
| 108 | + return GetDesc(builder.ToString()); |
| 109 | + } |
| 110 | + |
| 111 | + string GetDesc(string desc) |
| 112 | + { |
| 113 | + var result = desc; |
| 114 | + |
| 115 | + result = result.Replace("\\n", "\n"); |
| 116 | + |
| 117 | + result = ImgTagRegex().Replace(result, match => |
| 118 | + { |
| 119 | + var url = match.Groups["url"].Value; |
| 120 | + |
| 121 | + ApplicationEmoji? emoji = null; |
| 122 | + |
| 123 | + var availableEmojis = HoyolabSharedUtils.UrlToEmojis |
| 124 | + .TryGetValue(url, out var emojiKey) |
| 125 | + && emojis.TryGetValue(emojiKey, out emoji); |
| 126 | + |
| 127 | + if (availableEmojis) return emoji!.ToString(); |
| 128 | + |
| 129 | + return match.Value; // leave unchanged if not found |
| 130 | + }); |
| 131 | + |
| 132 | + while (ColorTagRegex().IsMatch(result)) |
| 133 | + { |
| 134 | + result = ColorTagRegex().Replace(result, "**$1**"); |
| 135 | + } |
| 136 | + |
| 137 | + return result; |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + [ |
| 142 | + GeneratedRegex( |
| 143 | + pattern: @"<color\b[^>]*>(.*?)</color>", |
| 144 | + options: RegexOptions.IgnoreCase | RegexOptions.Singleline |
| 145 | + ) |
| 146 | + ] |
| 147 | + private static partial Regex ColorTagRegex(); |
| 148 | + |
| 149 | + [ |
| 150 | + GeneratedRegex( |
| 151 | + pattern: @"<img\s+src=['""](?<url>[^'""]+)['""][^>]*\/?>", |
| 152 | + options: RegexOptions.IgnoreCase, cultureName: "en-US" |
| 153 | + ) |
| 154 | + ] |
| 155 | + private static partial Regex ImgTagRegex(); |
| 156 | +} |
0 commit comments