-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
134 lines (113 loc) · 3.86 KB
/
Program.cs
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
using System.Text.Json;
using Discord;
using Discord.Addons.Hosting;
using Discord.Interactions;
using Discord.WebSocket;
using Fergun.Interactive;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Serilog;
using Serilog.Events;
using StarportBot.Common;
using StarportBot.Services;
using EventHandler = StarportBot.Events.EventHandler;
var builder = Host.CreateApplicationBuilder(args);
const string conf = "app_config.json";
#pragma warning disable CA1869
var serializerSettings = new JsonSerializerOptions
{
AllowTrailingCommas = true,
ReadCommentHandling = JsonCommentHandling.Skip,
WriteIndented = true,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
PropertyNameCaseInsensitive = true
};
if (!File.Exists(conf))
{
Console.WriteLine("Application config is missing. Writing default config.");
Console.WriteLine("Please fill out the config before running this application.");
ApplicationConfiguration tempConf = new();
await File.WriteAllTextAsync(conf, JsonSerializer.Serialize(tempConf, serializerSettings));
Environment.Exit(1);
}
var configuration = new ConfigurationBuilder()
.AddJsonFile(conf)
.Build();
var appConfig = configuration.Get<ApplicationConfiguration>();
if (appConfig is null)
{
throw new InvalidOperationException("Configuration file was missing or invalid.");
}
var services = builder.Services
.AddLogging(logBuilder =>
{
var logLevel = appConfig.Debug ? LogEventLevel.Debug : LogEventLevel.Information;
var logger = new LoggerConfiguration()
.MinimumLevel.Is(logLevel)
.WriteTo.Console()
.CreateLogger();
logBuilder.AddSerilog(logger);
})
.AddSingleton(appConfig);
services.AddDiscordHost((config, _) =>
{
config.SocketConfig = new DiscordSocketConfig
{
AlwaysDownloadUsers = true,
AlwaysDownloadDefaultStickers = true,
AlwaysResolveStickers = true,
LogLevel = appConfig.Debug ? LogSeverity.Debug : LogSeverity.Info,
UseSystemClock = true,
GatewayIntents = GatewayIntents.AllUnprivileged | GatewayIntents.GuildMembers | GatewayIntents.MessageContent
};
config.Token = appConfig.DiscordSettings.Token;
});
services.AddInteractionService((config, _) =>
{
config.LogLevel = LogSeverity.Debug;
config.DefaultRunMode = RunMode.Async;
config.UseCompiledLambda = true;
});
services.AddSingleton(new InteractiveConfig()
{
LogLevel = LogSeverity.Warning,
DefaultTimeout = TimeSpan.FromMinutes(5),
ProcessSinglePagePaginators = true
});
services.AddSingleton<InteractiveService>();
services.AddSingleton<LostFreelancerCollection>();
services.AddHostedService<EventHandler>();
var host = builder.Build();
var logger = host.Services.GetRequiredService<ILogger<DiscordSocketClient>>();
var client = host.Services.GetRequiredService<DiscordSocketClient>();
#pragma warning disable CA2254
client.Log += async (msg) =>
{
switch (msg.Severity)
{
case LogSeverity.Critical:
logger.Log(LogLevel.Critical, msg.Exception, msg.Message);
break;
case LogSeverity.Error:
logger.Log(LogLevel.Error, msg.Exception, msg.Message);
break;
case LogSeverity.Warning:
logger.Log(LogLevel.Warning, msg.Message);
break;
case LogSeverity.Info:
logger.Log(LogLevel.Information, msg.Message);
break;
case LogSeverity.Verbose:
logger.Log(LogLevel.Trace, msg.Message);
break;
case LogSeverity.Debug:
logger.Log(LogLevel.Debug, msg.Message);
break;
default:
throw new InvalidOperationException(nameof(msg.Severity));
}
await Task.CompletedTask;
};
await host.RunAsync();