-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
113 lines (86 loc) · 4.25 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
using DSharpPlus;
using DSharpPlus.Entities;
using DSharpPlus.SlashCommands;
using Newtonsoft.Json;
using Microsoft.Extensions.DependencyInjection;
using LLM.Rori.Discord.Data;
using LLM.Rori.Discord.Data.Bot;
using LLM.Rori.Discord.Services;
using LLM.Rori.Discord.Databases;
using LLM.Rori.Discord.Extension;
using LLM.Rori.Discord.Data.Database;
using LLM.Rori.Discord.Commands.Slash;
using LLM.Rori.Discord.Data.Starchild;
using LLM.Rori.Discord.Base.Interfaces;
using LLM.Rori.Discord.Databases.Connectors;
namespace LLM.Rori.Discord
{
internal class Program
{
private class DiscordInfo
{
public DiscordClient Discord;
public DiscordActivity Activity;
public UserStatus Status;
public DiscordInfo(DiscordClient discord, DiscordActivity activity, UserStatus status = UserStatus.Idle) { Discord = discord; Activity = activity; Status = status; }
}
private class DbInfo<TDbConfig>
{
public Config Config;
public TDbConfig DbConfig;
public DbInfo(TDbConfig config) { DbConfig = config; }
}
public const string ConfigFile = "rori_config.json";
static async Task Main(string[] args)
{
//Init and cache localization packets
await new Services.Localization().InitPacketAsync();
//Init database config and data handlers
var profileService = new BotProfileHandler();
//Implemented Example of Database Swap:
//var dbInfo = await GetDatabaseConfig<MySqlDatabaseConfig>("mysql_dbconfig.json");
//IDatabaseHandler<MintyBarData, MintyBarData> dataHandler = new MySqlDataHandler(dbInfo.DbConfig, dbInfo.Config);
var dbInfo = await GetDatabaseConfig<SqliteDatabaseConfig>("sqlite_dbconfig.json");
IDatabaseHandler<User, User> dataHandler = new SqliteDataHandler(dbInfo.DbConfig, dbInfo.Config);
//Init discord client itself and setup activity
var discordInfo = await InitializeDiscord(profileService);
RegisterCommands(discordInfo.Discord, BindServices(profileService, dataHandler, dbInfo.Config));
await discordInfo.Discord.ConnectAsync(discordInfo.Activity, discordInfo.Status);
await Task.Delay(-1);
}
private static async Task<DbInfo<TDbConfig>> GetDatabaseConfig<TDbConfig>(string dbConfigFilename)
{
var rawConfig = await DataGrabber.GrabFromConfigs(dbConfigFilename);
rawConfig.LogStatus(dbConfigFilename);
var dbInfo = new DbInfo<TDbConfig>(JsonConvert.DeserializeObject<TDbConfig>(rawConfig));
var rawMainConfigData = await DataGrabber.GrabFromConfigs(ConfigFile);
dbInfo.Config = JsonConvert.DeserializeObject<Config>(rawMainConfigData);
return dbInfo;
}
private static async Task<DiscordInfo> InitializeDiscord(BotProfileHandler profileService)
{
var client = new DiscordClient(await profileService.SetConfig());
DiscordActivity activity = await profileService.SetStatus(ActivityType.Streaming, "Minty Girl");
return new DiscordInfo(client, activity, UserStatus.Idle);
}
private static ServiceProvider BindServices(
BotProfileHandler profileService, IDatabaseHandler<User, User> dataHandler,
Config mainConfig)
{
ServiceCollection services = new ServiceCollection();
services.AddSingleton(profileService);
services.AddSingleton(dataHandler);
services.AddSingleton(mainConfig);
return services.BuildServiceProvider();
}
private static void RegisterCommands(DiscordClient discord, IServiceProvider services)
{
//Attach slash commands service collection so the services will be injected in slash commands
var slashCommands = new SlashCommandsConfiguration() { Services = services };
var slash = discord.UseSlashCommands(slashCommands);
//Register one-by-one is preffered instead of .RegisterCommands(typeof(Program).Assembly);
slash.RegisterCommands<AboutCommand>();
slash.RegisterCommands<ConnectCommand>();
}
}
}