From b7119c8bf8272282767fafb1f04adb26f7549728 Mon Sep 17 00:00:00 2001 From: Timothy Baldridge Date: Tue, 18 Jan 2022 14:47:36 -0700 Subject: [PATCH 1/2] Fix broken Discord webhook integration --- Wabbajack.Server/DTOs/DiscordMessage.cs | 57 +++++++++++---------- Wabbajack.Server/Services/DiscordWebHook.cs | 14 +++-- Wabbajack.Server/Services/Watchdog.cs | 33 ------------ Wabbajack.Server/Startup.cs | 2 - Wabbajack.Server/appsettings.json | 2 + 5 files changed, 41 insertions(+), 67 deletions(-) delete mode 100644 Wabbajack.Server/Services/Watchdog.cs diff --git a/Wabbajack.Server/DTOs/DiscordMessage.cs b/Wabbajack.Server/DTOs/DiscordMessage.cs index 2801f24e..3e8a0414 100644 --- a/Wabbajack.Server/DTOs/DiscordMessage.cs +++ b/Wabbajack.Server/DTOs/DiscordMessage.cs @@ -1,73 +1,76 @@ -using System; -using Newtonsoft.Json; +using System.Text.Json.Serialization; namespace Wabbajack.Server.DTOs; public class DiscordMessage { - [JsonProperty("username")] public string UserName { get; set; } + [JsonPropertyName("username")] + public string? UserName { get; set; } - [JsonProperty("avatar_url")] public Uri AvatarUrl { get; set; } + [JsonPropertyName("avatar_url")] + public Uri? AvatarUrl { get; set; } - [JsonProperty("content")] public string Content { get; set; } + [JsonPropertyName("content")] + public string? Content { get; set; } - [JsonProperty("embeds")] public DiscordEmbed[] Embeds { get; set; } + [JsonPropertyName("embeds")] + public DiscordEmbed[]? Embeds { get; set; } } public class DiscordEmbed { - [JsonProperty("title")] public string Title { get; set; } + [JsonPropertyName("title")] public string Title { get; set; } - [JsonProperty("color")] public int Color { get; set; } + [JsonPropertyName("color")] public int Color { get; set; } - [JsonProperty("author")] public DiscordAuthor Author { get; set; } + [JsonPropertyName("author")] public DiscordAuthor Author { get; set; } - [JsonProperty("url")] public Uri Url { get; set; } + [JsonPropertyName("url")] public Uri Url { get; set; } - [JsonProperty("description")] public string Description { get; set; } + [JsonPropertyName("description")] public string Description { get; set; } - [JsonProperty("fields")] public DiscordField Field { get; set; } + [JsonPropertyName("fields")] public DiscordField Field { get; set; } - [JsonProperty("thumbnail")] public DiscordNumbnail Thumbnail { get; set; } + [JsonPropertyName("thumbnail")] public DiscordThumbnail Thumbnail { get; set; } - [JsonProperty("image")] public DiscordImage Image { get; set; } + [JsonPropertyName("image")] public DiscordImage Image { get; set; } - [JsonProperty("footer")] public DiscordFooter Footer { get; set; } + [JsonPropertyName("footer")] public DiscordFooter Footer { get; set; } - [JsonProperty("timestamp")] public DateTime Timestamp { get; set; } = DateTime.UtcNow; + [JsonPropertyName("timestamp")] public DateTime Timestamp { get; set; } = DateTime.UtcNow; } public class DiscordAuthor { - [JsonProperty("name")] public string Name { get; set; } + [JsonPropertyName("name")] public string Name { get; set; } - [JsonProperty("url")] public Uri Url { get; set; } + [JsonPropertyName("url")] public Uri Url { get; set; } - [JsonProperty("icon_url")] public Uri IconUrl { get; set; } + [JsonPropertyName("icon_url")] public Uri IconUrl { get; set; } } public class DiscordField { - [JsonProperty("name")] public string Name { get; set; } + [JsonPropertyName("name")] public string Name { get; set; } - [JsonProperty("value")] public string Value { get; set; } + [JsonPropertyName("value")] public string Value { get; set; } - [JsonProperty("inline")] public bool Inline { get; set; } + [JsonPropertyName("inline")] public bool Inline { get; set; } } -public class DiscordNumbnail +public class DiscordThumbnail { - [JsonProperty("Url")] public Uri Url { get; set; } + [JsonPropertyName("Url")] public Uri Url { get; set; } } public class DiscordImage { - [JsonProperty("Url")] public Uri Url { get; set; } + [JsonPropertyName("Url")] public Uri Url { get; set; } } public class DiscordFooter { - [JsonProperty("text")] public string Text { get; set; } + [JsonPropertyName("text")] public string Text { get; set; } - [JsonProperty("icon_url")] public Uri icon_url { get; set; } + [JsonPropertyName("icon_url")] public Uri icon_url { get; set; } } \ No newline at end of file diff --git a/Wabbajack.Server/Services/DiscordWebHook.cs b/Wabbajack.Server/Services/DiscordWebHook.cs index b24503ee..1a01ca0c 100644 --- a/Wabbajack.Server/Services/DiscordWebHook.cs +++ b/Wabbajack.Server/Services/DiscordWebHook.cs @@ -34,12 +34,16 @@ public class DiscordWebHook : AbstractService _client = client; _dtos = dtos; - var message = new DiscordMessage + Task.Run(async () => { - Content = $"\"{GetQuote()}\" - Sheogorath (as he brings the server online)" - }; - var a = Send(Channel.Ham, message); - var b = Send(Channel.Spam, message); + + var message = new DiscordMessage + { + Content = $"\"{await GetQuote()}\" - Sheogorath (as he brings the server online)" + }; + await Send(Channel.Ham, message); + await Send(Channel.Spam, message); + }); } public async Task Send(Channel channel, DiscordMessage message) diff --git a/Wabbajack.Server/Services/Watchdog.cs b/Wabbajack.Server/Services/Watchdog.cs deleted file mode 100644 index 567583a7..00000000 --- a/Wabbajack.Server/Services/Watchdog.cs +++ /dev/null @@ -1,33 +0,0 @@ -using System; -using System.Threading.Tasks; -using Microsoft.Extensions.Logging; -using Wabbajack.BuildServer; -using Wabbajack.Server.DTOs; - -namespace Wabbajack.Server.Services; - -public class Watchdog : AbstractService -{ - private readonly DiscordWebHook _discord; - - public Watchdog(ILogger logger, AppSettings settings, QuickSync quickSync, DiscordWebHook discordWebHook) - : base(logger, settings, quickSync, TimeSpan.FromMinutes(5)) - { - _discord = discordWebHook; - } - - public override async Task Execute() - { - var report = await _quickSync.Report(); - foreach (var service in report) - if (service.Value.LastRunTime != default && service.Value.LastRunTime >= service.Value.Delay * 4) - await _discord.Send(Channel.Spam, - new DiscordMessage - { - Content = - $"Service {service.Key.Name} has missed it's scheduled execution window. \n Current work: \n {string.Join("\n", service.Value.ActiveWork)}" - }); - - return report.Count; - } -} \ No newline at end of file diff --git a/Wabbajack.Server/Startup.cs b/Wabbajack.Server/Startup.cs index 0d068181..f59c4740 100644 --- a/Wabbajack.Server/Startup.cs +++ b/Wabbajack.Server/Startup.cs @@ -68,7 +68,6 @@ public class Startup services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); - services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); @@ -142,7 +141,6 @@ public class Startup app.UseResponseCompression(); app.UseService(); - app.UseService(); app.UseResponseCaching(); diff --git a/Wabbajack.Server/appsettings.json b/Wabbajack.Server/appsettings.json index de6eb138..654753c5 100644 --- a/Wabbajack.Server/appsettings.json +++ b/Wabbajack.Server/appsettings.json @@ -14,6 +14,8 @@ "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": "*" From 035b8e7aa7f28332a288442e6b45da0345369fee Mon Sep 17 00:00:00 2001 From: Timothy Baldridge Date: Tue, 18 Jan 2022 15:58:54 -0700 Subject: [PATCH 2/2] 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": "*"