-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProgram.cs
118 lines (97 loc) · 3.81 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 EleCho.GoCqHttpSdk;
using MiraiGo_C;
using MiraiGoC;
using NullLib.CommandLine;
using System.Diagnostics.CodeAnalysis;
using System.Text.Json;
using System.Xml;
namespace MiraiGoC
{
class Program
{
static Program()
{
// 启用 ANSI 转义序列
Util.EnableAnsiSequence();
}
static string AppConfigPath = "appconfig.json";
static char g = Path.DirectorySeparatorChar;
static object o = new object();
public readonly static SimpleLogger Logger = new SimpleLogger();
private static bool LoadConfig([NotNullWhen(true)] out AppConfig? config)
{
JsonSerializerOptions options = new JsonSerializerOptions
{
WriteIndented = true
};
if (!File.Exists(AppConfigPath))
{
Logger.LogError("未找到配置文件, 已创建. 请编辑后重启");
using Stream wfs = File.Create(AppConfigPath);
JsonSerializer.Serialize(wfs, new AppConfig(), options);
config = null;
return false;
}
using Stream rfs = File.OpenRead(AppConfigPath);
config = JsonSerializer.Deserialize<AppConfig>(rfs, options);
if (config == null)
{
Logger.LogError("配置文件解析失败, 请检查配置文件");
return false;
}
return true;
}
static async Task Main()
{
Console.WriteLine(" ____ _ ____ __ ");
Console.WriteLine(" / __ \\_____(_) __ )____ / /_");
Console.WriteLine(" / / / / ___/ / __ / __ \\/ __/");
Console.WriteLine("/ /_/ / / / / /_/ / /_/ / /_ ");
Console.WriteLine("\\____/_/ /_/_____/\\____/\\__/ \n");
Console.WriteLine("Version: 1.1\n");
try
{
if (!LoadConfig(out AppConfig? config))
return;
string _ws
= $"ws://{config.Host}:{config.Port}";
Logger.LogInfo($"正在连接至 {_ws}");
CqWsSession session = new CqWsSession(new CqWsSessionOptions()
{
BaseUri = new Uri(_ws),
UseApiEndPoint = true,
UseEventEndPoint = true,
});
await session.StartAsync();
Logger.LogInfo("Bot启动完成!");
Logger.LogInfo("正在加载插件...");
PluginLoader.LoadPlugins(session);
Logger.LogInfo("正在启动信息处理器...");
session.UsePlugin(new Receiver());
Logger.LogInfo("准备就绪");
CommandObject<ManageCommands> cmdobj = new CommandObject<ManageCommands>();
while (true)
{
string? stdinput = Console.ReadLine();
if (stdinput == null) // 当没有可用的输入时, 停止程序
return;
// 执行指令 (解析是自动的)
if (!cmdobj.TryExecuteCommand(stdinput, out _))
{
Logger.LogError("无效的命令. 要获取帮助请输入help.");
}
}
}
catch (Exception ex)
{
Logger.LogError($"连接至Bot时失败: {ex.Message}");
Console.WriteLine("按下 回车键 退出程序...");
// 只有按下回车键才会退出, 且按别的键不会在屏幕中显示
while (Console.ReadKey(true).Key != ConsoleKey.Enter)
{
// pass
}
}
}
}
}