Fix broken Discord webhook integration

This commit is contained in:
Timothy Baldridge 2022-01-18 14:47:36 -07:00
parent 47052e1fc7
commit b7119c8bf8
5 changed files with 41 additions and 67 deletions

View File

@ -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; }
}

View File

@ -34,12 +34,16 @@ public class DiscordWebHook : AbstractService<DiscordWebHook, int>
_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)

View File

@ -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<Watchdog, int>
{
private readonly DiscordWebHook _discord;
public Watchdog(ILogger<Watchdog> logger, AppSettings settings, QuickSync quickSync, DiscordWebHook discordWebHook)
: base(logger, settings, quickSync, TimeSpan.FromMinutes(5))
{
_discord = discordWebHook;
}
public override async Task<int> 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;
}
}

View File

@ -68,7 +68,6 @@ public class Startup
services.AddSingleton<QuickSync>();
services.AddSingleton<GlobalInformation>();
services.AddSingleton<DiscordWebHook>();
services.AddSingleton<Watchdog>();
services.AddSingleton<Metrics>();
services.AddSingleton<HttpClient>();
services.AddSingleton<AuthorFiles>();
@ -142,7 +141,6 @@ public class Startup
app.UseResponseCompression();
app.UseService<DiscordWebHook>();
app.UseService<Watchdog>();
app.UseResponseCaching();

View File

@ -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": "*"