|
3 | 3 | using System.Threading.Tasks;
|
4 | 4 | using ArchiSteamFarm.Core;
|
5 | 5 | using ArchiSteamFarm.Steam;
|
| 6 | +using ArchiSteamFarm.Localization; |
6 | 7 | using ArchiSteamFarm.Plugins.Interfaces;
|
| 8 | +using ArchiSteamFarm.Web.GitHub.Data; |
| 9 | +using ArchiSteamFarm.Web.GitHub; |
| 10 | +using System.Globalization; |
7 | 11 |
|
8 |
| -namespace CommandlessRedeem { |
9 |
| - [Export(typeof(IPlugin))] |
10 |
| - public sealed class CommandlessRedeem : IBotMessage, IBotCommand2 { |
11 |
| - public string Name => nameof(CommandlessRedeem); |
12 |
| - public Version Version => typeof(CommandlessRedeem).Assembly.GetName().Version ?? new Version("0"); |
| 12 | +namespace CommandlessRedeem; |
| 13 | +[Export(typeof(IPlugin))] |
| 14 | +internal sealed class CommandlessRedeem : IBotMessage, IBotCommand2, IGitHubPluginUpdates { |
| 15 | + public string Name => nameof(CommandlessRedeem); |
| 16 | + public Version Version => typeof(CommandlessRedeem).Assembly.GetName().Version ?? new Version("0"); |
13 | 17 |
|
14 |
| - public Task OnLoaded() { |
15 |
| - ASF.ArchiLogger.LogGenericInfo("Commandless Redeem Plugin by Rudokhvist, powered by ginger cats"); |
16 |
| - return Task.CompletedTask; |
| 18 | + public string RepositoryName => "CatPoweredPlugins/CommandlessRedeem"; |
| 19 | + |
| 20 | + public async Task<Uri?> GetTargetReleaseURL(Version asfVersion, string asfVariant, bool asfUpdate, bool stable, bool forced) { |
| 21 | + ArgumentNullException.ThrowIfNull(asfVersion); |
| 22 | + ArgumentException.ThrowIfNullOrEmpty(asfVariant); |
| 23 | + |
| 24 | + if (string.IsNullOrEmpty(RepositoryName)) { |
| 25 | + ASF.ArchiLogger.LogGenericError(string.Format(CultureInfo.CurrentCulture, Strings.WarningFailedWithError, nameof(RepositoryName))); |
| 26 | + |
| 27 | + return null; |
| 28 | + } |
| 29 | + |
| 30 | + ReleaseResponse? releaseResponse = await GitHubService.GetLatestRelease(RepositoryName, stable).ConfigureAwait(false); |
| 31 | + |
| 32 | + if (releaseResponse == null) { |
| 33 | + return null; |
| 34 | + } |
| 35 | + |
| 36 | + Version newVersion = new(releaseResponse.Tag); |
| 37 | + |
| 38 | + if (!(Version.Major == newVersion.Major && Version.Minor == newVersion.Minor && Version.Build == newVersion.Build) && !(asfUpdate || forced)) { |
| 39 | + ASF.ArchiLogger.LogGenericInfo(string.Format(CultureInfo.CurrentCulture, "New {0} plugin version {1} is only compatible with latest ASF version", Name, newVersion)); |
| 40 | + return null; |
| 41 | + } |
| 42 | + |
| 43 | + |
| 44 | + if (Version >= newVersion & !forced) { |
| 45 | + ASF.ArchiLogger.LogGenericInfo(string.Format(CultureInfo.CurrentCulture, Strings.PluginUpdateNotFound, Name, Version, newVersion)); |
| 46 | + |
| 47 | + return null; |
17 | 48 | }
|
18 | 49 |
|
19 |
| - public Task<string?> OnBotMessage(Bot bot, ulong steamID, string message) => HandleMessageInternal(bot, bot.GetAccess(steamID), message); |
| 50 | + if (releaseResponse.Assets.Count == 0) { |
| 51 | + ASF.ArchiLogger.LogGenericWarning(string.Format(CultureInfo.CurrentCulture, Strings.PluginUpdateNoAssetFound, Name, Version, newVersion)); |
20 | 52 |
|
21 |
| - public static async Task<string?> HandleMessageInternal(Bot bot, EAccess access, string message) { |
22 |
| - if (access < EAccess.Operator) { |
23 |
| - return null; |
24 |
| - } |
| 53 | + return null; |
| 54 | + } |
| 55 | + |
| 56 | + ReleaseAsset? asset = await ((IGitHubPluginUpdates) this).GetTargetReleaseAsset(asfVersion, asfVariant, newVersion, releaseResponse.Assets).ConfigureAwait(false); |
25 | 57 |
|
26 |
| - if (!Utilities.IsValidCdKey(message.Split((char[]?) null, StringSplitOptions.RemoveEmptyEntries)[0])) { |
27 |
| - return null; |
28 |
| - } |
| 58 | + if ((asset == null) || !releaseResponse.Assets.Contains(asset)) { |
| 59 | + ASF.ArchiLogger.LogGenericWarning(string.Format(CultureInfo.CurrentCulture, Strings.PluginUpdateNoAssetFound, Name, Version, newVersion)); |
29 | 60 |
|
30 |
| - return await bot.Commands.Response(access, "r " + bot.BotName + " " + message).ConfigureAwait(false); |
| 61 | + return null; |
31 | 62 | }
|
32 | 63 |
|
33 |
| - public Task<string?> OnBotCommand(Bot bot, EAccess access, string message, string[] args, ulong steamID = 0) => HandleMessageInternal(bot, access, string.Join(" ", args)); |
| 64 | + ASF.ArchiLogger.LogGenericInfo(string.Format(CultureInfo.CurrentCulture, Strings.PluginUpdateFound, Name, Version, newVersion)); |
| 65 | + |
| 66 | + return asset.DownloadURL; |
34 | 67 | }
|
| 68 | + |
| 69 | + public Task OnLoaded() { |
| 70 | + ASF.ArchiLogger.LogGenericInfo("Commandless Redeem Plugin by Rudokhvist, powered by ginger cats"); |
| 71 | + return Task.CompletedTask; |
| 72 | + } |
| 73 | + |
| 74 | + public Task<string?> OnBotMessage(Bot bot, ulong steamID, string message) => HandleMessageInternal(bot, bot.GetAccess(steamID), message); |
| 75 | + |
| 76 | + public static async Task<string?> HandleMessageInternal(Bot bot, EAccess access, string message) { |
| 77 | + if (access < EAccess.Operator) { |
| 78 | + return null; |
| 79 | + } |
| 80 | + |
| 81 | + if (!Utilities.IsValidCdKey(message.Split((char[]?) null, StringSplitOptions.RemoveEmptyEntries)[0])) { |
| 82 | + return null; |
| 83 | + } |
| 84 | + |
| 85 | + return await bot.Commands.Response(access, "r " + bot.BotName + " " + message).ConfigureAwait(false); |
| 86 | + } |
| 87 | + |
| 88 | + public Task<string?> OnBotCommand(Bot bot, EAccess access, string message, string[] args, ulong steamID = 0) => HandleMessageInternal(bot, access, string.Join(" ", args)); |
35 | 89 | }
|
0 commit comments