-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
118 lines (100 loc) · 4.42 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
using System;
using System.Linq;
using System.Threading.Tasks;
using Discord.WebSocket;
using Hangfire;
using Hangfire.AspNetCore;
using Hangfire.Redis;
using Microsoft.Extensions.DependencyInjection;
using PochinkiBot.Background;
using PochinkiBot.Client;
using PochinkiBot.Client.Commands;
using PochinkiBot.Client.Commands.DailyPidor.Scripts;
using PochinkiBot.Configuration;
using PochinkiBot.Misc;
using PochinkiBot.Repositories.Implementations;
using PochinkiBot.Repositories.Interfaces;
using Serilog;
using Serilog.Events;
namespace PochinkiBot
{
public static class Program
{
private const string DefaultConfigFileName = "config.json";
private const string ConfigArgKey = "--c";
public static async Task Main(string[] args)
{
var provider = ConfigureServiceProvider(args);
var redis = provider.GetRequiredService<IRedisConnectionProvider>();
var config = provider.GetRequiredService<BotConfig>();
if (!await redis.TryConnect())
{
Console.WriteLine("Redis is not available.");
return;
}
GlobalConfiguration.Configuration
.UseDefaultActivator()
.UseRedisStorage(redis.Connection,
new RedisStorageOptions
{
Db = config.RedisConfiguration.JobStorageDatabase
})
.UseSerilogLogProvider()
.UseActivator(new AspNetCoreJobActivator(provider.GetRequiredService<IServiceScopeFactory>()));
var jobHandler = provider.GetRequiredService<BackgroundJobHandler>();
jobHandler.Run();
var client = provider.GetRequiredService<DiscordClient>();
await client.WaitForShutdown();
}
private static IServiceProvider ConfigureServiceProvider(string[] args)
{
var configFileName = GetConfigFileNameFromArgs(args);
var services = new ServiceCollection();
services.AddSingleton(new BotConfig(configFileName));
services.AddSingleton<IRedisConnectionProvider, RedisConnectionProvider>();
services.AddSingleton<IRedisDatabaseProvider, RedisDatabaseProvider>();
services.AddSingleton<DiscordClient>();
services.AddSingleton<IBackgroundJobClient, BackgroundJobClient>();
services.AddSingleton<ICommandCollection, CommandCollection>();
services.AddSingleton<ICommandArgsParser, CommandArgsParser>();
services.Scan(s => s.FromApplicationDependencies()
.AddClasses(c => c.AssignableTo<IRedisStore>())
.AsImplementedInterfaces()
.WithSingletonLifetime());
services.Scan(s => s.FromApplicationDependencies()
.AddClasses(c => c.AssignableTo<IBackgroundJob>())
.AsSelfWithInterfaces()
.WithSingletonLifetime());
services.Scan(s => s.FromApplicationDependencies()
.AddClasses(c => c.AssignableTo<IBotCommand>().WithAttribute<CommandAttribute>())
.AsSelfWithInterfaces()
.WithSingletonLifetime());
services.Scan(s => s.FromApplicationDependencies()
.AddClasses(c => c.AssignableTo<IDailyPidorScript>())
.AsSelfWithInterfaces()
.WithSingletonLifetime());
services.AddSingleton<IBotDeveloperProvider, BotDeveloperProvider>();
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.MinimumLevel.Override("Hangfire", LogEventLevel.Information)
.WriteTo.Console()
.CreateLogger();
services.AddSingleton(new DiscordSocketClient(new DiscordSocketConfig
{
AlwaysDownloadUsers = true,
MessageCacheSize = 250
}));
services.AddScoped<BackgroundJobHandler>();
var provider = services.BuildServiceProvider();
services.AddSingleton(provider);
return provider;
}
private static string GetConfigFileNameFromArgs(string[] args)
{
var configFile = args.SkipWhile(a => a != ConfigArgKey).Skip(1).FirstOrDefault();
return configFile == null || !configFile.EndsWith(".json")
? DefaultConfigFileName
: configFile;
}
}
}