-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
187 lines (156 loc) · 6.38 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Serilog;
using Serilog.Core;
using Discord;
using Discord.Commands;
using Discord.WebSocket;
using DiscordVerifyBot.Core.Services;
using DiscordVerifyBot.Core.Handlers;
using DiscordVerifyBot.Core.Logging;
using DiscordVerifyBot.Resources;
using DiscordVerifyBot.Resources.Database;
namespace DiscordVerifyBot
{
class Program
{
private readonly DiscordSocketClient _client;
private readonly CommandService _commandService;
private readonly IServiceProvider _serviceProvider;
private readonly CommandHandler _commandHandler;
internal static ManualResetEvent _quitEvent = new ManualResetEvent(false);
public Program()
{
Console.CancelKeyPress += (sender, eArgs) =>
{
_quitEvent.Set();
eArgs.Cancel = true;
};
//Creates and/or Updates the database when the program is strated
using (var DbContext = new SQLiteDatabaseContext())
{
DbContext.Database.Migrate();
//int maxRetries = 3;
//for(int i = 0; i < maxRetries; i++)
//{
// try
// {
// DbContext.Database.Migrate();
// break;
// }
// catch (System.NotSupportedException e)
// {
// DbContext.Database.EnsureDeleted();
// }
//}
}
Settings settings;
using (var DH = new SettingsDataHandler())
{
settings = DH.GetSettings();
#region Logger Creation
//Gets and converts Log Levels to match between Serilog and integrated Discord.Net Logger
var LogLevelSerilog = Serilog.Events.LogEventLevel.Information;
var LogLevelDiscord = LogSeverity.Info;
if (Enum.IsDefined(typeof(Serilog.Events.LogEventLevel), settings.LogLevel))
{
LogLevelSerilog = (Serilog.Events.LogEventLevel)settings.LogLevel;
new LogLevelConverter().SerilogToDiscordNet(LogLevelSerilog, out LogLevelDiscord);
}
LoggingLevelSwitch levelSwitch = new LoggingLevelSwitch()
{
MinimumLevel = LogLevelSerilog
};
var loggerConfiguration = new LoggerConfiguration()
.MinimumLevel.Is(LogLevelSerilog)
.Enrich.With(new ThreadIdEnricher())
.Enrich.With(new ProcessIdEnricher())
.WriteTo.Console(
outputTemplate: "{Timestamp:HH:mm} [{Level}] [{ProcessId}-{ThreadId}] {Message}{NewLine}{Exception}");
if (Convert.ToBoolean(settings.RollingLogRetainedFiles))
{
loggerConfiguration.WriteTo.File(
path: DH.GetLogFilePath(),
rollingInterval: RollingInterval.Day,
retainedFileCountLimit: settings.RollingLogRetainedFiles,
outputTemplate: "[{Timestamp:HH:mm:ss} {Level}] {Message}{NewLine}{Exception}");
}
Log.Logger = loggerConfiguration.CreateLogger();
Log.Debug("Logger Created");
#endregion
_client = new DiscordSocketClient(new DiscordSocketConfig
{
LogLevel = LogLevelDiscord
});
_commandService = new CommandService(new CommandServiceConfig
{
CaseSensitiveCommands = false,
DefaultRunMode = RunMode.Async,
LogLevel = LogLevelDiscord
});
}
_serviceProvider = new ServiceProviderFactory(_client, _commandService).Build();
_commandHandler = new CommandHandler(
client: _client,
commandService: _commandService,
serviceProvider: _serviceProvider,
replyService: _serviceProvider.GetRequiredService<IReplyService>()
);
_client.Log += OnClientLogAsync;
_client.Ready += OnClientReadyAsync;
}
private async Task OnClientReadyAsync()
{
await _client.SetGameAsync("your commands.", null, ActivityType.Listening);
}
private async Task OnClientLogAsync(LogMessage message)
{
switch (message.Severity)
{
case LogSeverity.Critical:
Log.Fatal(message.Message);
break;
case LogSeverity.Error:
Log.Error(message.Message);
break;
case LogSeverity.Warning:
Log.Warning(message.Message);
break;
case LogSeverity.Info:
Log.Information(message.Message);
break;
case LogSeverity.Debug:
Log.Debug(message.Message);
break;
case LogSeverity.Verbose:
Log.Verbose(message.Message);
break;
}
//await _serviceProvider.GetRequiredService<ILoggerService>().LogAsync(Message: message.Message, Source: message.Source);
}
public static void Main()
=> new Program().MainAsync().GetAwaiter().GetResult();
public async Task MainAsync()
{
await _commandHandler.SetupAsync();
//Fetches the Bot Token From File
string Bot_Token = "";
using(var DH = new SettingsDataHandler())
{
Bot_Token = DH.GetSettings().BotToken;
}
//Awaits Bot Login / Start Confirmation
await _client.LoginAsync(TokenType.Bot, Bot_Token);
await _client.StartAsync();
_quitEvent.WaitOne();
Log.Logger.Information(
"User initiated shutdown.");
await _client.LogoutAsync();
await _client.StopAsync();
await Task.Delay(millisecondsDelay: 1000);
}
}
}