Skip to content

Commit

Permalink
feat 新增 SETAVATAR 命令
Browse files Browse the repository at this point in the history
  • Loading branch information
chr233 committed Jan 15, 2023
1 parent b67d84b commit a621896
Show file tree
Hide file tree
Showing 10 changed files with 252 additions and 3 deletions.
19 changes: 17 additions & 2 deletions ASFEnhance/ASFEnhance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -493,15 +493,30 @@ public Task OnLoaded()

case "GAMEAVATAR" when argLength > 3 && access >= EAccess.Master:
case "GA" when argLength > 3 && access >= EAccess.Master:
string botNames = string.Join(',', args[1..(argLength - 2)]);
return await Profile.Command.ResponseSetProfileGameAvatar(botNames, args[argLength - 2], args[argLength - 1]).ConfigureAwait(false);
{
string botNames = string.Join(',', args[1..(argLength - 2)]);
return await Profile.Command.ResponseSetProfileGameAvatar(botNames, args[argLength - 2], args[argLength - 1]).ConfigureAwait(false);
}
case "GAMEAVATAR" when argLength == 3 && access >= EAccess.Master:
case "GA" when argLength == 3 && access >= EAccess.Master:
return await Profile.Command.ResponseSetProfileGameAvatar(args[1], args[2], null).ConfigureAwait(false);
case "GAMEAVATAR" when access >= EAccess.Master:
case "GA" when access >= EAccess.Master:
return await Profile.Command.ResponseSetProfileGameAvatar(bot, args[1], null).ConfigureAwait(false);

case "SETAVATAR" when argLength > 3 && access >= EAccess.Master:
case "SEA" when argLength > 3 && access >= EAccess.Master:
{
string botNames = string.Join(',', args[1..(argLength - 1)]);
return await Profile.Command.ResponseSetProfileAvatar(botNames, args[argLength - 1]).ConfigureAwait(false);
}
case "SETAVATAR" when argLength == 3 && access >= EAccess.Master:
case "SEA" when argLength == 3 && access >= EAccess.Master:
return await Profile.Command.ResponseSetProfileAvatar(args[1], args[2]).ConfigureAwait(false);
case "SETAVATAR" when access >= EAccess.Master:
case "SEA" when access >= EAccess.Master:
return await Profile.Command.ResponseSetProfileAvatar(bot, args[1]).ConfigureAwait(false);

case "STEAMID" when access >= EAccess.FamilySharing:
case "SID" when access >= EAccess.FamilySharing:
return await Profile.Command.ResponseGetSteamId(Utilities.GetArgsAsText(args, 1, ",")).ConfigureAwait(false);
Expand Down
2 changes: 1 addition & 1 deletion ASFEnhance/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.Reflection;

[assembly: CLSCompliant(false)]
[assembly: AssemblyVersion("1.7.8.0")]
[assembly: AssemblyVersion("1.7.9.0")]
[assembly: AssemblyCopyright("Copyright © 2022 Chr_")]
[assembly: AssemblyProduct("ASFEnhance")]
[assembly: AssemblyCompany("chrxw.com")]
Expand Down
54 changes: 54 additions & 0 deletions ASFEnhance/Localization/Langs.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions ASFEnhance/Localization/Langs.en-US.resx
Original file line number Diff line number Diff line change
Expand Up @@ -909,4 +909,22 @@
<data name="GameAvatarGameNotFound" xml:space="preserve">
<value>Fetch avilable game list for game avatar failed</value>
</data>
<data name="ChangeAvatarFailed" xml:space="preserve">
<value>Set avatar failed</value>
</data>
<data name="ChangeAvatarSuccess" xml:space="preserve">
<value>Set avatar success</value>
</data>
<data name="DownloadImageFailed" xml:space="preserve">
<value>Download avatar image failed</value>
</data>
<data name="UsageGAMEAVATAR" xml:space="preserve">
<value>Set game avatar by specified gameId and avatarId</value>
</data>
<data name="UsageRANDOMGAMEAVATAR" xml:space="preserve">
<value>Set game avatar randomly</value>
</data>
<data name="UsageSETAVATAR" xml:space="preserve">
<value>Set avatar to specified online image</value>
</data>
</root>
18 changes: 18 additions & 0 deletions ASFEnhance/Localization/Langs.resx
Original file line number Diff line number Diff line change
Expand Up @@ -909,4 +909,22 @@
<data name="GameAvatarGameAvatarIsEmpty" xml:space="preserve">
<value>读取可用游戏头像失败</value>
</data>
<data name="DownloadImageFailed" xml:space="preserve">
<value>下载头像图片失败</value>
</data>
<data name="ChangeAvatarSuccess" xml:space="preserve">
<value>修改头像成功</value>
</data>
<data name="ChangeAvatarFailed" xml:space="preserve">
<value>修改头像失败</value>
</data>
<data name="UsageGAMEAVATAR" xml:space="preserve">
<value>根据指定 AppID 和 AvatarID 设置机器人的头像</value>
</data>
<data name="UsageRANDOMGAMEAVATAR" xml:space="preserve">
<value>设置机器人的头像为随机游戏头像</value>
</data>
<data name="UsageSETAVATAR" xml:space="preserve">
<value>设置机器人头像为给定的网络图片</value>
</data>
</root>
18 changes: 18 additions & 0 deletions ASFEnhance/Localization/Langs.ru-RU.resx
Original file line number Diff line number Diff line change
Expand Up @@ -909,4 +909,22 @@
<data name="GameAvatarGameAvatarIsEmpty" xml:space="preserve">
<value>Fetch avilable game avatar failed</value>
</data>
<data name="ChangeAvatarFailed" xml:space="preserve">
<value>Set avatar failed</value>
</data>
<data name="ChangeAvatarSuccess" xml:space="preserve">
<value>Set avatar success</value>
</data>
<data name="DownloadImageFailed" xml:space="preserve">
<value>Download avatar image failed</value>
</data>
<data name="UsageGAMEAVATAR" xml:space="preserve">
<value>Set game avatar by specified gameId and avatarId</value>
</data>
<data name="UsageRANDOMGAMEAVATAR" xml:space="preserve">
<value>Set game avatar randomly</value>
</data>
<data name="UsageSETAVATAR" xml:space="preserve">
<value>Set avatar to specified online image</value>
</data>
</root>
18 changes: 18 additions & 0 deletions ASFEnhance/Localization/Langs.zh-Hans.resx
Original file line number Diff line number Diff line change
Expand Up @@ -909,4 +909,22 @@
<data name="GameAvatarGameNotFound" xml:space="preserve">
<value>获取游戏列表失败</value>
</data>
<data name="ChangeAvatarFailed" xml:space="preserve">
<value>修改头像失败</value>
</data>
<data name="ChangeAvatarSuccess" xml:space="preserve">
<value>修改头像成功</value>
</data>
<data name="DownloadImageFailed" xml:space="preserve">
<value>下载头像图片失败</value>
</data>
<data name="UsageGAMEAVATAR" xml:space="preserve">
<value>根据指定 AppID 和 AvatarID 设置机器人的头像</value>
</data>
<data name="UsageRANDOMGAMEAVATAR" xml:space="preserve">
<value>设置机器人的头像为随机游戏头像</value>
</data>
<data name="UsageSETAVATAR" xml:space="preserve">
<value>设置机器人头像为给定的网络图片</value>
</data>
</root>
9 changes: 9 additions & 0 deletions ASFEnhance/Other/CommandHelpData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ internal static class CommandHelpData
{ "PROFILE", "[Bots]" },
{ "PROFILELINK", "[Bots]" },
{ "TRADELINK", "[Bots]" },
{ "GAMEAVATAR", "[Bots] AppId [AvatarID]" },
{ "RANDOMGAMEAVATAR", "[Bots]" },
{ "SETAVATAR", "[Bots] ImageUrl" },

// 商店
{ "APPDETAIL", "[Bots] <AppIds>" },
Expand Down Expand Up @@ -130,6 +133,9 @@ internal static class CommandHelpData
{ "PROFILE", Langs.UsagePROFILE },
{ "PROFILELINK", Langs.UsagePROFILELINK },
{ "TRADELINK", Langs.UsageTRADELINK },
{ "GAMEAVATAR", Langs.UsageGAMEAVATAR },
{ "RANDOMGAMEAVATAR", Langs.UsageRANDOMGAMEAVATAR },
{ "SETAVATAR", Langs.UsageSETAVATAR },

// 商店
{ "APPDETAIL", Langs.UsageAPPDETAIL },
Expand Down Expand Up @@ -202,6 +208,9 @@ internal static class CommandHelpData
{ "PF", "PROFILE" },
{ "PFL", "PROFILELINK" },
{ "TL", "TRADELINK" },
{ "GA", "GAMEAVATAR"},
{ "RGA", "RANDOMGAMEAVATAR"},
{ "SEA", "SETAVATAR" },

// 商店
{ "AD", "APPDETAIL" },
Expand Down
51 changes: 51 additions & 0 deletions ASFEnhance/Profile/Command.cs
Original file line number Diff line number Diff line change
Expand Up @@ -595,5 +595,56 @@ internal static partial class Command

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

/// <summary>
/// 设置个人资料头像
/// </summary>
/// <param name="bot"></param>
/// <param name="imgUrl"></param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
internal static async Task<string?> ResponseSetProfileAvatar(Bot bot, string imgUrl)
{
if (!bot.IsConnectedAndLoggedOn)
{
return bot.FormatBotResponse(Strings.BotNotConnected);
}

if (string.IsNullOrEmpty(imgUrl))
{
throw new ArgumentNullException(nameof(imgUrl));
}

var result = await WebRequest.ApplyCustomAvatar(bot, imgUrl).ConfigureAwait(false);
return bot.FormatBotResponse(result);
}

/// <summary>
/// 设置个人资料头像 (多个Bot)
/// </summary>
/// <param name="botNames"></param>
/// <param name="imgUrl"></param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
internal static async Task<string?> ResponseSetProfileAvatar(string botNames, string imgUrl)
{
if (string.IsNullOrEmpty(botNames))
{
throw new ArgumentNullException(nameof(botNames));
}

HashSet<Bot>? bots = Bot.GetBots(botNames);

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

IList<string?> results = await Utilities.InParallel(bots.Select(bot => ResponseSetProfileAvatar(bot, imgUrl))).ConfigureAwait(false);

List<string> responses = new(results.Where(result => !string.IsNullOrEmpty(result))!);

return responses.Count > 0 ? string.Join(Environment.NewLine, responses) : null;
}
}
}
48 changes: 48 additions & 0 deletions ASFEnhance/Profile/WebRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using ArchiSteamFarm.Web.Responses;
using ASFEnhance.Data;
using ASFEnhance.Localization;
using System.Net;
using static ASFEnhance.Utils;

namespace ASFEnhance.Profile
Expand Down Expand Up @@ -159,5 +160,52 @@ internal static async Task<bool> ApplyGameAvatar(Bot bot, int gameId, int avatar
var response = await bot.ArchiWebHandler.UrlGetToHtmlDocumentWithSession(request, referer: SteamCommunityURL).ConfigureAwait(false);
return HtmlParser.ParseAvatarsPageToGameIds(response);
}

/// <summary>
/// 下载图片
/// </summary>
/// <param name="bot"></param>
/// <param name="url"></param>
/// <returns></returns>
private static async Task<IEnumerable<byte>?> DownloadImage(Bot bot, string url)
{
Uri request = new Uri(url);
var response = await bot.ArchiWebHandler.WebBrowser.UrlGetToBinary(request).ConfigureAwait(false);
return response?.Content;
}

/// <summary>
/// 设置自定义头像
/// </summary>
/// <param name="bot"></param>
/// <param name="imgUrl"></param>
/// <returns></returns>
internal static async Task<string> ApplyCustomAvatar(Bot bot, string imgUrl)
{
var bytes = await DownloadImage(bot, imgUrl).ConfigureAwait(false);

if (bytes == null)
{
return Langs.DownloadImageFailed;
}

var content = new MultipartFormDataContent
{
{ new ByteArrayContent(bytes.ToArray()), "file", "Test.txt" }
};

var cookies = bot.ArchiWebHandler.WebBrowser.CookieContainer.GetCookies(SteamStoreURL)
.Select(x => $"{x.Name}={x.Value}");

Dictionary<string, string> headers = new() {
{ "Accept", "application/json, text/plain, */*" },
{ "Cookie", string.Join(';',cookies) }
};

Uri request = new(SteamCommunityURL, "/actions/FileUploader/");
var response = await bot.ArchiWebHandler.WebBrowser.UrlPost(request, headers, data: content).ConfigureAwait(false);

return response?.StatusCode == HttpStatusCode.OK ? Langs.ChangeAvatarSuccess : Langs.ChangeAvatarFailed;
}
}
}

0 comments on commit a621896

Please sign in to comment.