forked from mo10/TS3NeteaseCloudMusicBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Command.cs
387 lines (346 loc) · 14.1 KB
/
Command.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
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
using TS3AudioBot.Audio;
using TS3AudioBot;
using TS3AudioBot.Plugins;
using TS3AudioBot.ResourceFactories;
using System.Threading.Tasks;
using TS3AudioBot.CommandSystem;
using System.Threading;
using System.Linq;
using TSLib.Scheduler;
using System.Collections.Generic;
using System;
using TS3AudioBot.Helper;
using System.IO;
using System.Reflection;
namespace ts3ncm
{
public class NCMCommand : IBotPlugin
{
private PlayManager playManager;
private Ts3Client ts3Client;
private ResourceResolver resourceResolver;
private DedicatedTaskScheduler taskScheduler;
private TickWorker worker;
private InvokerData invokerData;
private Queue<AudioResource> songQueue;
public string Cookie;
/// <summary>
/// 登入操作正在进行中?
/// </summary>
private bool isLogging = false;
/// <summary>
/// 1 未登入, 2 登入有效
/// </summary>
public int loginStatus = 0;
public NCMCommand(PlayManager playManager, Ts3Client ts3Client, ResourceResolver resourceResolver, DedicatedTaskScheduler taskScheduler)
{
this.playManager = playManager;
this.ts3Client = ts3Client;
this.resourceResolver = resourceResolver;
this.taskScheduler = taskScheduler;
songQueue = new Queue<AudioResource>();
}
public async void Dispose()
{
worker.Disable();
playManager.AfterResourceStarted -= PlayManager_AfterResourceStarted;
playManager.PlaybackStopped -= PlayManager_PlaybackStopped;
await this.ts3Client.SendChannelMessage($"{Env.Name} 卸载");
}
public async Task CheckLoginStatus()
{
int login_status = 1;
if (!string.IsNullOrEmpty(this.Cookie))
{
var status = await APIHelper.GetLoginStatusAasync(Env.Backend, Cookie);
if (status.data.code == 200 && status.data.account.status == 0)
login_status = 2;
}
if (this.loginStatus != login_status)
{
this.loginStatus = login_status;
if (login_status == 1)
await this.ts3Client.ChangeName($"{Env.Name} - 未登入");
if (login_status == 2)
await this.ts3Client.ChangeName($"{Env.Name}");
}
}
public async void Initialize()
{
playManager.AfterResourceStarted += PlayManager_AfterResourceStarted;
playManager.PlaybackStopped += PlayManager_PlaybackStopped;
// 30秒检查一次登入状态
worker = taskScheduler.CreateTimer(async() => await CheckLoginStatus(), new TimeSpan(0, 0, 30), true);
await CheckLoginStatus();
await this.ts3Client.SendChannelMessage($"{Env.Name} 就绪");
}
private Task PlayManager_AfterResourceStarted(object sender, PlayInfoEventArgs value)
{
this.invokerData = value.Invoker;
return Task.CompletedTask;
}
private async Task PlayManager_PlaybackStopped(object sender, EventArgs e)
{
PlayManager playManager = (PlayManager)sender;
await TryPlayNextAsync(playManager, ts3Client, invokerData);
}
private bool IsSongInQueue(string id)
{
return songQueue.Any(ar => ar.ResourceId.Equals(id));
}
public async Task<string> TryPlayNextAsync(PlayManager playManager, Ts3Client ts3Client, InvokerData invoker)
{
AudioResource ar;
if (!songQueue.TryDequeue(out ar))
{
await ts3Client.SendChannelMessage($"⏹ 播放结束");
return null;
}
var url = await APIHelper.GetAudioUrlAsync(Env.Backend, Cookie, ar.ResourceId);
try
{
await playManager.Play(invoker, new PlayResource(url, ar));
await ts3Client.SendChannelMessage($"► 正在播放: [URL={ar.Get("songUrl")}]{ar.ResourceTitle}[/URL] - {ar.Get("artist")}");
await MainCommands.CommandBotAvatarSet(ts3Client, ar.Get("coverUrl"));
}
catch (Exception ex)
{
throw Error.Str($"‼️ 播放失败: {ex}"); ;
}
return null;
}
[Command("login")]
public async Task CommandNcmLoginAsync(PlayManager playManager, Ts3Client ts3Client)
{
if (isLogging)
{
await ts3Client.SendChannelMessage("请使用APP扫描机器人头像二维码登入");
return;
}
isLogging = true;
// 1. 生成QR Key
var key = await APIHelper.GetLoginQRKeyAsync(Env.Backend);
if (string.IsNullOrEmpty(key))
{
await ts3Client.SendChannelMessage("生成二维码失败,请重试");
isLogging = false;
return;
}
// 2. 生成二维码
using (var img = await APIHelper.GetLoginQRImageAsync(Env.Backend, key))
{
if (img == null)
{
await ts3Client.SendChannelMessage("二维码加载失败,请重试");
isLogging = false;
return;
}
await this.ts3Client.UploadAvatar(img);
}
// 3. 等待登入
await ts3Client.SendChannelMessage("请使用APP扫描机器人头像二维码登入");
bool waiting = true;
int lastCode = 0;
while (waiting)
{
await Task.Delay(3000);
var resp = await APIHelper.GetLoginQRStatusAsync(Env.Backend, key);
switch (resp.code)
{
case 800:
if (lastCode != resp.code)
{
await ts3Client.SendChannelMessage("二维码已过期,请重新登入");
}
lastCode = resp.code;
waiting = false;
break;
case 801:
if (lastCode != resp.code)
{
await ts3Client.ChangeDescription("等待扫码");
}
lastCode = resp.code;
break;
case 802:
if (lastCode != resp.code)
{
await ts3Client.ChangeDescription($"{resp.nickname} 等待客户端确认");
await MainCommands.CommandBotAvatarSet(ts3Client, resp.avatarUrl);
}
lastCode = resp.code;
break;
case 803:
if (lastCode != resp.code)
{
this.Cookie = processCookie(resp.cookie);
await ts3Client.SendChannelMessage($"{resp.nickname} 登入成功");
await CheckLoginStatus();
}
lastCode = resp.code;
waiting = false;
break;
default:
await ts3Client.SendChannelMessage($"未知状态码 {resp.code}:{resp.message}");
waiting = false;
break;
}
}
if (playManager.IsPlaying)
{
// 恢复正在播放的描述和封面图
await this.ts3Client.ChangeDescription(playManager.CurrentPlayData.PlayResource.AudioResource.ResourceTitle);
await MainCommands.CommandBotAvatarSet(ts3Client, playManager.CurrentPlayData.PlayResource.AudioResource.Get("coverUrl"));
}
else
{
await this.ts3Client.DeleteAvatar();
await this.ts3Client.ChangeDescription("等待点歌...");
}
isLogging = false;
}
[Command("logout")]
public async Task CommandNcmLogoutAsync(PlayManager playManager, Ts3Client ts3Client)
{
if (isLogging)
{
await ts3Client.SendChannelMessage("请使用APP扫描机器人头像二维码登入");
return;
}
this.Cookie = "";
await ts3Client.SendChannelMessage("已登出");
}
[Command("ncm status")]
public async Task<string> CommandNcmStatusAsync()
{
string result = $"\n后端服务器: {Env.Backend}\n当前用户: ";
if (string.IsNullOrEmpty(this.Cookie))
{
result += $"未登入";
return result;
}
var status = await APIHelper.GetLoginStatusAasync(Env.Backend, Cookie);
if (status.data.code == 200 && status.data.account.status == 0)
{
result += $"[URL=https://music.163.com/#/user/home?id={status.data.profile.userId}]{status.data.profile.nickname}[/URL]\n";
result += $"饼干: {this.Cookie}";
}
else
{
result += $"未登入";
}
return result;
}
[Command("ncm setcookie")]
public async Task<string> CommandNcmSetCookieAsync(string cookie)
{
var status = await APIHelper.GetLoginStatusAasync(Env.Backend, cookie);
if (status.data.code == 200 && status.data.account.status == 0)
{
this.Cookie = cookie;
await CheckLoginStatus();
return $"登入成功";
}
return $"饼干无效";
}
[Command("p")]
public async Task<string> CommandNcmPlayAsync(PlayManager playManager, Ts3Client ts3Client, InvokerData invoker, string url_or_keyword)
{
var url_or_name = TextUtil.ExtractUrlFromBb(url_or_keyword);
var count = 0;
IEnumerable<SongDetail> details = null;
string urlPrefix = "https://music.163.com/#/song?id=";
switch (UrlMatcher.MatchUrlType(url_or_name))
{
case NcmUrlType.Song:
var songId = UrlMatcher.ExtractID(url_or_name);
details = await APIHelper.GetSongDetailAsync(Env.Backend, Cookie, songId);
break;
case NcmUrlType.Album:
var albumId = UrlMatcher.ExtractID(url_or_name);
details = await APIHelper.GetAlbumDetailAsync(Env.Backend, Cookie, albumId);
break;
case NcmUrlType.Playlist:
var listId = UrlMatcher.ExtractID(url_or_name);
details = await APIHelper.GetPlaylistDetailAsync(Env.Backend, Cookie, listId);
break;
case NcmUrlType.Radio:
var readioId = UrlMatcher.ExtractID(url_or_name);
details = await APIHelper.GetDjRadioDetailAsync(Env.Backend, Cookie, readioId);
urlPrefix = "https://music.163.com/#/program?id=";
break;
case NcmUrlType.Program:
var programId = UrlMatcher.ExtractID(url_or_name);
details = await APIHelper.GetProgramDetailAsync(Env.Backend, Cookie, programId);
urlPrefix = "https://music.163.com/#/program?id=";
break;
default:
// 搜索
var result = await APIHelper.GetSearchDetailAsync(Env.Backend, Cookie, url_or_keyword);
if (result.Count() >= 0)
{
var list = new List<SongDetail>();
list.Add(result.First());
details = list;
}
break;
}
if (details == null || details.Count() == 0)
return $"无法播放,可能版权问题或未开通VIP";
foreach (var detail in details)
{
if (IsSongInQueue(detail.id))
continue;
var ar = new AudioResource(detail.id, detail.title, "ncm")
.Add("artist", detail.author)
.Add("coverUrl", detail.picUrl);
if (!string.IsNullOrEmpty(detail.program_id))
ar.Add("songUrl", $"{urlPrefix}{detail.program_id}");
else
ar.Add("songUrl", $"{urlPrefix}{detail.id}");
songQueue.Enqueue(ar);
count++;
}
// 没有歌曲正在播放,开始播放
if (!playManager.IsPlaying)
{
await TryPlayNextAsync(playManager, ts3Client, invoker);
count--;
}
if (count > 0)
{
return $"添加了 {count} 首歌到播放队列";
}
return null;
}
[Command("s")]
public async Task<string> CommandNcmSkipAsync(PlayManager playManager)
{
await playManager.Stop();
return null;
}
[Command("fs")]
public async Task<string> CommandStopAsync(PlayManager playManager)
{
songQueue.Clear();
await playManager.Stop();
return null;
}
public string processCookie(string cookie)
{
string realCookie = string.Empty;
foreach (string line in cookie.Split("Only;"))
{
if (line.Contains("Path=/;"))
{
var kv = line.Split(';')[0];
if (!string.IsNullOrEmpty(kv.Split('=')[1]))
{
realCookie += kv + "; ";
}
}
}
return realCookie;
}
}
}