-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDevCommuBot.cs
123 lines (113 loc) · 4.43 KB
/
DevCommuBot.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
using System;
using System.Threading.Tasks;
using DevCommuBot.Data;
using DevCommuBot.Services;
using Discord.Commands;
using Discord.Interactions;
using Discord.WebSocket;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Http;
using Serilog;
namespace DevCommuBot
{
internal class DevCommuBot
{
private IConfigurationRoot config;
public async Task StartAsync()
{
config = new ConfigurationBuilder()
.SetBasePath(AppContext.BaseDirectory)
.AddJsonFile(path: "config.json").Build();
var services = new ServiceCollection()
.AddSingleton(new DiscordSocketClient(new()
{
LogLevel = Discord.LogSeverity.Verbose,
AlwaysDownloadUsers = true,
MessageCacheSize = 100,
GatewayIntents = Discord.GatewayIntents.All
}))
.AddSingleton(config)
.AddSingleton(new CommandService(new()
{
DefaultRunMode = Discord.Commands.RunMode.Async,
LogLevel = Discord.LogSeverity.Verbose,
CaseSensitiveCommands = false,
ThrowOnError = false,
}))
.AddSingleton<CommandHandler>()
.AddSingleton<StartupService>()
.AddSingleton<LoggerService>()
.AddSingleton<UtilService>()
.AddSingleton<GuildService>()
.AddSingleton<ForumService>()
.AddSingleton<DataService>()
.AddDbContext<DataContext>()
.AddSingleton<PointService>()
.AddSingleton<StarboardService>()
.AddSingleton<InteractionService>()
.AddSingleton<ApexStatsService>();
ConfigureServices(services);
var serviceProvider = services.BuildServiceProvider();
serviceProvider.GetRequiredService<LoggerService>();
//Start bot
await serviceProvider.GetRequiredService<StartupService>().StartAsync();
serviceProvider.GetRequiredService<CommandHandler>();
serviceProvider.GetRequiredService<GuildService>();
serviceProvider.GetRequiredService<ForumService>();
serviceProvider.GetRequiredService<StarboardService>();
await Task.Delay(-1);
}
private void ConfigureServices(IServiceCollection services)
{
//Add SeriLog
services.AddLogging((configure) => configure.AddSerilog());
//Remove default HttpClient logging as it is extremely verbose
services.RemoveAll<IHttpMessageHandlerBuilderFilter>();
//Configure logging level
var logLevel = "debug";
var level = Serilog.Events.LogEventLevel.Error;
if (!string.IsNullOrEmpty(logLevel))
{
switch (logLevel.ToLower())
{
case "error":
{
level = Serilog.Events.LogEventLevel.Error;
break;
}
case "info":
{
level = Serilog.Events.LogEventLevel.Information;
break;
}
case "debug":
{
level = Serilog.Events.LogEventLevel.Debug;
break;
}
case "crit":
{
level = Serilog.Events.LogEventLevel.Fatal;
break;
}
case "warn":
{
level = Serilog.Events.LogEventLevel.Warning;
break;
}
case "trace":
{
level = Serilog.Events.LogEventLevel.Debug;
break;
}
}
}
Log.Logger = new LoggerConfiguration()
.WriteTo.Console()
.MinimumLevel.Is(level)
.CreateLogger();
}
}
}