2021-09-03 03:38:21 -04:00
using System ;
using System.IO ;
using System.Net ;
2021-09-03 11:33:28 -04:00
using System.Threading.Tasks ;
2021-09-03 03:38:21 -04:00
namespace Director
{
public class HumanCommunication
{
2021-09-03 07:18:38 -04:00
private static string call_for_humans_discord_webhook { get ; set ; }
2021-09-03 03:38:21 -04:00
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}" ) ;
2021-09-03 11:33:28 -04:00
if ( logLevel > = LogLevel . Warning )
2021-09-03 03:38:21 -04:00
{
2021-09-03 11:33:28 -04:00
//Task.Run(() => forwardToDiscord(message, logLevel));
}
}
2021-09-05 02:01:08 -04:00
public void forwardToDiscord ( string message , LogLevel logLevel )
2021-09-03 11:33:28 -04:00
{
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}\"}}" ) ;
2021-09-03 03:38:21 -04:00
}
2021-09-03 11:33:28 -04:00
httpWebRequest . GetResponseAsync ( ) ;
2021-09-03 03:38:21 -04:00
}
2021-09-03 07:18:38 -04:00
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
}
2021-09-03 03:38:21 -04:00
}
public class NotInitializedException : Exception
{
public NotInitializedException ( string message ) : base ( message ) { }
}
}