Refactor URL handling in play
This commit is contained in:
		
							parent
							
								
									593c132555
								
							
						
					
					
						commit
						eca5c58af7
					
				
					 1 changed files with 14 additions and 5 deletions
				
			
		| 
						 | 
					@ -1,16 +1,25 @@
 | 
				
			||||||
import { play } from "../../utils/soundplayer.js";
 | 
					import { play } from "../../utils/soundplayer.js";
 | 
				
			||||||
import MusicCommand from "../../classes/musicCommand.js";
 | 
					import MusicCommand from "../../classes/musicCommand.js";
 | 
				
			||||||
const urlRegex = /(?:\w+:)?\/\/(\S+)/;
 | 
					 | 
				
			||||||
const searchRegex = /^ytsearch:/;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
class PlayCommand extends MusicCommand {
 | 
					class PlayCommand extends MusicCommand {
 | 
				
			||||||
  async run() {
 | 
					  async run() {
 | 
				
			||||||
    if (!this.args[0] && this.message.attachments.length <= 0) return "You need to provide what you want to play!";
 | 
					    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 attachment = this.message.attachments[0];
 | 
				
			||||||
    const search = urlRegex.test(query) ? query : searchRegex.test(query) ? query : !this.args[0] && attachment ? attachment.url : `ytsearch:${query}`;
 | 
					    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);
 | 
					      return await play(this.client, search, this.message, true);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  static description = "Plays a song or adds it to the queue";
 | 
					  static description = "Plays a song or adds it to the queue";
 | 
				
			||||||
  static aliases = ["p"];
 | 
					  static aliases = ["p"];
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue