-
-
Notifications
You must be signed in to change notification settings - Fork 94
/
DirectLink.ts
23 lines (21 loc) · 820 Bytes
/
DirectLink.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import { request } from "undici";
import { ExtractorPlugin, Song } from "..";
import type { GuildMember } from "discord.js";
export class DirectLinkPlugin extends ExtractorPlugin {
override async validate(url: string) {
try {
const headers = await request(url, { method: "HEAD" }).then(res => res.headers);
const types = headers["content-type"];
const type = Array.isArray(types) ? types[0] : types;
if (["audio/", "video/", "application/ogg"].some(s => type?.startsWith(s))) return true;
} catch {
// invalid URL
}
return false;
}
resolve(url: string, options: { member?: GuildMember; metadata?: any } = {}) {
const u = new URL(url);
const name = u.pathname.split("/").pop() || u.href;
return new Song({ name, url, src: "direct_link" }, options);
}
}