-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
109 lines (92 loc) · 4.35 KB
/
Program.cs
File metadata and controls
109 lines (92 loc) · 4.35 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
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Serilog;
using System.Reflection;
using TreePassBot.Data;
using TreePassBot.Exceptions;
using TreePassBot.Handlers;
using TreePassBot.Handlers.Commands;
using TreePassBot.Handlers.MessageHandler;
using TreePassBot.Handlers.MessageHandler.Handlers;
using TreePassBot.Models;
using TreePassBot.Services;
using TreePassBot.Services.Interfaces;
using TreePassBot.Utils;
namespace TreePassBot;
#nullable disable
internal static class Program
{
public static IHost AppHost { get; private set; }
public static string[] InArgs { get; private set; }
private static IHost Init()
{
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(new ConfigurationManager()
.AddJsonFile("appsettings.json")
.Build())
.CreateLogger();
try
{
Log.Information("Starting QQBot host...");
var host = Host.CreateDefaultBuilder(InArgs)
.UseSerilog((context, services, configuration) =>
configuration
.ReadFrom.Configuration(context.Configuration)
.ReadFrom.Services(services)
.Enrich.FromLogContext())
.ConfigureServices(((hostContext, services) =>
{
services
// Data Store
.Configure<BotConfig>(hostContext.Configuration.GetSection("BotConfig"))
.AddSingleton<JsonDataStore>()
// Services
.AddScoped<IUserService, UserService>()
.AddScoped<IAuditService, AuditService>()
.AddScoped<IMessageService, MessageService>()
// Command Dispatcher
.AddCommandModules(Assembly.GetExecutingAssembly())
.AddSingleton<CommandDispatcher>()
// Factory
.AddSingleton<HandlerLinkNodeFactory>()
// Event Handlers
.AddMessageModules(Assembly.GetExecutingAssembly())
.AddSingleton((provider =>
{
var logger = provider.GetRequiredService<ILogger<MessageHandlerDispatcher>>();
var dispatcher = new MessageHandlerDispatcher(logger, provider);
dispatcher.UseHandler<UriBlockerMessageHandler>()
.UseHandler<AdminCommandMessageHandler>()
.UseHandler<AuditCommandMessageHandler>();
return dispatcher;
}))
.AddSingleton<GroupMessageEventHandler>()
.AddSingleton<GroupRequestEventHandler>()
.AddSingleton<GroupMemberEventHandler>()
.AddSingleton<PrivateMessageEventHandler>()
.AddSingleton<CatchUnhandledException>()
// Utils
.AddSingleton<PasscodeGeneratorUtil>()
.AddSingleton<ArgumentsSpiliterUtil>()
// Host
.AddHostedService<QqBotService>();
}))
.Build();
Log.Information("QQBot host started successfully");
return host;
}
catch (Exception ex)
{
Log.Fatal(ex, "Host rerminated unexpectedly");
throw;
}
}
private static async Task Main(string[] args)
{
InArgs = args;
AppHost = Init();
await AppHost.RunAsync();
}
}