-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
72 lines (65 loc) · 2.57 KB
/
Program.cs
File metadata and controls
72 lines (65 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
using Discord;
using Discord.Commands;
using Discord.Interactions;
using Discord.WebSocket;
using Portal.DB;
using Portal.Helpers;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Serilog;
namespace Portal;
public class Program
{
public static async Task Main()
{
// Create config builder
var configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables()
.Build();
Log.Debug("Configuration loaded");
Log.Debug("Configuring logger");
ConfigureLogger(configuration["Logger:Template"]!);
DiscordSocketConfig discordSocketConfig = new()
{
GatewayIntents = GatewayIntents.Guilds | GatewayIntents.GuildMessages | GatewayIntents.MessageContent,
LogGatewayIntentWarnings = false,
LogLevel = LogSeverity.Debug
};
// Creating a .NET generic Host instance
IHost host = Host.CreateDefaultBuilder()
.ConfigureServices(serviceCollection =>
{
serviceCollection.AddSingleton(discordSocketConfig)
.AddSingleton<IConfiguration>(configuration)
.AddDbContext<BotDbContext>(
options => options.UseSqlite(configuration["Database:ConnectionString"]))
.AddSingleton<DiscordSocketClient>()
.AddSingleton(provider =>
new InteractionService(provider.GetRequiredService<DiscordSocketClient>()))
.AddSingleton<CommandService>()
.AddSingleton<InteractionHandler>()
.AddSingleton<CommandHandler>()
.AddTransient<Helper>();
serviceCollection.AddHostedService<BotService>();
Log.Debug("Added dependencies to ServiceCollection");
})
.UseSerilog()
.Build();
Log.Debug("Built host successfully");
Log.Debug("Attempting to start application");
// Start the host
await host.RunAsync();
}
private static void ConfigureLogger(string loggerTemplate)
{
// Additional configs for logger like formatters
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.Enrich.WithAssemblyName()
.WriteTo.Console(outputTemplate: loggerTemplate)
.CreateLogger();
}
}