-
-
Notifications
You must be signed in to change notification settings - Fork 94
/
Song.ts
255 lines (242 loc) · 7.33 KB
/
Song.ts
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
import { Playlist } from "./Playlist";
import { DisTubeError, formatDuration, isMemberInstance, parseNumber, toSecond } from "..";
import type ytpl from "@distube/ytpl";
import type ytdl from "@distube/ytdl-core";
import type { GuildMember } from "discord.js";
import type { Chapter, OtherSongInfo, RelatedSong, SearchResult } from "..";
/**
* Class representing a song.
*
* <info>If {@link Song} is added from a YouTube {@link SearchResult} or {@link
* Playlist}, some info will be missing to save your resources. It will be filled
* when emitting {@link DisTube#playSong} event.
*
* Missing info: {@link Song#likes}, {@link Song#dislikes}, {@link Song#streamURL},
* {@link Song#related}, {@link Song#chapters}, {@link Song#age_restricted}</info>
*/
export class Song<T = unknown> {
source!: string;
#metadata!: T;
formats?: ytdl.videoFormat[];
#member?: GuildMember;
id?: string;
name?: string;
isLive!: boolean;
duration!: number;
formattedDuration?: string;
url!: string;
streamURL?: string;
thumbnail?: string;
related!: RelatedSong[];
views!: number;
likes!: number;
dislikes!: number;
uploader!: {
name?: string;
url?: string;
};
age_restricted!: boolean;
chapters!: Chapter[];
reposts!: number;
#playlist?: Playlist;
/**
* Create a Song
*
* @param info - Raw info
* @param options - Optional options
*/
constructor(
info:
| ytdl.videoInfo
| SearchResult
| OtherSongInfo
| ytdl.relatedVideo
| RelatedSong
| ytpl.result["items"][number],
options: {
member?: GuildMember;
source?: string;
metadata?: T;
} = {},
) {
const { member, source, metadata } = { source: "youtube", ...options };
if (
typeof source !== "string" ||
((info as OtherSongInfo).src && typeof (info as OtherSongInfo).src !== "string")
) {
throw new DisTubeError("INVALID_TYPE", "string", source, "source");
}
/**
* The source of the song
*/
this.source = ((info as OtherSongInfo)?.src || source).toLowerCase();
/**
* Optional metadata that can be used to identify the song. This is attached by the
* {@link DisTube#play} method.
*/
this.metadata = metadata as T;
this.member = member;
if (this.source === "youtube") {
this._patchYouTube(info as ytdl.videoInfo);
} else {
this._patchOther(info as OtherSongInfo);
}
}
_patchYouTube(i: ytdl.videoInfo | SearchResult) {
// FIXME
const info = i as any;
if (info.full === true) {
/**
* Stream formats (Available if the song is from YouTube and playing)
*/
this.formats = info.formats;
// eslint-disable-next-line @typescript-eslint/no-var-requires, @typescript-eslint/no-require-imports
const err = require("@distube/ytdl-core/lib/utils").playError(info.player_response, [
"UNPLAYABLE",
"LIVE_STREAM_OFFLINE",
"LOGIN_REQUIRED",
]);
if (err) throw err;
if (!info.formats?.length) throw new DisTubeError("UNAVAILABLE_VIDEO");
}
const details = info.videoDetails || info;
/**
* YouTube video id
*/
this.id = details.videoId || details.id;
/**
* Song name.
*/
this.name = details.title || details.name;
/**
* Indicates if the video is an active live.
*/
this.isLive = Boolean(details.isLive);
/**
* Song duration.
*/
this.duration = this.isLive ? 0 : toSecond(details.lengthSeconds || details.length_seconds || details.duration);
/**
* Formatted duration string (`hh:mm:ss`, `mm:ss` or `Live`).
*/
this.formattedDuration = this.isLive ? "Live" : formatDuration(this.duration);
/**
* Song URL.
*/
this.url = `https://www.youtube.com/watch?v=${this.id}`;
/**
* Stream / Download URL (Available if the song is playing)
*/
this.streamURL = undefined;
/**
* Song thumbnail.
*/
this.thumbnail =
details.thumbnails?.sort((a: any, b: any) => b.width - a.width)?.[0]?.url ||
details.thumbnail?.url ||
details.thumbnail;
/**
* Related songs (without {@link Song#related} properties)
*/
this.related = info?.related_videos || details.related || [];
if (!Array.isArray(this.related)) throw new DisTubeError("INVALID_TYPE", "Array", this.related, "Song#related");
this.related = this.related.map((v: any) => new Song(v, { source: this.source, metadata: this.metadata }));
/**
* Song views count
*/
this.views = parseNumber(details.viewCount || details.view_count || details.views);
/**
* Song like count
*/
this.likes = parseNumber(details.likes);
/**
* Song dislike count
*/
this.dislikes = parseNumber(details.dislikes);
/**
* Song uploader
*/
this.uploader = {
name: info.uploader?.name || details.author?.name,
url: info.uploader?.url || details.author?.channel_url || details.author?.url,
};
/**
* Whether or not an age-restricted content
*/
this.age_restricted = Boolean(details.age_restricted);
/**
* Chapters information (YouTube only)
*/
this.chapters = details.chapters || [];
/**
* Song repost count
*/
this.reposts = 0;
}
/**
* Patch data from other source
*
* @param info - Video info
*/
_patchOther(info: OtherSongInfo) {
this.id = info.id;
this.name = info.title || info.name;
this.isLive = Boolean(info.is_live || info.isLive);
this.duration = this.isLive ? 0 : toSecond(info._duration_raw || info.duration);
this.formattedDuration = this.isLive ? "Live" : formatDuration(this.duration);
this.url = info.webpage_url || info.url;
this.thumbnail = info.thumbnail;
this.related = info.related || [];
if (!Array.isArray(this.related)) throw new DisTubeError("INVALID_TYPE", "Array", this.related, "Song#related");
this.related = this.related.map(i => new Song(i, { source: this.source, metadata: this.metadata }));
this.views = parseNumber(info.view_count || info.views);
this.likes = parseNumber(info.like_count || info.likes);
this.dislikes = parseNumber(info.dislike_count || info.dislikes);
this.reposts = parseNumber(info.repost_count || info.reposts);
if (typeof info.uploader === "string") {
this.uploader = {
name: info.uploader,
url: info.uploader_url,
};
} else {
this.uploader = {
name: info.uploader?.name,
url: info.uploader?.url,
};
}
this.age_restricted = info.age_restricted || (Boolean(info.age_limit) && parseNumber(info.age_limit) >= 18);
this.chapters = info.chapters || [];
}
/**
* The playlist added this song
*/
get playlist() {
return this.#playlist;
}
set playlist(playlist: Playlist | undefined) {
if (!(playlist instanceof Playlist)) throw new DisTubeError("INVALID_TYPE", "Playlist", playlist, "Song#playlist");
this.#playlist = playlist;
this.member = playlist.member;
}
/**
* User requested.
*/
get member() {
return this.#member;
}
set member(member: GuildMember | undefined) {
if (isMemberInstance(member)) this.#member = member;
}
/**
* User requested.
*/
get user() {
return this.member?.user;
}
get metadata() {
return this.#metadata;
}
set metadata(metadata: T) {
this.#metadata = metadata;
}
}