-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGameRemoverPlugin.cs
117 lines (98 loc) · 4.35 KB
/
GameRemoverPlugin.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
using System;
using System.Collections.Generic;
using System.Composition;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using AngleSharp.Dom;
using ArchiSteamFarm.Core;
using ArchiSteamFarm.Localization;
using ArchiSteamFarm.Plugins.Interfaces;
using ArchiSteamFarm.Steam;
using ArchiSteamFarm.Steam.Data;
using ArchiSteamFarm.Steam.Integration;
using ArchiSteamFarm.Steam.Interaction;
using JetBrains.Annotations;
namespace GameRemover;
// ReSharper disable once UnusedMember.Global
[Export(typeof(IPlugin))]
[UsedImplicitly]
public class GameRemoverPlugin : IBotCommand2
{
public Task OnLoaded()
{
ASF.ArchiLogger.LogGenericInfo($"{Name} by ezhevita | Support & source code: https://github.com/ezhevita/{Name}");
return Task.CompletedTask;
}
public string Name => nameof(GameRemover);
public Version Version => Assembly.GetExecutingAssembly().GetName().Version ?? throw new InvalidOperationException(nameof(Version));
[CLSCompliant(false)]
public Task<string?> OnBotCommand(Bot bot, EAccess access, string message, string[] args, ulong steamID = 0)
{
ArgumentNullException.ThrowIfNull(bot);
ArgumentNullException.ThrowIfNull(args);
return args[0].ToUpperInvariant() switch
{
"DELETEGAME" when args.Length > 2 => ResponseDeleteGame(steamID, access, args[1], Utilities.GetArgsAsText(args, 2, ",")),
"DELETEGAME" when args.Length > 1 => ResponseDeleteGame(bot, access, args[1]),
_ => Task.FromResult<string?>(null)
};
}
private static async Task<string?> ResponseDeleteGame(Bot bot, EAccess access, string appIDsText)
{
if (access < EAccess.Master)
{
return null;
}
HashSet<uint> appIDs = new();
var appIDTexts = appIDsText.Split(',', StringSplitOptions.RemoveEmptyEntries);
foreach (var appIDText in appIDTexts)
{
if (!uint.TryParse(appIDText, out var appID) || (appID == 0) || !appIDs.Add(appID))
{
return bot.Commands.FormatBotResponse(string.Format(CultureInfo.CurrentCulture, Strings.ErrorIsInvalid, appIDText));
}
}
ushort successCount = 0;
foreach (var appID in appIDs)
{
Uri uriDeleteGamePage = new(ArchiWebHandler.SteamHelpURL, $"/en/wizard/HelpWithGameIssue/?appid={appID}&issueid=123");
using var responseDeleteGamePage = (await bot.ArchiWebHandler.UrlGetToHtmlDocumentWithSession(uriDeleteGamePage).ConfigureAwait(false))?.Content;
if (responseDeleteGamePage == null)
{
return bot.Commands.FormatBotResponse(string.Format(CultureInfo.CurrentCulture, Strings.ErrorObjectIsNull, nameof(responseDeleteGamePage)));
}
var node = responseDeleteGamePage.SelectSingleNode<IElement>("//input[@id='packageid']");
if (node == null)
{
return bot.Commands.FormatBotResponse(string.Format(CultureInfo.CurrentCulture, Strings.ErrorObjectIsNull, nameof(node)));
}
if (!uint.TryParse(node.GetAttribute("value"), out var packageID) || (packageID == 0))
{
return bot.Commands.FormatBotResponse(string.Format(CultureInfo.CurrentCulture, Strings.ErrorIsInvalid, nameof(packageID)));
}
Dictionary<string, string> data = new(3)
{
{"packageid", packageID.ToString(CultureInfo.InvariantCulture)},
{"appid", appIDsText}
};
const string RequestDeleteGame = "/en/wizard/AjaxDoPackageRemove";
var responseDeleteGame = (await bot.ArchiWebHandler.UrlPostToJsonObjectWithSession<BooleanResponse>(new Uri(ArchiWebHandler.SteamHelpURL, RequestDeleteGame), data: data, referer: uriDeleteGamePage).ConfigureAwait(false))?.Content;
if (responseDeleteGame?.Success == true)
successCount++;
}
return bot.Commands.FormatBotResponse(successCount == appIDs.Count ? Strings.Success : $"{Strings.WarningFailed}: {successCount} / {appIDs.Count}");
}
private static async Task<string?> ResponseDeleteGame(ulong steamID, EAccess access, string botNames, string appIDsText)
{
var bots = Bot.GetBots(botNames);
if ((bots == null) || (bots.Count == 0))
{
return access >= EAccess.Owner ? Commands.FormatStaticResponse(string.Format(CultureInfo.CurrentCulture, Strings.BotNotFound, botNames)) : null;
}
var results = await Utilities.InParallel(bots.Select(bot => ResponseDeleteGame(bot, Commands.GetProxyAccess(bot, access, steamID), appIDsText))).ConfigureAwait(false);
List<string> responses = new(results.Where(result => !string.IsNullOrEmpty(result))!);
return responses.Count > 0 ? string.Join(Environment.NewLine, responses) : null;
}
}