This repository has been archived by the owner on Jan 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
118 lines (112 loc) · 3.88 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.Threading.Tasks;
using BrikBotCore.Cache;
using BrikBotCore.Extensions;
using BrikBotCore.Services;
using Discord;
using Discord.Commands;
using Discord.Interactions;
using Discord.WebSocket;
using Microsoft.Extensions.DependencyInjection;
using Serilog;
using Serilog.Filters;
using RunMode = Discord.Commands.RunMode;
namespace BrikBotCore
{
public class Program
{
public static void Main()
{
try
{
AppDomain.CurrentDomain.UnhandledException += UnhandledExceptionTrapper;
new Program().MainAsync().GetAwaiter().GetResult();
}
catch (Exception e)
{
Log.Debug($"!!! POSSIBLE UNHANDLED EXCEPTION !!!\n{e.Message} \n {e.StackTrace}", "Red");
}
}
private static void UnhandledExceptionTrapper(object sender, UnhandledExceptionEventArgs e)
{
try
{
Console.WriteLine(e.ExceptionObject.ToString());
}
catch (Exception ex)
{
Log.Debug($"!!! UNHANDLED EXCEPTION !!!\n{ex.Message} \n {ex.StackTrace}", "Red");
}
}
private async Task MainAsync()
{
try
{
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Verbose()
.Enrich.FromLogContext()
.WriteTo.Console(outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss} [{Level:u4}] {Message:lj}{NewLine}{Exception}")
.WriteTo.File("logs/log-.txt", outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss} [{Level:u4}] {Message:lj}{NewLine}{Exception}", rollingInterval: RollingInterval.Day, retainedFileCountLimit: 10, retainedFileTimeLimit: TimeSpan.FromDays(7))
.CreateLogger();
var services = new ServiceCollection();
ConfigureServices(services);
var provider = services.BuildServiceProvider();
await provider.GetRequiredService<CommandHandler>().InitializeAsync();
provider.GetRequiredService<DiscordShardedClient>();
provider.GetRequiredService<InteractionService>();
provider.GetRequiredService<EventService>();
await provider.GetRequiredService<StartupService>().StartAsync(provider, services);
await Task.Delay(-1);
}
catch (Exception e)
{
Log.Debug("{Message} \n {StackTrace}", e.Message, e.StackTrace);
}
}
private void ConfigureServices(IServiceCollection services)
{
services.AddSingleton(new DiscordShardedClient(new DiscordSocketConfig
{
LogLevel = LogSeverity.Verbose,
MessageCacheSize = 0,
TotalShards = null,
DefaultRetryMode = RetryMode.AlwaysFail,
AlwaysDownloadUsers = true,
HandlerTimeout = 3000,
ConnectionTimeout = int.MaxValue,
IdentifyMaxConcurrency = 1,
MaxWaitBetweenGuildAvailablesBeforeReady = 10000,
LargeThreshold = 250,
GatewayIntents = GatewayIntents.Guilds | GatewayIntents.DirectMessages,
UseInteractionSnowflakeDate = true,
UseSystemClock = true,
AlwaysResolveStickers = false,
AlwaysDownloadDefaultStickers = false,
LogGatewayIntentWarnings = true,
DefaultRatelimitCallback = x =>
{
//Log.Debug("[Ratelimit] Endpoint: {Endpoint} - Limit: {Limit} - Remaining: {Remaining} - ResetAfter: {ResetAfter} - isGlobal? {IsGlobal} - Bucket: {Bucket}", x?.Endpoint ?? "Unknown", x?.Limit ?? -1, x?.Remaining ?? -1, x?.ResetAfter ?? TimeSpan.Zero, x?.IsGlobal ?? false, x?.Bucket ?? "Unknown");
return Task.CompletedTask;
}
}));
services.AddSingleton(new CommandService(new CommandServiceConfig
{
DefaultRunMode = RunMode.Async,
LogLevel = LogSeverity.Verbose,
IgnoreExtraArgs = true,
CaseSensitiveCommands = false,
ThrowOnError = true,
SeparatorChar = ' '
}));
services.AddSingleton<EventService>();
services.AddSingleton<StartupService>();
services.AddLazyCache();
services.AddSingleton<Logger>();
services.AddSingleton<MessageSender>();
services.AddSingleton<DataCache>();
services.AddSingleton<InteractionService>();
services.AddSingleton(x => new InteractionService(x.GetRequiredService<DiscordShardedClient>()));
services.AddSingleton<CommandHandler>();
}
}
}