director/HumanCommunication.cs

75 lines
2.7 KiB
C#

using System;
using System.IO;
using System.Net;
using System.Threading.Tasks;
namespace Director
{
public class HumanCommunication
{
private static string call_for_humans_discord_webhook { get; set; }
private static HumanCommunication _instance = null;
private static readonly object createLock = new object();
public static void Configure(string call_for_humans_discord_webhook)
{
lock (createLock)
{
if (_instance == null)
{
_instance = new HumanCommunication(call_for_humans_discord_webhook);
}
}
}
public static HumanCommunication Instance
{
get
{
lock (createLock)
{
if (_instance == null)
{
throw new NotInitializedException("Configure me first");
}
}
return _instance;
}
}
private HumanCommunication(string discord_webhook)
{
HumanCommunication.call_for_humans_discord_webhook = discord_webhook;
}
public void Say(string message, LogLevel logLevel = LogLevel.Info)
{
Console.WriteLine($"[{DateTime.Now.ToLongTimeString()}] [{logLevel.ToString()}] {message}");
if(logLevel >= LogLevel.Warning)
{
//Task.Run(() => forwardToDiscord(message, logLevel));
}
}
public async void forwardToDiscord(string message, LogLevel logLevel)
{
var httpWebRequest = (HttpWebRequest)WebRequest.Create(call_for_humans_discord_webhook);
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
streamWriter.Write($"{{\"content\":\"[{DateTime.Now.ToLongTimeString()}] [{logLevel.ToString()}] {message}\"}}");
}
httpWebRequest.GetResponseAsync();
}
public enum LogLevel
{
Trace, //development
Verbose,
Info, //archival purposes, status checking, curiosity satisfaciton
Warning,
Error, //it's going to go up, but look bad. The bar is (currently) low enough that this won't necessarily damage the brand, but for better creators it would.
Showstopper //it's not going to go up
}
}
public class NotInitializedException : Exception
{
public NotInitializedException(string message) : base(message) { }
}
}