diff --git a/ASFEnhance/ASFEnhance.cs b/ASFEnhance/ASFEnhance.cs index 4dd1beda..0750ea58 100644 --- a/ASFEnhance/ASFEnhance.cs +++ b/ASFEnhance/ASFEnhance.cs @@ -13,7 +13,7 @@ namespace ASFEnhance; [Export(typeof(IPlugin))] -internal sealed class ASFEnhance : IASF, IBotCommand2, IBotFriendRequest, IGitHubPluginUpdates +internal sealed class ASFEnhance : IASF, IBotCommand2, IBotFriendRequest, IBotModules, IGitHubPluginUpdates { public string Name => nameof(ASFEnhance); @@ -1243,4 +1243,25 @@ public Task OnBotFriendRequest(Bot bot, ulong steamId) return Task.FromResult(result); } + + /// + public Task OnBotInitModules(Bot bot, IReadOnlyDictionary? additionalConfigProperties) + { + if (additionalConfigProperties != null) + { + foreach (var (configProperty, configValue) in additionalConfigProperties) + { + if (configProperty == "UserCountry" && configValue.ValueKind == JsonValueKind.String) + { + var countryCode = configValue.GetString(); + if (!string.IsNullOrEmpty(countryCode)) + { + CustomUserCountry.TryAdd(bot, countryCode.ToUpperInvariant()); + } + break; + } + } + } + return Task.CompletedTask; + } } diff --git a/ASFEnhance/Cart/WebRequest.cs b/ASFEnhance/Cart/WebRequest.cs index 092cbd66..be6b518d 100644 --- a/ASFEnhance/Cart/WebRequest.cs +++ b/ASFEnhance/Cart/WebRequest.cs @@ -19,7 +19,7 @@ internal static class WebRequest internal static async Task GetAccountCart(this Bot bot) { var token = bot.AccessToken ?? throw new AccessTokenNullException(); - var userCountry = WalletCurrency2UserCountry(bot.WalletCurrency); + var userCountry = bot.GetUserCountryCode(); var request = new Uri(SteamApiURL, $"/IAccountCartService/GetCart/v1/?access_token={token}&user_country={userCountry}"); var response = await bot.ArchiWebHandler.UrlGetToJsonObjectWithSession>(request, referer: SteamStoreURL).ConfigureAwait(false); return response?.Content?.Response; @@ -51,7 +51,7 @@ internal static class WebRequest var payload = new AddItemsToCartRequest { Items = items.ToList(), - UserCountry = WalletCurrency2UserCountry(bot.WalletCurrency), + UserCountry = bot.GetUserCountryCode(), Navdata = new NavdataData { Domain = "store.steampowered.com", @@ -97,7 +97,7 @@ internal static class WebRequest var payload = new AddItemsToCartRequest { Items = items, - UserCountry = WalletCurrency2UserCountry(bot.WalletCurrency), + UserCountry = bot.GetUserCountryCode(), Navdata = new NavdataData { Domain = "store.steampowered.com", @@ -146,7 +146,7 @@ internal static class WebRequest var payload = new ModifyLineItemRequest { LineItemId = lineItemId, - UserCountry = WalletCurrency2UserCountry(bot.WalletCurrency), + UserCountry = bot.GetUserCountryCode(), GiftInfo = giftInfo, Flags = new FlagsData { @@ -179,7 +179,7 @@ internal static class WebRequest var payload = new ModifyLineItemRequest { LineItemId = lineItemId, - UserCountry = WalletCurrency2UserCountry(bot.WalletCurrency), + UserCountry = bot.GetUserCountryCode(), }; var json = payload.ToJsonText(); diff --git a/ASFEnhance/Utils.cs b/ASFEnhance/Utils.cs index 5601b043..8c0b01ef 100644 --- a/ASFEnhance/Utils.cs +++ b/ASFEnhance/Utils.cs @@ -7,6 +7,7 @@ using ASFEnhance.Data.Plugin; using ProtoBuf; using SteamKit2; +using System.Collections.Concurrent; using System.Collections.ObjectModel; using System.Reflection; using System.Text; @@ -21,6 +22,8 @@ internal static class Utils /// internal static PluginConfig Config { get; set; } = new(); + internal static ConcurrentDictionary CustomUserCountry { get; } = []; + /// /// 格式化返回文本 /// @@ -65,12 +68,6 @@ internal static string FormatBotResponse(this Bot bot, string message, params ob return bot.FormatBotResponse(string.Format(message, args)); } - [Obsolete("请使用 AppendLine")] - internal static StringBuilder AppendLineFormat(this StringBuilder sb, string format) - { - return sb.AppendLine(format); - } - internal static StringBuilder AppendLineFormat(this StringBuilder sb, string format, params object?[] args) { return sb.AppendLine(string.Format(format, args)); @@ -392,56 +389,64 @@ internal static string GetGifteeProfile(ulong accountId) /// /// 货币代码转国家代码 /// - /// + /// /// - internal static string WalletCurrency2UserCountry(ECurrencyCode currencyCode) => currencyCode switch + internal static string GetUserCountryCode(this Bot bot) { - ECurrencyCode.USD => "US", - ECurrencyCode.GBP => "GB", - ECurrencyCode.EUR => "EU", - ECurrencyCode.CHF => "CH", - ECurrencyCode.RUB => "RU", - ECurrencyCode.PLN => "PL", - ECurrencyCode.BRL => "BR", - ECurrencyCode.JPY => "JP", - ECurrencyCode.NOK => "NO", - ECurrencyCode.IDR => "ID", - ECurrencyCode.MYR => "MY", - ECurrencyCode.PHP => "PH", - ECurrencyCode.SGD => "SG", - ECurrencyCode.THB => "TH", - ECurrencyCode.VND => "VN", - ECurrencyCode.KRW => "KR", - ECurrencyCode.TRY => "TR", - ECurrencyCode.UAH => "UA", - ECurrencyCode.MXN => "MX", - ECurrencyCode.CAD => "CA", - ECurrencyCode.AUD => "CX", - ECurrencyCode.NZD => "CK", - ECurrencyCode.CNY => "CN", - ECurrencyCode.INR => "IN", - ECurrencyCode.CLP => "CL", - ECurrencyCode.PEN => "PE", - ECurrencyCode.COP => "CO", - ECurrencyCode.ZAR => "ZA", - ECurrencyCode.HKD => "HK", - ECurrencyCode.TWD => "TW", - ECurrencyCode.SAR => "SA", - ECurrencyCode.AED => "AE", - ECurrencyCode.ARS => "AR", - ECurrencyCode.ILS => "IL", - ECurrencyCode.BYN => "BY", - ECurrencyCode.KZT => "KZ", - ECurrencyCode.KWD => "KW", - ECurrencyCode.QAR => "QA", - ECurrencyCode.CRC => "CT", - ECurrencyCode.UYU => "UY", - ECurrencyCode.BGN => "BG", - ECurrencyCode.HRK => "HR", - ECurrencyCode.CZK => "CZ", - ECurrencyCode.DKK => "DK", - ECurrencyCode.HUF => "HU", - ECurrencyCode.RON => "RO", - _ => Langs.CountryCode, - }; + if (CustomUserCountry.TryGetValue(bot, out var code) && !string.IsNullOrEmpty(code)) + { + return code; + } + + return bot.WalletCurrency switch + { + ECurrencyCode.USD => "US", + ECurrencyCode.GBP => "GB", + ECurrencyCode.EUR => "EU", + ECurrencyCode.CHF => "CH", + ECurrencyCode.RUB => "RU", + ECurrencyCode.PLN => "PL", + ECurrencyCode.BRL => "BR", + ECurrencyCode.JPY => "JP", + ECurrencyCode.NOK => "NO", + ECurrencyCode.IDR => "ID", + ECurrencyCode.MYR => "MY", + ECurrencyCode.PHP => "PH", + ECurrencyCode.SGD => "SG", + ECurrencyCode.THB => "TH", + ECurrencyCode.VND => "VN", + ECurrencyCode.KRW => "KR", + ECurrencyCode.TRY => "TR", + ECurrencyCode.UAH => "UA", + ECurrencyCode.MXN => "MX", + ECurrencyCode.CAD => "CA", + ECurrencyCode.AUD => "CX", + ECurrencyCode.NZD => "CK", + ECurrencyCode.CNY => "CN", + ECurrencyCode.INR => "IN", + ECurrencyCode.CLP => "CL", + ECurrencyCode.PEN => "PE", + ECurrencyCode.COP => "CO", + ECurrencyCode.ZAR => "ZA", + ECurrencyCode.HKD => "HK", + ECurrencyCode.TWD => "TW", + ECurrencyCode.SAR => "SA", + ECurrencyCode.AED => "AE", + ECurrencyCode.ARS => "AR", + ECurrencyCode.ILS => "IL", + ECurrencyCode.BYN => "BY", + ECurrencyCode.KZT => "KZ", + ECurrencyCode.KWD => "KW", + ECurrencyCode.QAR => "QA", + ECurrencyCode.CRC => "CT", + ECurrencyCode.UYU => "UY", + ECurrencyCode.BGN => "BG", + ECurrencyCode.HRK => "HR", + ECurrencyCode.CZK => "CZ", + ECurrencyCode.DKK => "DK", + ECurrencyCode.HUF => "HU", + ECurrencyCode.RON => "RO", + _ => Langs.CountryCode, + }; + } }