-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathProgram.cs
More file actions
136 lines (120 loc) · 3.84 KB
/
Program.cs
File metadata and controls
136 lines (120 loc) · 3.84 KB
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
using Discord;
using Discord.Interactions;
using Discord.WebSocket;
using FFMpegCore;
using FFMpegCore.Exceptions;
using FFMpegCore.Helpers;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using QuickEdit.Commands;
using QuickEdit.Config;
using QuickEdit.Logger;
using Serilog;
namespace QuickEdit;
internal class Program
{
public static Task Main(string[] args) => new Program().MainAsync(args);
public async Task MainAsync(string[] args)
{
// Configure Serilog first
SerilogConfiguration.ConfigureLogger();
ShowStartMessage();
// Generic Host setup
HostApplicationBuilderSettings hostSettings = new()
{
Args = args,
Configuration = new ConfigurationManager(),
ContentRootPath = AppDomain.CurrentDomain.BaseDirectory
};
try
{
hostSettings.Configuration.AddJsonFile("config.json");
hostSettings.Configuration.AddCommandLine(args);
}
catch (FileNotFoundException)
{
// Can't log the file name using FileNotFoundException.FileName as it's just null
Log.Fatal("Couldn't find file 'config.json' in path: {path}", AppDomain.CurrentDomain.BaseDirectory);
return;
}
catch (Exception e)
{
Log.Fatal("Failed to add config providers:{e}", e);
return;
}
HostApplicationBuilder hostBuilder = Host.CreateApplicationBuilder(hostSettings);
if (!ConfigManager.LoadConfiguration(hostBuilder)) return;
if (!ConfigureServices(hostBuilder.Services)) return;
using var host = hostBuilder.Build();
// Change log level after getting Config
host.Services.GetRequiredService<SerilogConfiguration>().SetLoggingLevelFromConfig();
if (!CheckFFMpegExists()) return;
await host.RunAsync();
}
/// <summary>
/// Configures Dependency Injection Services
/// </summary>
/// <param name="services">The service collection from the builder</param>
/// <returns>True is success, False if failure</returns>
private static bool ConfigureServices(IServiceCollection services)
{
try
{
var socketConfig = new DiscordSocketConfig()
{
GatewayIntents = GatewayIntents.None
};
var interactionServiceConfig = new InteractionServiceConfig()
{
UseCompiledLambda = true,
DefaultRunMode = RunMode.Async
};
services.AddSerilog();
services.AddTransient<SerilogConfiguration>();
services.AddSingleton(socketConfig);
services.AddSingleton(interactionServiceConfig);
services.AddSingleton<DiscordSocketClient>();
services.AddSingleton(x => new InteractionService(x.GetRequiredService<DiscordSocketClient>(), interactionServiceConfig));
services.AddHostedService<InteractionServiceHandler>();
services.AddHostedService<Bot>();
return true;
}
catch (Exception e)
{
Log.Fatal("Failed to configure services: {e}", e);
Environment.ExitCode = 1;
return false;
}
}
private static bool CheckFFMpegExists()
{
try
{
FFMpegHelper.VerifyFFMpegExists(GlobalFFOptions.Current);
Log.Debug("Found FFMpeg");
return true;
}
catch (FFMpegException)
{
Log.Fatal("FFMpeg not found.");
Environment.ExitCode = 1;
return false;
}
catch (Exception e)
{
// It seems that there might be a bug in FFMpegCore, causing VerifyFFMpegExists() to
// fail before it can throw the correct exception, which causes a different exception.
Log.Fatal("FFMpeg verification resulted in a failure: {Message}", e);
Environment.ExitCode = 1;
return false;
}
}
private static void ShowStartMessage()
{
// https://stackoverflow.com/questions/1600962/displaying-the-build-date
var buildVer = typeof(Program).Assembly.GetName().Version?.ToString() ?? "??";
var compileTime = new DateTime(Builtin.CompileTime, DateTimeKind.Utc); // Use a different method maybe
Console.WriteLine($"\u001b[36m ---- QuickEdit ver. {buildVer} - Build Date: {compileTime.ToUniversalTime()} UTC - By HEJOK254 ---- \u001b[0m");
}
}