Skip to content

Commit

Permalink
feat 新增 ADDFRIEND 命令
Browse files Browse the repository at this point in the history
  • Loading branch information
chr233 committed Jan 28, 2023
1 parent bedd85d commit 8665ea3
Show file tree
Hide file tree
Showing 7 changed files with 186 additions and 23 deletions.
37 changes: 19 additions & 18 deletions ASFEnhance/ASFEnhance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ public Task OnLoaded()
case "EXPLORER" when access >= EAccess.Master:
case "EX" when access >= EAccess.Master:
return await Explorer.Command.ResponseExploreDiscoveryQueue(bot).ConfigureAwait(false);

//Group
case "GROUPLIST" when Config.EULA && access >= EAccess.FamilySharing:
case "GL" when Config.EULA && access >= EAccess.FamilySharing:
Expand Down Expand Up @@ -331,17 +331,14 @@ public Task OnLoaded()

case "DL2" when access >= EAccess.Operator:
return await Event.Command.ResponseDL2(Utilities.GetArgsAsText(args, 1, ",")).ConfigureAwait(false);
case "UNLOCKPOINTBADGE" when argLength > 3 && access >= EAccess.Master:
case "UPB" when argLength > 3 && access >= EAccess.Master:

case "UNLOCKPOINTBADGE" when argLength >= 4 && access >= EAccess.Master:
case "UPB" when argLength >= 4 && access >= EAccess.Master:
{
string botNames = string.Join(',', args[1..(argLength - 2)]);
return await Event.Command.ResponseUnlockPointBadge(botNames, args[argLength - 2], args[argLength - 1]).ConfigureAwait(false);
return await Event.Command.ResponseUnlockPointBadge(botNames, args[argLength - 2], args.Last()).ConfigureAwait(false);
}
case "UNLOCKPOINTBADGE" when access >= EAccess.Master:
case "UPB" when argLength == 4 && access >= EAccess.Master:
return await Event.Command.ResponseUnlockPointBadge(args[1], args[2], args[3]).ConfigureAwait(false);
case "UNLOCKPOINTBADGE" when access >= EAccess.Master:
case "UPB" when argLength == 3 && access >= EAccess.Master:
return await Event.Command.ResponseUnlockPointBadge(bot, args[1], args[2]).ConfigureAwait(false);

Expand Down Expand Up @@ -470,12 +467,19 @@ public Task OnLoaded()
return await Explorer.Command.ResponseExploreDiscoveryQueue(Utilities.GetArgsAsText(args, 1, ",")).ConfigureAwait(false);

//Friend
case "ADDBOTFRIEND" when argLength == 2 && access >= EAccess.Master:
case "ABF" when argLength == 2 && access >= EAccess.Master:
case "ADDBOTFRIEND" when argLength > 2 && access >= EAccess.Master:
case "ABF" when argLength > 2 && access >= EAccess.Master:
return await Friend.Command.ResponseAddBotFriend(args[1], Utilities.GetArgsAsText(message, 2)).ConfigureAwait(false);
case "ADDBOTFRIEND" when access >= EAccess.Master:
case "ABF" when access >= EAccess.Master:
return await Friend.Command.ResponseAddBotFriend(args[1], args[2]).ConfigureAwait(false);
return await Friend.Command.ResponseAddBotFriend(bot, args[1]).ConfigureAwait(false);

case "ADDFRIEND" when argLength > 2 && access >= EAccess.Master:
case "AF" when argLength > 2 && access >= EAccess.Master:
return await Friend.Command.ResponseAddFriend(args[1], Utilities.GetArgsAsText(message, 2)).ConfigureAwait(false);
case "ADDFRIEND" when access >= EAccess.Master:
case "AF" when access >= EAccess.Master:
return await Friend.Command.ResponseAddFriend(bot, args[1]).ConfigureAwait(false);

//Group
case "GROUPLIST" when Config.EULA && access >= EAccess.FamilySharing:
Expand Down Expand Up @@ -517,7 +521,7 @@ public Task OnLoaded()
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);
return await Profile.Command.ResponseSetProfileGameAvatar(botNames, args[argLength - 2], args.Last()).ConfigureAwait(false);
}
case "GAMEAVATAR" when argLength == 3 && access >= EAccess.Master:
case "GA" when argLength == 3 && access >= EAccess.Master:
Expand All @@ -526,15 +530,12 @@ public Task OnLoaded()
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:
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);
return await Profile.Command.ResponseSetProfileAvatar(botNames, args.Last()).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);
Expand Down
2 changes: 1 addition & 1 deletion ASFEnhance/Community/WebRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ internal static class WebRequest
/// <returns></returns>
internal static async Task PureCommentNotifications(Bot bot)
{
Uri request = new(SteamCommunityURL, $"/profiles/{bot.SteamID}/commentnotifications/");
Uri request = new(SteamCommunityURL, $"/profiles/{bot.SteamID}/commentnotifications/");

Dictionary<string, string> data = new(2) {
{"action", "markallread"},
Expand Down
87 changes: 85 additions & 2 deletions ASFEnhance/Friend/Command.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ internal static class Command
StringBuilder sb = new();
sb.AppendLine(bot.FormatBotResponse(Langs.MultipleLineResult));

foreach (var result in results)
foreach (var (steamId, response) in results)
{
sb.Append(string.Format(Langs.SendBotFriendRequest, result.Item1, result.Item2?.Result == EResult.OK ? Langs.Success : Langs.Failure));
sb.Append(string.Format(Langs.SendBotFriendRequest, steamId, response?.Result == EResult.OK ? Langs.Success : Langs.Failure));
}

return sb.ToString();
Expand Down Expand Up @@ -69,5 +69,88 @@ internal static class Command

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

/// <summary>
/// 添加好友
/// </summary>
/// <param name="bot"></param>
/// <returns></returns>
internal static async Task<string?> ResponseAddFriend(Bot bot, string query)
{
if (!bot.IsConnectedAndLoggedOn)
{
return bot.FormatBotResponse(Strings.BotNotConnected);
}

StringBuilder sb = new();

string[] entries = query.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

if (entries.Length > 1)
{
sb.AppendLine(Langs.MultipleLineResult);
}

foreach (string entry in entries)
{
ulong? steamId;

if (ulong.TryParse(entry, out ulong value))
{
steamId = await WebRequest.GetSteamIdByProfileLink(bot, $"/profiles/{entry}").ConfigureAwait(false) ??
await WebRequest.GetSteamIdByProfileLink(bot, $"/id/{entry}").ConfigureAwait(false);

// 好友代码
if (steamId == null && value < 0xFFFFFFFF)
{
value = SteamId2Steam32(value);
steamId = await WebRequest.GetSteamIdByProfileLink(bot, $"/profiles/{value}").ConfigureAwait(false);
}
}
else
{
steamId = await WebRequest.GetSteamIdByProfileLink(bot, $"/id/{entry}").ConfigureAwait(false);
}

if (steamId != null)
{
var (_, response) = await WebRequest.SendFriendRequest(bot, steamId.Value).ConfigureAwait(false);
sb.AppendLine(string.Format(Langs.CookieItem, entry, response?.Result == EResult.OK ? "发送好友请求成功" : "发送好友请求失败"));
}
else
{
sb.AppendLine(string.Format(Langs.CookieItem, entry, "未找到个人资料"));
}
}

return sb.ToString();
}

/// <summary>
/// 添加好友 (多个Bot)
/// </summary>
/// <param name="botNames"></param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
internal static async Task<string?> ResponseAddFriend(string botNames, string query)
{
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 => ResponseAddFriend(bot, query))).ConfigureAwait(false);

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

return responses.Count > 0 ? string.Join(Environment.NewLine, responses) : null;
}
}
}
44 changes: 44 additions & 0 deletions ASFEnhance/Friend/HtmlParser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using AngleSharp.Dom;
using ArchiSteamFarm.Web.Responses;
using System.Text.RegularExpressions;

namespace ASFEnhance.Friend
{
internal static partial class HtmlParser
{

[GeneratedRegex("\"steamid\":\"(\\d+)\"")]
private static partial Regex MatchSteamId();

/// <summary>
/// 解析个人资料页面
/// </summary>
/// <param name="response"></param>
/// <returns></returns>
internal static ulong? ParseProfilePage(HtmlDocumentResponse? response)
{
if (response?.Content == null)
{
return null;
}

var scriptNode = response.Content.QuerySelector<IElement>("#responsive_page_template_content>script");
var text = scriptNode?.Text();

if (!string.IsNullOrEmpty(text))
{
Regex regex = MatchSteamId();

var match = regex.Match(text);
if (match.Success)
{
var strId = match.Groups[1].Value;
return ulong.TryParse(strId, out ulong steamId) ? steamId : null;
}
}

return null;
}

}
}
15 changes: 15 additions & 0 deletions ASFEnhance/Friend/WebRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,20 @@ internal static class WebRequest
var response = await bot.ArchiWebHandler.UrlPostToJsonObjectWithSession<AjaxAddFriendResponse>(request, data: data, referer: SteamCommunityURL, session: ArchiWebHandler.ESession.CamelCase).ConfigureAwait(false);
return (steamId, response?.Content);
}

/// <summary>
/// 验证个人资料链接
/// </summary>
/// <param name="bot"></param>
/// <param name="path"></param>
/// <returns></returns>
internal static async Task<ulong?> GetSteamIdByProfileLink(Bot bot, string path)
{
Uri request = new(SteamCommunityURL, path);

var response = await bot.ArchiWebHandler.UrlGetToHtmlDocumentWithSession(request).ConfigureAwait(false);

return HtmlParser.ParseProfilePage(response);
}
}
}
3 changes: 1 addition & 2 deletions ASFEnhance/Profile/WebRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ internal static async Task ClearAliasHisrory(Bot bot)
Uri request = new(SteamCommunityURL, $"/profiles/{bot.SteamID}/ajaxclearaliashistory/");
await bot.ArchiWebHandler.UrlPostWithSession(request, referer: SteamStoreURL).ConfigureAwait(false);
}



/// <summary>
/// 获取年度总结Token
/// </summary>
Expand Down
21 changes: 21 additions & 0 deletions ASFEnhance/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using ArchiSteamFarm.Steam.Integration;
using ASFEnhance.Data;
using System.Reflection;
using ArchiSteamFarm.Steam.Integration;

namespace ASFEnhance
{
Expand Down Expand Up @@ -60,6 +61,16 @@ internal static string FormatBotResponse(this Bot bot, string message)
return $"<{bot.BotName}{flag}> {message}";
}

/// <summary>
/// 获取个人资料链接
/// </summary>
/// <param name="bot"></param>
/// <returns></returns>
internal static async Task<string?> GetProfileLink(this Bot bot)
{
return await bot.ArchiWebHandler.GetAbsoluteProfileURL(true).ConfigureAwait(false);
}

/// <summary>
/// 转换SteamId
/// </summary>
Expand All @@ -70,6 +81,16 @@ internal static ulong SteamId2Steam32(ulong steamId)
return steamId - 0x110000100000000;
}

/// <summary>
/// 转换SteamId
/// </summary>
/// <param name="steamId"></param>
/// <returns></returns>
internal static ulong Steam322SteamId(ulong steamId)
{
return steamId + 0x110000100000000;
}

/// <summary>
/// 匹配Steam商店Id
/// </summary>
Expand Down

0 comments on commit 8665ea3

Please sign in to comment.