Add support for discord webhooks

This commit is contained in:
jvyden 2022-01-19 12:50:17 -05:00
commit 4437470bc5
No known key found for this signature in database
GPG key ID: 18BCF2BE0262B278
8 changed files with 110 additions and 10 deletions

View file

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

View file

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

View file

@ -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<IActionResult> 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");
}
}

View file

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

View file

@ -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
</div>
</a>
@if (ServerSettings.Instance.DiscordWebhookEnabled)
{
<a href="/admin/testWebhook">
<div class="ui blue button">
Test Discord Webhook
</div>
</a>
}
<h2>Commands</h2>
<div class="ui grid">

View file

@ -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)

View file

@ -9,6 +9,7 @@
<ItemGroup>
<PackageReference Include="BCrypt.Net-Next" Version="4.0.2"/>
<PackageReference Include="Discord.Net.Webhook" Version="3.2.0"/>
<PackageReference Include="InfluxDB.Client" Version="3.2.0"/>
<PackageReference Include="JetBrains.Annotations" Version="2021.3.0"/>
<PackageReference Include="Kettu" Version="1.2.1"/>

View file

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