-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
118 lines (101 loc) · 5.65 KB
/
Program.cs
File metadata and controls
118 lines (101 loc) · 5.65 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
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
using Discord;
using Discord.Interactions;
using Discord.Net.Providers.WS4Net;
using Discord.WebSocket;
using Lyuze.Core.Abstractions.Interfaces;
using Lyuze.Core.Features.Admin;
using Lyuze.Core.Features.Anime;
using Lyuze.Core.Features.Profiles;
using Lyuze.Core.Features.Roles;
using Lyuze.Core.Infrastructure.Configuration;
using Lyuze.Core.Infrastructure.Database;
using Lyuze.Core.Infrastructure.DiscordNet;
using Lyuze.Core.Infrastructure.DiscordNet.Handlers;
using Lyuze.Core.Infrastructure.Http;
using Lyuze.Core.Infrastructure.Logging;
using Lyuze.Core.Shared.Embeds;
using Lyuze.Core.Shared.Embeds.Providers;
using Lyuze.Core.Shared.Images;
using Lyuze.Core.Shared.Images.Primitives;
using Lyuze.Core.Shared.Status;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
namespace Lyuze;
public class Program {
public static async Task Main() {
var host = Host.CreateDefaultBuilder()
.ConfigureServices((_, services) => {
// Core config & logging
services.AddSingleton<ILoggingService, LoggingService>();
services.AddSingleton<ISettingsService, SettingsService>();
services.AddSingleton<IPlayerService, PlayerService>();
services.AddSingleton(sp => sp.GetRequiredService<ISettingsService>().Value); // SettingsConfig
// DatabaseContext depends on SettingsConfig
services.AddSingleton<DatabaseContext>(sp => new DatabaseContext(sp.GetRequiredService<SettingsConfig>()));
// Discord client
services.AddSingleton(_ => new DiscordSocketClient(new DiscordSocketConfig {
GatewayIntents = GatewayIntents.All,
WebSocketProvider = WS4NetProvider.Instance,
AlwaysDownloadUsers = true,
LogLevel = LogSeverity.Info,
DefaultRetryMode = RetryMode.AlwaysRetry,
LogGatewayIntentWarnings = false,
MessageCacheSize = 102
}));
// Interaction service
services.AddSingleton(sp =>
new InteractionService(sp.GetRequiredService<DiscordSocketClient>()));
// Handlers
services.AddSingleton<InteractionHandler>();
services.AddSingleton<Core.Infrastructure.DiscordNet.Handlers.EventHandler>();
// Bot services
services.AddSingleton<AdminService>();
services.AddSingleton<ReactionRolesService>();
services.AddSingleton<LevelingService>();
services.AddSingleton<WaifuService>();
services.AddSingleton<AnimeQuoteService>();
services.AddSingleton<TraceMoeService>();
services.AddSingleton<IEmbedService, EmbedService>();
services.AddSingleton<DanbooruIqdbService>();
services.AddSingleton<SauceNaoService>();
services.AddSingleton<ProfileService>();
services.AddSingleton<ImageFetcher>();
services.AddSingleton<ColorUtils>();
// Providers
services.AddSingleton<IStatusProvider, StatusProvider>();
services.AddSingleton<IEmbedColorProvider, EmbedColorProvider>();
// Http client services
services.AddHttpClient<IApiClient, ApiClient>(client => {
// Some APIs (incl. Danbooru) may reject requests without a User-Agent.
client.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", "LyuzeBot/3.0");
client.DefaultRequestHeaders.TryAddWithoutValidation("Accept", "application/json");
});
// Hosted service for Discord bot lifecycle
services.AddHostedService<DiscordStartupService>();
// Host logging configuration
services.AddLogging(logging => {
logging.ClearProviders();
logging.AddSimpleConsole();
logging.SetMinimumLevel(LogLevel.Warning);
// Suppress Microsoft host lifetime logs (we use our own logging)
logging.AddFilter("Microsoft.Hosting.Lifetime", LogLevel.Warning);
logging.AddFilter("Microsoft.Extensions.Hosting", LogLevel.Warning);
// Logging filters to remove noisy HTTP logs
logging.AddFilter("System.Net.Http.HttpClient.WaifuService.LogicalHandler", LogLevel.Warning);
logging.AddFilter("System.Net.Http.HttpClient.WaifuService.ClientHandler", LogLevel.Warning);
logging.AddFilter("System.Net.Http.HttpClient.AnimeQuoteService.LogicalHandler", LogLevel.Warning);
logging.AddFilter("System.Net.Http.HttpClient.AnimeQuoteService.ClientHandler", LogLevel.Warning);
logging.AddFilter("Microsoft.Extensions.Http", LogLevel.Warning);
logging.AddFilter("Microsoft.Extensions.Http.DefaultHttpClientFactory", LogLevel.Warning);
logging.AddFilter("System.Net.Http.HttpClient.IApiClient.LogicalHandler", LogLevel.Warning);
logging.AddFilter("System.Net.Http.HttpClient.IApiClient.ClientHandler", LogLevel.Warning);
// Allow Lyuze namespace debug logs through ILogger (if any modules still use it)
logging.AddFilter("Lyuze", LogLevel.Debug);
});
})
.Build();
// Run the host - this will start the DiscordStartupService and keep the app running
await host.RunAsync();
}
}