107 lines
3.6 KiB
C#
107 lines
3.6 KiB
C#
using Discord.Interactions;
|
|
using Discord.WebSocket;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Hosting;
|
|
using Serilog;
|
|
using Serilog.Events;
|
|
|
|
namespace GodReplacementProduct;
|
|
|
|
public class Program
|
|
{
|
|
public static int Main(string[] args)
|
|
{
|
|
Log.Logger = new LoggerConfiguration()
|
|
.MinimumLevel.Debug()
|
|
.MinimumLevel.Override("Microsoft", LogEventLevel.Information)
|
|
.Enrich.FromLogContext()
|
|
.WriteTo.Console()
|
|
.CreateLogger();
|
|
|
|
try
|
|
{
|
|
Log.Information("Starting host");
|
|
BuildHost(args).Run();
|
|
return 0;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Fatal(ex, "Host terminated unexpectedly");
|
|
return 1;
|
|
}
|
|
finally
|
|
{
|
|
Log.CloseAndFlush();
|
|
}
|
|
}
|
|
|
|
public static IHost BuildHost(string[] args) => new HostBuilder()
|
|
.ConfigureDefaults(args)
|
|
.ConfigureAppConfiguration((context, builder) =>
|
|
{
|
|
builder
|
|
.AddEnvironmentVariables(prefix: "DC_")
|
|
.AddEnvironmentVariables("DOTNET_")
|
|
.AddJsonFile("appsettings.json", optional: true)
|
|
.AddJsonFile($"appsettings.{context.HostingEnvironment.EnvironmentName}.json", optional: true);
|
|
|
|
if (context.HostingEnvironment.IsDevelopment())
|
|
{
|
|
builder.AddUserSecrets<Program>();
|
|
|
|
}
|
|
|
|
builder.Build();
|
|
})
|
|
.ConfigureServices((context, services) =>
|
|
{
|
|
services
|
|
.AddSingleton<GodReplacementLogger>()
|
|
.AddSingleton(provider =>
|
|
{
|
|
var logger = provider.GetRequiredService<GodReplacementLogger>();
|
|
var client = new DiscordSocketClient();
|
|
client.Log += logger.LogAsync;
|
|
|
|
return client;
|
|
})
|
|
.AddSingleton(provider =>
|
|
{
|
|
var logger = provider.GetRequiredService<GodReplacementLogger>();
|
|
var interactions = new InteractionService(provider.GetService<DiscordSocketClient>());
|
|
interactions.Log += logger.LogAsync;
|
|
|
|
return interactions;
|
|
})
|
|
.AddHostedService<GodReplacementBot>()
|
|
.AddHostedService<InteractionHandler>();
|
|
|
|
if (context.HostingEnvironment.IsDevelopment())
|
|
{
|
|
services.Configure<DevelopmentOptions>(context.Configuration.GetSection(DevelopmentOptions.Development));
|
|
}
|
|
})
|
|
.UseSerilog((context, services, loggerConfiguration) =>
|
|
{
|
|
loggerConfiguration
|
|
.MinimumLevel.Information()
|
|
.MinimumLevel.Override("Microsoft", LogEventLevel.Information);
|
|
if (context.HostingEnvironment.IsDevelopment()) {
|
|
loggerConfiguration.MinimumLevel.Debug();
|
|
}
|
|
loggerConfiguration
|
|
.ReadFrom.Configuration(context.Configuration)
|
|
.Enrich.FromLogContext()
|
|
.WriteTo.Console();
|
|
|
|
})
|
|
.Build();
|
|
}
|
|
|
|
public class DevelopmentOptions
|
|
{
|
|
public const string Development = "Development";
|
|
|
|
public ulong TestGuildId { get; set; }
|
|
} |