forked from Alex1911-Jiang/GreenOnions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FrmMain.cs
269 lines (236 loc) · 8.08 KB
/
FrmMain.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
using GreenOnions.BotMain;
using GreenOnions.BotMain.CqHttp;
using GreenOnions.BotMain.MiraiApiHttp;
using GreenOnions.Interface;
using GreenOnions.Utility;
using GreenOnions.Utility.Helper;
namespace GreenOnions.BotManagerWindows
{
public partial class FrmMain : Form
{
private WebBrowserForm webBrowserForm;
private bool _connecting;
public FrmMain()
{
InitializeComponent();
webBrowserForm = new WebBrowserForm();
EventHelper.GetDocumentByBrowserEvent += webBrowserForm.GetDocument;
#region -- 读取配置 --
try
{
if (!File.Exists("config.json"))
{
MessageBox.Show("初次使用本机器人,请先配置相关参数。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
OpenSetting();
}
txbQQ.Text = BotInfo.Config.QQId.ToString();
txbIP.Text = BotInfo.Config.IP;
txbPort.Text = BotInfo.Config.Port.ToString();
txbVerifyKey.Text = BotInfo.Config.VerifyKey;
}
catch (Exception ex)
{
MessageBox.Show($"读取配置发生异常,{ex.Message},请删除应用目录下的config.json和cache.json文件后重启应用。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
#endregion -- 读取配置 --
}
protected override void OnShown(EventArgs e)
{
base.OnShown(e);
Task.Run(() =>
{
int iLoadCount = PluginManager.Load();
if (iLoadCount > 0)
Invoke(new Action(() => btnPlugins.Text = $"插件列表({iLoadCount})"));
//自动连接到机器人平台
if (BotInfo.Config.AutoConnectEnabled)
{
Task.Delay(BotInfo.Config.AutoConnectDelay * 1000).Wait();
WorkingTimeRecorder.DoWork = true;
if (BotInfo.Config.AutoConnectProtocol == 0)
ConnectToMiraiApiHttp();
else
ConnectToCqHttp();
}
});
}
protected override void OnSizeChanged(EventArgs e)
{
base.OnSizeChanged(e);
if (WindowState == FormWindowState.Minimized)
{
if (chkMinimizeToTray.Checked)
{
Hide();
ShowInTaskbar = false;
notifyIcon.Visible = true;
}
}
}
private void ConnectToPlatform(int platform)
{
switch (platform)
{
case 0:
ConnectToMiraiApiHttp();
break;
case 1:
ConnectToCqHttp();
break;
}
}
public void ConnectToMiraiApiHttp(long qqId, string ip, ushort port, string verifyKey)
{
try
{
MiraiApiHttpMain.Connect(qqId, ip, port, verifyKey, (bConnect, nickNameOrErrorMessage) => Invoke(new Action(() => Connecting(bConnect, qqId, ip, port, verifyKey, nickNameOrErrorMessage, 0, "mirai-api-http"))));
}
catch (Exception ex)
{
LogHelper.WriteErrorLogWithUserMessage("连接到mirai-api-http发生异常", ex);
MessageBox.Show("连接mirai-api-http失败" + ex.Message);
}
_connecting = false;
}
public void ConnectToCqHttp(long qqId, string ip, ushort port, string verifyKey)
{
try
{
CqHttpMain.Connect(qqId, ip, port, verifyKey, (bConnect, nickNameOrErrorMessage) => Invoke(new Action(() => Connecting(bConnect, qqId, ip, port, verifyKey, nickNameOrErrorMessage, 1, "cqhttp"))));
}
catch (Exception ex)
{
LogHelper.WriteErrorLogWithUserMessage("连接到cqhttp发生异常", ex);
MessageBox.Show("连接cqhttp失败" + ex.Message);
}
_connecting = false;
}
private void btnDeconnect_Click(object? sender, EventArgs e)
{
WorkingTimeRecorder.DoWork = false;
Disconnect();
}
private void Disconnect()
{
TextReader sr = new StringReader("exit");
Console.SetIn(sr);
btnConnectToMiraiApiHttp.Click -= btnDeconnect_Click;
btnConnectToMiraiApiHttp.Click += btnConnectToMiraiApiHttp_Click;
btnConnectToCqHttp.Click -= btnDeconnect_Click;
btnConnectToCqHttp.Click += btnConnectToCqHttp_Click;
}
private void btnConnectToMiraiApiHttp_Click(object? sender, EventArgs e)
{
WorkingTimeRecorder.DoWork = true;
ConnectToMiraiApiHttp();
}
private void ConnectToMiraiApiHttp()
{
if (!_connecting)
{
if (CheckInfo())
{
_connecting = true;
ConnectToMiraiApiHttp(Convert.ToInt64(txbQQ.Text), txbIP.Text, Convert.ToUInt16(txbPort.Text), txbVerifyKey.Text);
}
}
}
private void btnConnectToCqHttp_Click(object? sender, EventArgs e)
{
WorkingTimeRecorder.DoWork = true;
ConnectToCqHttp();
}
private void ConnectToCqHttp()
{
if (!_connecting)
{
if (CheckInfo())
{
_connecting = true;
ConnectToCqHttp(Convert.ToInt64(txbQQ.Text), txbIP.Text, Convert.ToUInt16(txbPort.Text), txbVerifyKey.Text);
}
}
}
private void btnBotSettings_Click(object sender, EventArgs e) => OpenSetting();
private void OpenSetting() => new FrmAppSetting().ShowDialog();
private void notifyIcon_MouseDoubleClick(object sender, MouseEventArgs e)
{
Visible = true;
ShowInTaskbar = true;
WindowState = FormWindowState.Normal;
notifyIcon.Visible = false;
}
private void Connecting(bool bConnect, long qqId, string ip, ushort port, string verifyKey, string nickNameOrErrorMessage, int platform, string protocolName)
{
if (bConnect)
{
lblState.Text = $"连接状态: 已连接到{protocolName}, 登录昵称:{nickNameOrErrorMessage}";
lblState.ForeColor = Color.Black;
btnConnectToMiraiApiHttp.Text = "断开连接";
btnConnectToMiraiApiHttp.Click -= btnConnectToMiraiApiHttp_Click;
btnConnectToMiraiApiHttp.Click += btnDeconnect_Click;
btnConnectToCqHttp.Text = "断开连接";
btnConnectToCqHttp.Click -= btnConnectToCqHttp_Click;
btnConnectToCqHttp.Click += btnDeconnect_Click;
notifyIcon.Text = $"葱葱机器人:{nickNameOrErrorMessage}";
BotInfo.Config.QQId = qqId;
BotInfo.Config.IP = ip;
BotInfo.Config.Port = port;
BotInfo.Config.VerifyKey = verifyKey;
BotInfo.SaveConfigFile();
WorkingTimeRecorder.StartRecord(platform, ConnectToPlatform, Disconnect);
webBrowserForm.Show();
}
else if (nickNameOrErrorMessage is null) //连接失败且没有异常
{
MessageBox.Show($"连接失败,请检查{protocolName}是否已经正常启动并已配置IP端口相关参数, 以及机器人QQ是否成功登录。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else //发生异常或主动断开连接
{
btnConnectToMiraiApiHttp.Text = "连接到mirai-api-http";
btnConnectToCqHttp.Text = "连接到cqhttp";
lblState.Text = $"连接状态: 未连接到机器人平台";
lblState.ForeColor = Color.Red;
notifyIcon.Text = $"葱葱机器人";
if (nickNameOrErrorMessage.Length > 0) //发生异常
MessageBox.Show("连接失败," + nickNameOrErrorMessage, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private bool CheckInfo()
{
if (BotInfo.IsLogin)
{
return false;
}
if (string.IsNullOrEmpty(txbQQ.Text))
{
MessageBox.Show("请先输入机器人QQ号。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return false;
}
if (string.IsNullOrEmpty(txbIP.Text))
{
MessageBox.Show("请先输入机器人平台 IP。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return false;
}
if (string.IsNullOrEmpty(txbPort.Text))
{
MessageBox.Show("请先输入机器人平台端口号。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return false;
}
if (string.IsNullOrEmpty(txbVerifyKey.Text))
{
MessageBox.Show("请先输入机器人平台连接凭证。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return false;
}
BotInfo.Config.QQId = Convert.ToInt64(txbQQ.Text);
BotInfo.Config.IP = txbIP.Text;
BotInfo.Config.Port = Convert.ToUInt16(txbPort.Text);
BotInfo.Config.VerifyKey = txbVerifyKey.Text;
return true;
}
private void btnPlugins_Click(object sender, EventArgs e)
{
new FrmPlugins().ShowDialog();
}
}
}