Skip to content

Commit

Permalink
feat 新增 "REGISTEDATE` 命令
Browse files Browse the repository at this point in the history
  • Loading branch information
chr233 committed Jul 5, 2024
1 parent a1c0e80 commit b51dcb8
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 191 deletions.
10 changes: 8 additions & 2 deletions ASFEnhance/ASFEnhance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ public Task OnLoaded()
"PLUGINSUPDATE" or
"PLUGINUPDATE" or
"PU" when access >= EAccess.Master =>
Update.Command.ResponsePluginUpdate(null),
Update.Command.ResponsePluginUpdate(bot, access, null),

//Event
"DL2" when access >= EAccess.Operator =>
Expand Down Expand Up @@ -353,6 +353,9 @@ public Task OnLoaded()
"PHONESUFFIX" when access >= EAccess.Operator =>
Account.Command.ResponseGetPhoneSuffix(bot),

"REGISTEDATE" when access >= EAccess.Operator =>
Account.Command.ResponseGetRegisteDate(bot),

//Cart
"CART" or
"C" when access >= EAccess.Operator =>
Expand Down Expand Up @@ -498,7 +501,7 @@ public Task OnLoaded()
"PLUGINSUPDATE" or
"PLUGINUPDATE" or
"PU" when access >= EAccess.Master =>
Update.Command.ResponsePluginUpdate(Utilities.GetArgsAsText(args, 1, ",")),
Update.Command.ResponsePluginUpdate(bot, access, Utilities.GetArgsAsText(args, 1, ",")),

//Event
"DL2" when argLength > 2 && access >= EAccess.Operator =>
Expand Down Expand Up @@ -639,6 +642,9 @@ public Task OnLoaded()
"PHONESUFFIX" when access >= EAccess.Operator =>
Account.Command.ResponseGetPhoneSuffix(Utilities.GetArgsAsText(args, 1, ",")),

"REGISTEDATE" when access >= EAccess.Operator =>
Account.Command.ResponseGetRegisteDate(Utilities.GetArgsAsText(args, 1, ",")),

//Cart
"CART" or
"C" when access >= EAccess.Operator =>
Expand Down
50 changes: 44 additions & 6 deletions ASFEnhance/Account/Command.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1349,11 +1349,9 @@ private static string NotificationTargetToString(NotificationTarget target)
}

/// <summary>
/// 检查市场限制
/// 获取手机尾号
/// </summary>
/// <param name="bot"></param>
/// <param name="query"></param>
/// <param name="privacy"></param>
/// <returns></returns>
internal static async Task<string?> ResponseGetPhoneSuffix(Bot bot)
{
Expand All @@ -1367,11 +1365,9 @@ private static string NotificationTargetToString(NotificationTarget target)
}

/// <summary>
/// 检查市场限制 (多个Bot)
/// 获取手机尾号 (多个Bot)
/// </summary>
/// <param name="botNames"></param>
/// <param name="query"></param>
/// <param name="privacy"></param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
internal static async Task<string?> ResponseGetPhoneSuffix(string botNames)
Expand All @@ -1393,4 +1389,46 @@ private static string NotificationTargetToString(NotificationTarget target)

return responses.Count > 0 ? string.Join(Environment.NewLine, responses) : null;
}

/// <summary>
/// 获取注册时间
/// </summary>
/// <param name="bot"></param>
/// <returns></returns>
internal static async Task<string?> ResponseGetRegisteDate(Bot bot)
{
if (!bot.IsConnectedAndLoggedOn)
{
return bot.FormatBotResponse(Strings.BotNotConnected);
}

var response = await WebRequest.GetRegisteDate(bot).ConfigureAwait(false);
return bot.FormatBotResponse(response ?? Langs.NetworkError);
}

/// <summary>
/// 获取注册时间 (多个Bot)
/// </summary>
/// <param name="botNames"></param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
internal static async Task<string?> ResponseGetRegisteDate(string botNames)
{
if (string.IsNullOrEmpty(botNames))
{
throw new ArgumentNullException(nameof(botNames));
}

var bots = Bot.GetBots(botNames);

if (bots == null || bots.Count == 0)
{
return FormatStaticResponse(Strings.BotNotFound, botNames);
}

var results = await Utilities.InParallel(bots.Select(bot => ResponseGetRegisteDate(bot))).ConfigureAwait(false);
var responses = new List<string?>(results.Where(result => !string.IsNullOrEmpty(result)));

return responses.Count > 0 ? string.Join(Environment.NewLine, responses) : null;
}
}
9 changes: 4 additions & 5 deletions ASFEnhance/Account/WebRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -526,21 +526,20 @@ internal static async Task<bool> ToggleAppPrivacy(Bot bot, List<uint> appIds, bo
}

/// <summary>
/// 获取电话号码后缀
/// 获取注册时间
/// </summary>
/// <param name="bot"></param>
/// <returns></returns>
internal static async Task<string?> GetBirthday(Bot bot)
internal static async Task<string?> GetRegisteDate(Bot bot)
{
var path = await Utils.GetProfileLink(bot).ConfigureAwait(false);
var request = new Uri(SteamStoreURL, $"/phone/manage?l={Langs.Language}");
var request = new Uri(SteamCommunityURL, $"/profiles/{bot.SteamID}/badges/1?l={Langs.Language}");
var response = await bot.ArchiWebHandler.UrlGetToHtmlDocumentWithSession(request).ConfigureAwait(false);

if (response?.Content == null)
{
return null;
}

return response.Content.QuerySelector("div.phone_header_description>span")?.TextContent?.Trim();
return response.Content.QuerySelector("div.badge_description")?.TextContent?.Trim();
}
}
44 changes: 13 additions & 31 deletions ASFEnhance/Update/Command.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using ArchiSteamFarm.Core;
using ArchiSteamFarm.Plugins;
using ArchiSteamFarm.Plugins.Interfaces;
using ArchiSteamFarm.Steam;
using ASFEnhance.Data;
using ASFEnhance.Data.Plugin;
using ASFEnhance.Explorer;
Expand Down Expand Up @@ -135,69 +136,50 @@ internal static class Command
/// <summary>
/// 检查插件版本
/// </summary>
/// <param name="bot"></param>
/// <param name="access"></param>
/// <param name="pluginNames"></param>
/// <returns></returns>
internal static async Task<string?> ResponsePluginUpdate(string? pluginNames = null)
internal static async Task<string?> ResponsePluginUpdate(Bot bot, EAccess access, string? pluginNames = null)
{
var entries = pluginNames?.ToUpperInvariant().Split(',', StringSplitOptions.RemoveEmptyEntries);

var tasks = new List<Task<PluginUpdateResponse>>();
const string channel = "stable";
List<string> plugins = [];

if (entries?.Length > 0)
{
foreach (var entry in entries)
{
if (entry == "ASFE" || entry == "ASFENHANCE")
{
tasks.Add(WebRequest.UpdatePluginFile("ASFEnhance", MyVersion, "ASFEnhance"));
plugins.Add("ASFEnhance");
}

foreach (var (pluginId, subModule) in _Adapter_.ExtensionCore.SubModules)
{
if (pluginId == entry || subModule.MatchCmdPrefix(entry))
{
tasks.Add(WebRequest.UpdatePluginFile(subModule.PluginName, subModule.PluginVersion, subModule.RepoName));
plugins.Add(subModule.PluginName);
}
}

}
}
else
{
tasks.Add(WebRequest.UpdatePluginFile("ASFEnhance", MyVersion, "ASFEnhance"));
plugins.Add("ASFEnhance");
foreach (var subModule in _Adapter_.ExtensionCore.SubModules.Values)
{
tasks.Add(WebRequest.UpdatePluginFile(subModule.PluginName, subModule.PluginVersion, subModule.RepoName));
plugins.Add(subModule.PluginName);
}
}

if (tasks.Count == 0)
if (plugins.Count == 0)
{
return FormatStaticResponse(Langs.UpdateFailedPluginNotFound, pluginNames);
}

var results = await Utilities.InParallel(tasks).ConfigureAwait(false);

var sb = new StringBuilder();
sb.AppendLine(FormatStaticResponse(Langs.UpdatePluginUpdateInfo));

foreach (var info in results)
{
sb.AppendLine(Static.Line);
sb.AppendLineFormat(Langs.UpdatePluginListItemName, info.PluginName, info.Tips);
sb.AppendLineFormat(Langs.UpdatePluginListItemVersion, info.CurrentVersion, info.OnlineVersion?.ToString() ?? "-.-.-.-");
sb.AppendLineFormat(Langs.UpdatePluginListItemStatus, info.UpdateLog);

if (!string.IsNullOrEmpty(info.ReleaseNote))
{
sb.AppendLine(Langs.UpdatePluginListItemReleaseNote);
sb.AppendLine(info.ReleaseNote);
}
}

sb.AppendLine(Static.Line);
sb.AppendLine(Langs.UpdatePluginListItemUpdateTips);

return sb.ToString();
var cmd = string.Format("UPDATEPLUGINS {0} {1}", channel, string.Join(',', plugins));
return await bot.Commands.Response(access, cmd, 0).ConfigureAwait(false);
}
}
146 changes: 0 additions & 146 deletions ASFEnhance/Update/WebRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,150 +85,4 @@ internal static async Task<PluginUpdateResponse> GetPluginReleaseNote(string plu
//如果没有找到当前语言的版本, 则下载第一个
return relesaeData.Assets.FirstOrDefault()?.DownloadURL;
}

/// <summary>
/// 自动更新插件
/// </summary>
/// <param name="pluginName"></param>
/// <param name="pluginVersion"></param>
/// <param name="pluginRepo"></param>
/// <returns></returns>
internal static async Task<PluginUpdateResponse> UpdatePluginFile(string pluginName, Version pluginVersion, string? pluginRepo)
{
var response = new PluginUpdateResponse
{
PluginName = pluginName,
CurrentVersion = pluginVersion,
OnlineVersion = null,
ReleaseNote = "",
};

if (string.IsNullOrEmpty(pluginRepo))
{
response.UpdateLog = Langs.PluginUpdateNotSupport;
}
else
{
if (!pluginRepo.Contains('/'))
{
pluginRepo = "chr233/" + pluginRepo;
}

var relesaeData = await GitHubService.GetLatestRelease(pluginRepo, true, default).ConfigureAwait(false);
if (relesaeData == null)
{
response.UpdateLog = Langs.GetReleaseInfoFailedNetworkError;
}
else
{
response.ReleaseNote = relesaeData.MarkdownBody;
response.OnlineVersion = Version.TryParse(relesaeData.Tag, out var version) ? version : null;

if (!response.CanUpdate)
{
response.UpdateLog = response.IsLatest ? Langs.AlreadyLatest : Langs.VersionHigher;
}
else
{
var downloadUri = FetchDownloadUrl(relesaeData);
if (downloadUri == null)
{
response.UpdateLog = Langs.NoAssetFoundInReleaseInfo;
}
else
{
var mirrorUrl = new Uri(downloadUri.AbsoluteUri.Replace("https://github.com/chr233", "https://dl.chrxw.com"));
var binResponse = await DownloadRelease(mirrorUrl).ConfigureAwait(false) ?? await DownloadRelease(downloadUri).ConfigureAwait(false);

if (binResponse == null)
{
response.UpdateLog = Langs.DownloadFailed;
}
else
{
response.UpdateLog = await UnzipRelease(binResponse).ConfigureAwait(false);
}
}
}
}
}

return response;
}

/// <summary>
/// 下载发行版
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
internal static Task<BinaryResponse?> DownloadRelease(Uri request)
{
return ASF.WebBrowser?.UrlGetToBinary(request) ?? Task.FromResult<BinaryResponse?>(null);
}

/// <summary>
/// 下载发行版
/// </summary>
/// <param name="binResponse"></param>
/// <returns></returns>
internal static async Task<string> UnzipRelease(BinaryResponse binResponse)
{
var zipBytes = binResponse?.Content as byte[] ?? binResponse?.Content?.ToArray();
if (zipBytes == null)
{
return Langs.DownloadPluginFailed;
}

var ms = new MemoryStream(zipBytes);
try
{
await using (ms.ConfigureAwait(false))
{
using var zipArchive = new ZipArchive(ms);
string pluginFolder = MyDirectory;

var updateFolder = Path.Combine(pluginFolder, "_ASFEnhanceTemp_");
if (Directory.Exists(updateFolder))
{
Directory.Delete(updateFolder, true);
}

zipArchive.ExtractToDirectory(updateFolder);

foreach (var filePath in Directory.GetFiles(updateFolder, "*.dll"))
{
var pluginName = Path.GetFileName(filePath);

var oldPluginPath = Path.Combine(pluginFolder, pluginName);

if (File.Exists(oldPluginPath))
{
var originPath = Path.Combine(pluginFolder, pluginName);
var backupPath = $"{oldPluginPath}.autobak";

int i = 1;
while (File.Exists(backupPath))
{
backupPath = $"{oldPluginPath}.{i++}.autobak";

if (i >= 10)
{
return Langs.DownloadFailedFileConflict;
}
}

File.Move(oldPluginPath, backupPath, true);
File.Move(filePath, oldPluginPath, true);
}
}

return Langs.DownloadPluginSuccess;
}
}
catch (Exception ex)
{
ASFLogger.LogGenericException(ex);
return Langs.DownloadPluginFailedUnzipError;
}
}
}
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>
<PropertyGroup>
<Version>2.1.10.4</Version>
<Version>2.1.11.0</Version>
</PropertyGroup>

<PropertyGroup>
Expand Down

0 comments on commit b51dcb8

Please sign in to comment.