From 035b8e7aa7f28332a288442e6b45da0345369fee Mon Sep 17 00:00:00 2001 From: Timothy Baldridge Date: Tue, 18 Jan 2022 15:58:54 -0700 Subject: [PATCH] Re-add discord code --- Wabbajack.CLI/CommandLineBuilder.cs | 10 +- Wabbajack.Server/AppSettings.cs | 2 + Wabbajack.Server/Services/DiscordBackend.cs | 104 ++++++++++++++++++ .../Services/NexusCacheManager.cs | 21 ++++ Wabbajack.Server/Startup.cs | 4 +- Wabbajack.Server/appsettings.json | 2 - 6 files changed, 131 insertions(+), 12 deletions(-) create mode 100644 Wabbajack.Server/Services/DiscordBackend.cs diff --git a/Wabbajack.CLI/CommandLineBuilder.cs b/Wabbajack.CLI/CommandLineBuilder.cs index debbb787..91fc91ae 100644 --- a/Wabbajack.CLI/CommandLineBuilder.cs +++ b/Wabbajack.CLI/CommandLineBuilder.cs @@ -22,15 +22,7 @@ public class CommandLineBuilder var root = new RootCommand(); foreach (var verb in _verbs) root.Add(verb.MakeCommand()); - - /* - foreach (var verb in _verbs) - root.AddCommand(verb.MakeCommand()); - var builder = new System.CommandLine.Builder.CommandLineBuilder(root); - var built = builder.Build(); - var parsed = built.Parse(args); - return await parsed.InvokeAsync(_console);*/ - + return await root.InvokeAsync(args); } } \ No newline at end of file diff --git a/Wabbajack.Server/AppSettings.cs b/Wabbajack.Server/AppSettings.cs index f7af489d..1bf1fd63 100644 --- a/Wabbajack.Server/AppSettings.cs +++ b/Wabbajack.Server/AppSettings.cs @@ -17,6 +17,8 @@ public class AppSettings public AbsolutePath TempPath => (AbsolutePath) TempFolder; public string SpamWebHook { get; set; } = null; public string HamWebHook { get; set; } = null; + + public string DiscordKey { get; set; } public string AuthoredFilesFolder { get; set; } diff --git a/Wabbajack.Server/Services/DiscordBackend.cs b/Wabbajack.Server/Services/DiscordBackend.cs new file mode 100644 index 00000000..c30ecdbe --- /dev/null +++ b/Wabbajack.Server/Services/DiscordBackend.cs @@ -0,0 +1,104 @@ +using Discord; +using Discord.WebSocket; +using Microsoft.Extensions.Logging; +using Wabbajack.BuildServer; + +namespace Wabbajack.Server.Services; + +public class DiscordBackend +{ + private readonly AppSettings _settings; + private readonly ILogger _logger; + private readonly DiscordSocketClient _client; + private readonly NexusCacheManager _nexusCacheManager; + + public DiscordBackend(ILogger logger, AppSettings settings, NexusCacheManager nexusCacheManager) + { + _settings = settings; + _logger = logger; + _nexusCacheManager = nexusCacheManager; + _client = new DiscordSocketClient(new DiscordSocketConfig() + { + + }); + _client.Log += LogAsync; + _client.Ready += ReadyAsync; + _client.MessageReceived += MessageReceivedAsync; + Task.Run(async () => + { + await _client.LoginAsync(TokenType.Bot, settings.DiscordKey); + await _client.StartAsync(); + }); + } + + private async Task MessageReceivedAsync(SocketMessage arg) + { + _logger.LogInformation(arg.Content); + + if (arg.Content.StartsWith("!dervenin")) + { + var parts = arg.Content.Split(" ", StringSplitOptions.RemoveEmptyEntries); + if (parts[0] != "!dervenin") + return; + + if (parts.Length == 1) + { + await ReplyTo(arg, "Wat?"); + } + + if (parts[1] == "purge-nexus-cache") + { + if (parts.Length != 3) + { + await ReplyTo(arg, "Welp you did that wrong, gotta give me a mod-id or url"); + return; + } + var rows = await _nexusCacheManager.Purge(parts[2]); + await ReplyTo(arg, $"Purged {rows} rows"); + } + + if (parts[1] == "nft") + { + await ReplyTo(arg, "No Fucking Thanks."); + } + + } + } + + private async Task ReplyTo(SocketMessage socketMessage, string message) + { + await socketMessage.Channel.SendMessageAsync(message); + } + + + private async Task ReadyAsync() + { + } + + private async Task LogAsync(LogMessage arg) + { + switch (arg.Severity) + { + case LogSeverity.Info: + _logger.LogInformation(arg.Message); + break; + case LogSeverity.Warning: + _logger.LogWarning(arg.Message); + break; + case LogSeverity.Critical: + _logger.LogCritical(arg.Message); + break; + case LogSeverity.Error: + _logger.LogError(arg.Exception, arg.Message); + break; + case LogSeverity.Verbose: + _logger.LogTrace(arg.Message); + break; + case LogSeverity.Debug: + _logger.LogDebug(arg.Message); + break; + default: + throw new ArgumentOutOfRangeException(); + } + } +} \ No newline at end of file diff --git a/Wabbajack.Server/Services/NexusCacheManager.cs b/Wabbajack.Server/Services/NexusCacheManager.cs index 54728d70..36b76b42 100644 --- a/Wabbajack.Server/Services/NexusCacheManager.cs +++ b/Wabbajack.Server/Services/NexusCacheManager.cs @@ -145,4 +145,25 @@ public class NexusCacheManager _lockObject.Release(); } } + + public async Task Purge(string mod) + { + if (Uri.TryCreate(mod, UriKind.Absolute, out var url)) + { + mod = Enumerable.Last(url.AbsolutePath.Split("/", StringSplitOptions.RemoveEmptyEntries)); + } + + var count = 0; + if (!int.TryParse(mod, out var mod_id)) return count; + + foreach (var file in _cacheFolder.EnumerateFiles()) + { + if (!file.FileName.ToString().Contains($"_{mod_id}")) continue; + + await PurgeCacheEntry(file); + count++; + } + + return count; + } } \ No newline at end of file diff --git a/Wabbajack.Server/Startup.cs b/Wabbajack.Server/Startup.cs index f59c4740..2a24309e 100644 --- a/Wabbajack.Server/Startup.cs +++ b/Wabbajack.Server/Startup.cs @@ -75,6 +75,7 @@ public class Startup services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); + services.AddSingleton(); services.AddAllSingleton, NexusApiTokenProvider>(); services.AddAllSingleton>(s => new Resource("Web Requests", 12)); // Application Info @@ -176,6 +177,7 @@ public class Startup app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); // Trigger the internal update code - var _ = app.ApplicationServices.GetRequiredService(); + app.ApplicationServices.GetRequiredService(); + app.ApplicationServices.GetRequiredService(); } } \ No newline at end of file diff --git a/Wabbajack.Server/appsettings.json b/Wabbajack.Server/appsettings.json index 654753c5..de6eb138 100644 --- a/Wabbajack.Server/appsettings.json +++ b/Wabbajack.Server/appsettings.json @@ -14,8 +14,6 @@ "PatchesFilesFolder": "c:\\tmp\\patches", "MirrorFilesFolder": "c:\\tmp\\mirrors", "NexusCacheFolder": "c:\\tmp\\nexus-cache", - "HamWebHook": "https://discordapp.com/api/webhooks/710264989646323772/2VI5Y0rYLOtq1o1KeSR6S2xTRNIOdxONiCju81rnoEUKtcdbVZDdC_PnLdEpYOdiMHCt", - "SpamWebHook": "https://discordapp.com/api/webhooks/710264989646323772/2VI5Y0rYLOtq1o1KeSR6S2xTRNIOdxONiCju81rnoEUKtcdbVZDdC_PnLdEpYOdiMHCt", "GitHubKey": "" }, "AllowedHosts": "*"