-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainForm.cs
More file actions
498 lines (407 loc) · 14.4 KB
/
MainForm.cs
File metadata and controls
498 lines (407 loc) · 14.4 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
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
using YTLiveChatFilter.Common;
using YTLiveChatFilter.Extensions;
using YTLiveChatFilter.Properties;
namespace YTLiveChatFilter;
public partial class MainForm : Form
{
public MainForm(IHttpClientFactory httpClientFactory)
{
InitializeComponent();
SharedHttpClientFactory = httpClientFactory;
}
private void MainForm_Load(object sender, EventArgs e)
{
CustomInit();
// TODO: 2025/4/23 暫時先停用版本檢查。
//CheckAppVersion();
}
private void BtnSelectClientSecretFile_Click(object sender, EventArgs e)
{
try
{
OpenFileDialog openFileDialog = new()
{
Title = "選擇 OAuth 用戶端檔案",
Filter = "JSON|*.json",
FilterIndex = 0,
FileName = "client_secret.json"
};
DialogResult dialogResult = openFileDialog.ShowDialog();
if (dialogResult == DialogResult.OK)
{
string filePath = openFileDialog.FileName;
if (!string.IsNullOrEmpty(value: filePath) && File.Exists(path: filePath))
{
TBClientSecretFilePath.InvokeIfRequired(action: () =>
{
TBClientSecretFilePath.Text = filePath;
});
}
else
{
MessageBox.Show(
text: "請選擇實際存在的檔案。",
caption: Text,
buttons: MessageBoxButtons.OK,
icon: MessageBoxIcon.Error);
}
}
}
catch (Exception ex)
{
CustomFunction.WriteLog(
control: TBLog,
message: ex.Message);
}
}
private void TBClientSecretFilePath_TextChanged(object sender, EventArgs e)
{
TBClientSecretFilePath.InvokeIfRequired(action: () =>
{
string value = TBClientSecretFilePath.Text;
if (Settings.Default.ClientSecretPath != value)
{
Settings.Default.ClientSecretPath = value;
Settings.Default.Save();
CustomFunction.WriteLog(
control: TBLog,
message: "已更新 OAuth 用戶端檔案的路徑。");
}
});
}
private void CBUseOAuth_CheckedChanged(object sender, EventArgs e)
{
CBUseOAuth20.InvokeIfRequired(action: () =>
{
bool value = CBUseOAuth20.Checked;
if (Settings.Default.UseOAuth20 != value)
{
Settings.Default.UseOAuth20 = value;
Settings.Default.Save();
string status = value ? "勾選" : "取消勾選";
CustomFunction.WriteLog(
control: TBLog,
message: $"已{status}使用 OAuth 2.0 用戶端 ID。");
}
TBAPIKey.InvokeIfRequired(action: () =>
{
TBAPIKey.Enabled = !value;
});
TBClientSecretFilePath.InvokeIfRequired(action: () =>
{
TBClientSecretFilePath.Enabled = value;
});
BtnSelectClientSecretFile.InvokeIfRequired(action: () =>
{
BtnSelectClientSecretFile.Enabled = value;
});
CBBanTemporary.InvokeIfRequired(action: () =>
{
CBBanTemporary.Enabled = value;
});
CBBanPermanent.InvokeIfRequired(action: () =>
{
CBBanPermanent.Enabled = value;
});
CBDeleteMessage.InvokeIfRequired(action: () =>
{
CBDeleteMessage.Enabled = value;
});
NUPBanDuration.InvokeIfRequired(action: () =>
{
NUPBanDuration.Enabled = value;
});
if (!value)
{
string message = "若不勾選「使用 OAuth 2.0 用戶端 ID」選項," +
"將無法使用「暫時停用使用者」、「隱藏使用者」以及「刪除訊息」等功能。";
MessageBox.Show(
text: message,
caption: Text,
buttons: MessageBoxButtons.OK,
icon: MessageBoxIcon.Information);
}
});
}
private void TBAPIKey_TextChanged(object sender, EventArgs e)
{
TBAPIKey.InvokeIfRequired(action: () =>
{
string value = TBAPIKey.Text;
if (Settings.Default.APIKey != value)
{
Settings.Default.APIKey = value;
Settings.Default.Save();
CustomFunction.WriteLog(
control: TBLog,
message: $"已更新 API 金鑰。");
}
});
}
private void CBBanTemporary_CheckedChanged(object sender, EventArgs e)
{
CBBanTemporary.InvokeIfRequired(action: () =>
{
bool value = CBBanTemporary.Checked;
if (Settings.Default.BanTemporary != value)
{
Settings.Default.BanTemporary = value;
Settings.Default.Save();
string status = value ? "啟用" : "停用";
CustomFunction.WriteLog(
control: TBLog,
message: $"已{status}暫時停用使用者。");
}
});
}
private void CBBanPermanent_CheckedChanged(object sender, EventArgs e)
{
CBBanPermanent.InvokeIfRequired(action: () =>
{
bool value = CBBanPermanent.Checked;
if (Settings.Default.BanPermanent != value)
{
Settings.Default.BanPermanent = value;
Settings.Default.Save();
string status = value ? "啟用" : "停用";
CustomFunction.WriteLog(
control: TBLog,
message: $"已{status}隱藏使用者。");
}
});
}
private void CBDeleteMessage_CheckedChanged(object sender, EventArgs e)
{
CBDeleteMessage.InvokeIfRequired(action: () =>
{
bool value = CBDeleteMessage.Checked;
if (Settings.Default.DeleteMessage != value)
{
Settings.Default.DeleteMessage = value;
Settings.Default.Save();
string status = value ? "啟用" : "停用";
CustomFunction.WriteLog(
control: TBLog,
message: $"已{status}刪除訊息。");
}
});
}
private void NUPBanDuration_ValueChanged(object sender, EventArgs e)
{
NUPBanDuration.InvokeIfRequired(action: () =>
{
int value = Convert.ToInt32(value: NUPBanDuration.Value);
if (Settings.Default.BanDuration != value)
{
Settings.Default.BanDuration = value;
Settings.Default.Save();
CustomFunction.WriteLog(
control: TBLog,
message: $"已更新暫時停用秒數,更新後為暫時停用 {value} 秒。");
}
});
}
private async void BtnStart_Click(object? sender, EventArgs? e)
{
try
{
SetControlState(enable: false);
SharedCancellationTokenSource = new();
SharedCancellationToken = SharedCancellationTokenSource.Token;
if (SharedCancellationToken.HasValue)
{
SharedCancellationToken.Value.ThrowIfCancellationRequested();
}
bool banTemporary = false,
banPermanent = false,
deleteMessage = false,
useOAuth20 = false;
int banDuration = 300;
string apiKey = string.Empty,
clientSecretFilePath = string.Empty,
videoID = string.Empty;
#region 取值
CBUseOAuth20.InvokeIfRequired(action: () =>
{
useOAuth20 = CBUseOAuth20.Checked;
});
TBAPIKey.InvokeIfRequired(action: () =>
{
apiKey = TBAPIKey.Text;
});
TBClientSecretFilePath.InvokeIfRequired(action: () =>
{
clientSecretFilePath = TBClientSecretFilePath.Text;
});
TBVideoID.InvokeIfRequired(action: () =>
{
videoID = TBVideoID.Text;
});
CBBanTemporary.InvokeIfRequired(action: () =>
{
banTemporary = CBBanTemporary.Checked;
});
CBBanPermanent.InvokeIfRequired(action: () =>
{
banPermanent = CBBanPermanent.Checked;
});
CBDeleteMessage.InvokeIfRequired(action: () =>
{
deleteMessage = CBDeleteMessage.Checked;
});
NUPBanDuration.InvokeIfRequired(action: () =>
{
banDuration = Convert.ToInt32(value: NUPBanDuration.Value);
});
#endregion
#region 檢查值
if (useOAuth20)
{
if (string.IsNullOrEmpty(value: clientSecretFilePath))
{
MessageBox.Show(
text: "請選擇 client_secret.json 檔案的路徑。",
caption: Text,
buttons: MessageBoxButtons.OK,
icon: MessageBoxIcon.Warning);
BtnStop_Click(sender: null, e: null);
return;
}
else
{
if (!File.Exists(path: clientSecretFilePath))
{
MessageBox.Show(
text: "請確認選擇的 client_secret.json 檔案是否存在。",
caption: Text,
buttons: MessageBoxButtons.OK,
icon: MessageBoxIcon.Warning);
BtnStop_Click(sender: null, e: null);
return;
}
}
}
else
{
if (string.IsNullOrEmpty(value: apiKey))
{
MessageBox.Show(
text: "請輸入 API 金鑰。",
caption: Text,
buttons: MessageBoxButtons.OK,
icon: MessageBoxIcon.Warning);
BtnStop_Click(sender: null, e: null);
return;
}
}
if (string.IsNullOrEmpty(value: videoID))
{
MessageBox.Show(
text: "請輸入影片 ID。",
caption: Text,
buttons: MessageBoxButtons.OK,
icon: MessageBoxIcon.Warning);
BtnStop_Click(sender: null, e: null);
return;
}
if (!useOAuth20)
{
CustomFunction.WriteLog(
control: TBLog,
message: "因為您使用「API 金鑰」,故只能蒐集可疑的頻道 ID。");
}
#endregion
CustomFunction.WriteLog(
control: TBLog,
message: "開始作業。");
await Task.Run(function: async () =>
{
await GetFirstLiveChatMessagesResp(
apiKey: apiKey,
clientSecretFilePath: clientSecretFilePath,
videoId: videoID,
deleteMessage: deleteMessage,
banTemporary: banTemporary,
banPermanent: banPermanent,
banDuration: banDuration,
useOAuth20: useOAuth20);
}, cancellationToken: SharedCancellationToken.Value);
}
catch (Exception ex)
{
CustomFunction.WriteLog(
control: TBLog,
message: ex.Message);
BtnStop_Click(sender: null, e: null);
}
}
private void BtnStop_Click(object? sender, EventArgs? e)
{
try
{
if (!SharedCancellationTokenSource.IsCancellationRequested)
{
SharedCancellationTokenSource.Cancel();
}
SharedTimer.Stop();
ResetVariables();
CustomFunction.WriteLog(
control: TBLog,
message: "已結束作業。");
}
catch (Exception ex)
{
CustomFunction.WriteLog(
control: TBLog,
message: ex.Message);
}
finally
{
SetControlState(enable: true);
}
}
private void BtnClear_Click(object sender, EventArgs e)
{
try
{
TBVideoID.InvokeIfRequired(action: TBVideoID.Clear);
TBSuspiciousChannelIds.InvokeIfRequired(action: () =>
{
SuspiciousChannelIds.Clear();
TBSuspiciousChannelIds.Clear();
SuspiciousChannels.Clear();
});
TBLog.InvokeIfRequired(action: TBLog.Clear);
}
catch (Exception ex)
{
CustomFunction.WriteLog(
control: TBLog,
message: ex.Message);
}
}
private void TBBanWords_TextChanged(object sender, EventArgs e)
{
TBBanWords.InvokeIfRequired(action: () =>
{
string value = TBBanWords.Text;
BanWords.Clear();
CustomFunction.WriteLog(control: TBLog, message: "已清除封鎖的字詞。");
if (!string.IsNullOrEmpty(value: value))
{
string[] banWords = value.Split(
separator: Environment.NewLine.ToCharArray(),
options: StringSplitOptions.RemoveEmptyEntries);
BanWords.AddRange(collection: banWords);
}
if (Settings.Default.BanWords != value)
{
Settings.Default.BanWords = value;
Settings.Default.Save();
CustomFunction.WriteLog(
control: TBLog,
message: "已更新封鎖的字詞。");
}
});
}
}