-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProgram.cs
114 lines (98 loc) · 4.09 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
using System.Net;
using Discord;
using Discord.Interactions;
using Discord.Net.Rest;
using Discord.Net.WebSockets;
using Discord.WebSocket;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using UnityBuilderDiscordBot.Services;
using UnityBuilderDiscordBot.Utilities;
namespace UnityBuilderDiscordBot;
internal class Program
{
public static Task<int> Main(string[] args)
{
return new Program().MainAsync();
}
public async Task<int> MainAsync()
{
var title = "Unity Builder Discord Bot";
// 计算装饰框的长度
var boxLength = title.Length + 4;
// 输出顶部边框
Console.WriteLine(new string('*', boxLength));
// 输出标题行
Console.WriteLine("* " + title + " *");
Console.WriteLine("* by Shepherd Zhu *");
// 输出底部边框
Console.WriteLine(new string('*', boxLength));
try
{
ConfigurationUtility.Initialize();
}
catch (Exception ex)
{
Console.WriteLine($"Failed to load appsettings.json! \n{ex}");
return -1;
}
var hostBuilder = new HostBuilder()
.ConfigureServices((hostContext, services) =>
{
services.AddLogging(builder => builder.AddConsole());
services.AddSingleton(serviceProvider =>
{
var config = new DiscordSocketConfig
{
RestClientProvider = DefaultRestClientProvider.Create(true),
WebSocketProvider = DefaultWebSocketProvider.Create(WebRequest.DefaultWebProxy),
GatewayIntents = GatewayIntents.All
};
return new DiscordSocketClient(config);
}); // Add the discord client to services
services.AddSingleton<InteractionService>(); // Add the interaction service to services
services.AddHostedService<CredentialServiceManager>();
services.AddHostedService<FileTransferServiceManager>();
services.AddHostedService<UnityEditorService>(); // Add the Unity Editor service
services.AddHostedService<InteractionHandlingService>(); // Add the slash command handler
services.AddHostedService<DiscordStartupService>(); // Add the discord startup service
// services.AddHostedService<SshCredentialService>(); // Add the SSH
// services.AddHostedService<SftpFileTransferService>(); // Add the SFTP
});
await hostBuilder.RunConsoleAsync();
return 0;
}
// Example of a logging handler. This can be re-used by addons
// that ask for a Func<LogMessage, Task>.
public static Task Log(LogMessage message)
{
switch (message.Severity)
{
case LogSeverity.Critical:
case LogSeverity.Error:
Console.ForegroundColor = ConsoleColor.Red;
break;
case LogSeverity.Warning:
Console.ForegroundColor = ConsoleColor.Yellow;
break;
case LogSeverity.Info:
Console.ForegroundColor = ConsoleColor.White;
break;
case LogSeverity.Verbose:
case LogSeverity.Debug:
Console.ForegroundColor = ConsoleColor.DarkGray;
break;
}
Console.WriteLine(
$"{DateTime.Now,-19} [{message.Severity,8}] {message.Source}: {message.Message} {message.Exception}");
Console.ResetColor();
// If you get an error saying 'CompletedTask' doesn't exist,
// your project is targeting .NET 4.5.2 or lower. You'll need
// to adjust your project's target framework to 4.6 or higher
// (instructions for this are easily Googled).
// If you *need* to run on .NET 4.5 for compat/other reasons,
// the alternative is to 'return Task.Delay(0);' instead.
return Task.CompletedTask;
}
}