Refactor URL handling in play

This commit is contained in:
Essem 2022-01-26 23:02:18 -06:00
parent 593c132555
commit eca5c58af7
No known key found for this signature in database
GPG key ID: 7D497397CC3A2A8C

View file

@ -1,15 +1,24 @@
import { play } from "../../utils/soundplayer.js";
import MusicCommand from "../../classes/musicCommand.js";
const urlRegex = /(?:\w+:)?\/\/(\S+)/;
const searchRegex = /^ytsearch:/;
class PlayCommand extends MusicCommand {
async run() {
if (!this.args[0] && this.message.attachments.length <= 0) return "You need to provide what you want to play!";
const query = this.args.join(" ").trim();
let query = this.args.join(" ").trim();
const attachment = this.message.attachments[0];
const search = urlRegex.test(query) ? query : searchRegex.test(query) ? query : !this.args[0] && attachment ? attachment.url : `ytsearch:${query}`;
return await play(this.client, search, this.message, true);
if (query.startsWith("||") && query.endsWith("||")) {
query = query.substring(2, query.length - 2);
}
if (query.startsWith("<") && query.endsWith(">")) {
query = query.substring(1, query.length - 1);
}
try {
const url = new URL(query);
return await play(this.client, url, this.message, true);
} catch {
const search = searchRegex.startsWith("ytsearch:") ? query : !this.args[0] && attachment ? attachment.url : `ytsearch:${query}`;
return await play(this.client, search, this.message, true);
}
}
static description = "Plays a song or adds it to the queue";