diff --git a/ProjectLighthouse/Controllers/PhotosController.cs b/ProjectLighthouse/Controllers/PhotosController.cs index f00ef305..850de160 100644 --- a/ProjectLighthouse/Controllers/PhotosController.cs +++ b/ProjectLighthouse/Controllers/PhotosController.cs @@ -5,7 +5,9 @@ using System.IO; using System.Linq; using System.Threading.Tasks; using System.Xml.Serialization; +using Discord; using Kettu; +using LBPUnion.ProjectLighthouse.Helpers; using LBPUnion.ProjectLighthouse.Logging; using LBPUnion.ProjectLighthouse.Serialization; using LBPUnion.ProjectLighthouse.Types; @@ -88,6 +90,17 @@ public class PhotosController : ControllerBase await this.database.SaveChangesAsync(); + await WebhookHelper.SendWebhook + ( + new EmbedBuilder + { + Title = "New photo uploaded!", + Description = $"{user.Username} uploaded a new photo.", + ImageUrl = $"{ServerSettings.Instance.ExternalUrl}/gameAssets/{photo.LargeHash}", + Color = WebhookHelper.UnionColor, + } + ); + return this.Ok(); } diff --git a/ProjectLighthouse/Controllers/PublishController.cs b/ProjectLighthouse/Controllers/PublishController.cs index 46a62ce7..ab004c2d 100644 --- a/ProjectLighthouse/Controllers/PublishController.cs +++ b/ProjectLighthouse/Controllers/PublishController.cs @@ -156,6 +156,12 @@ public class PublishController : ControllerBase this.database.Slots.Add(slot); await this.database.SaveChangesAsync(); + await WebhookHelper.SendWebhook + ( + "New level published!", + $"**{user.Username}** just published a new level: [**{slot.Name}**]({ServerSettings.Instance.ExternalUrl}/slot/{slot.SlotId})\n{slot.Description}" + ); + return this.Ok(slot.Serialize()); } diff --git a/ProjectLighthouse/Controllers/Website/Admin/AdminPanelController.cs b/ProjectLighthouse/Controllers/Website/Admin/AdminPanelController.cs new file mode 100644 index 00000000..c7e0e31a --- /dev/null +++ b/ProjectLighthouse/Controllers/Website/Admin/AdminPanelController.cs @@ -0,0 +1,29 @@ +#nullable enable +using System.Threading.Tasks; +using LBPUnion.ProjectLighthouse.Helpers; +using LBPUnion.ProjectLighthouse.Types; +using Microsoft.AspNetCore.Mvc; + +namespace LBPUnion.ProjectLighthouse.Controllers.Website.Admin; + +[ApiController] +[Route("/admin")] +public class AdminPanelController : ControllerBase +{ + private readonly Database database; + + public AdminPanelController(Database database) + { + this.database = database; + } + + [HttpGet("testWebhook")] + public async Task TestWebhook() + { + User? user = this.database.UserFromWebRequest(this.Request); + if (user == null || !user.IsAdmin) return this.NotFound(); + + await WebhookHelper.SendWebhook("Testing 123", "Someone is testing the Discord webhook from the admin panel."); + return this.Redirect("/admin"); + } +} \ No newline at end of file diff --git a/ProjectLighthouse/Helpers/WebhookHelper.cs b/ProjectLighthouse/Helpers/WebhookHelper.cs new file mode 100644 index 00000000..c624fc45 --- /dev/null +++ b/ProjectLighthouse/Helpers/WebhookHelper.cs @@ -0,0 +1,38 @@ +using System.Threading.Tasks; +using Discord; +using Discord.Webhook; +using LBPUnion.ProjectLighthouse.Types.Settings; + +namespace LBPUnion.ProjectLighthouse.Helpers; + +public static class WebhookHelper +{ + private static readonly DiscordWebhookClient client = new(ServerSettings.Instance.DiscordWebhookUrl); + public static readonly Color UnionColor = new(0, 140, 255); + + public static Task SendWebhook(EmbedBuilder builder) => SendWebhook(builder.Build()); + + public static async Task SendWebhook(Embed embed) + { + if (!ServerSettings.Instance.DiscordWebhookEnabled) return; + + await client.SendMessageAsync + ( + embeds: new[] + { + embed, + } + ); + } + + public static Task SendWebhook(string title, string description) + => SendWebhook + ( + new EmbedBuilder + { + Title = title, + Description = description, + Color = UnionColor, + } + ); +} \ No newline at end of file diff --git a/ProjectLighthouse/Pages/Admin/AdminPanelPage.cshtml b/ProjectLighthouse/Pages/Admin/AdminPanelPage.cshtml index f9c242b3..cb28e826 100644 --- a/ProjectLighthouse/Pages/Admin/AdminPanelPage.cshtml +++ b/ProjectLighthouse/Pages/Admin/AdminPanelPage.cshtml @@ -1,6 +1,7 @@ @page "/admin" @using LBPUnion.ProjectLighthouse.Helpers @using LBPUnion.ProjectLighthouse.Maintenance +@using LBPUnion.ProjectLighthouse.Types.Settings @model LBPUnion.ProjectLighthouse.Pages.Admin.AdminPanelPage @{ @@ -13,6 +14,14 @@ View Users +@if (ServerSettings.Instance.DiscordWebhookEnabled) +{ + +
+ Test Discord Webhook +
+
+}

Commands

diff --git a/ProjectLighthouse/Program.cs b/ProjectLighthouse/Program.cs index 56d9764a..aafe1d9d 100644 --- a/ProjectLighthouse/Program.cs +++ b/ProjectLighthouse/Program.cs @@ -61,14 +61,14 @@ public static class Program } #if DEBUG - Logger.Log - ( - "This is a debug build, so performance may suffer! " + - "If you are running Lighthouse in a production environment, " + - "it is highly recommended to run a release build. ", - LoggerLevelStartup.Instance - ); - Logger.Log("You can do so by running any dotnet command with the flag: \"-c Release\". ", LoggerLevelStartup.Instance); + Logger.Log + ( + "This is a debug build, so performance may suffer! " + + "If you are running Lighthouse in a production environment, " + + "it is highly recommended to run a release build. ", + LoggerLevelStartup.Instance + ); + Logger.Log("You can do so by running any dotnet command with the flag: \"-c Release\". ", LoggerLevelStartup.Instance); #endif if (args.Length != 0) diff --git a/ProjectLighthouse/ProjectLighthouse.csproj b/ProjectLighthouse/ProjectLighthouse.csproj index 53058e0f..212aa906 100644 --- a/ProjectLighthouse/ProjectLighthouse.csproj +++ b/ProjectLighthouse/ProjectLighthouse.csproj @@ -9,6 +9,7 @@ + diff --git a/ProjectLighthouse/Types/Settings/ServerSettings.cs b/ProjectLighthouse/Types/Settings/ServerSettings.cs index ba6cd179..c4ca4976 100644 --- a/ProjectLighthouse/Types/Settings/ServerSettings.cs +++ b/ProjectLighthouse/Types/Settings/ServerSettings.cs @@ -12,7 +12,7 @@ namespace LBPUnion.ProjectLighthouse.Types.Settings; public class ServerSettings { - public const int CurrentConfigVersion = 16; // MUST BE INCREMENTED FOR EVERY CONFIG CHANGE! + public const int CurrentConfigVersion = 17; // MUST BE INCREMENTED FOR EVERY CONFIG CHANGE! static ServerSettings() { if (ServerStatics.IsUnitTesting) return; // Unit testing, we don't want to read configurations here since the tests will provide their own @@ -77,7 +77,7 @@ public class ServerSettings #if !DEBUG public string AnnounceText { get; set; } = "You are now logged in as %user."; #else - public string AnnounceText { get; set; } = "You are now logged in as %user (id: %id)."; + public string AnnounceText { get; set; } = "You are now logged in as %user (id: %id)."; #endif public string DbConnectionString { get; set; } = "server=127.0.0.1;uid=root;pwd=lighthouse;database=lighthouse"; @@ -107,6 +107,10 @@ public class ServerSettings public bool BooingEnabled { get; set; } = true; + public bool DiscordWebhookEnabled { get; set; } + + public string DiscordWebhookUrl { get; set; } = ""; + public bool VitaCreateMode { get; set; } #region Meta