1
0
mirror of https://github.com/musix-org/musix-oss synced 2024-11-10 08:10:18 +00:00
musix-oss/commands/search.js

75 lines
3.5 KiB
JavaScript
Raw Normal View History

2020-02-05 20:02:53 +00:00
const YouTube = require("simple-youtube-api");
const he = require('he');
module.exports = {
name: 'search',
alias: 'sr',
2020-02-24 18:41:40 +00:00
usage: '<search word(s)>',
2020-02-05 20:02:53 +00:00
description: 'Search the top 10 queryes and choose one.',
onlyDev: false,
permission: 'none',
category: 'music',
async execute(msg, args, client, Discord, prefix) {
const youtube = new YouTube(client.config.api_key);
const searchString = args.slice(1).join(" ");
const url = args[1] ? args[1].replace(/<(.+)>/g, "$1") : "";
const serverQueue = client.queue.get(msg.guild.id);
const voiceChannel = msg.member.voice.channel;
if (!serverQueue) {
2020-03-12 11:56:31 +00:00
if (!msg.member.voice.channel) return msg.channel.send(client.messages.noVoiceChannel);
2020-02-05 20:02:53 +00:00
} else {
2020-03-12 11:56:31 +00:00
if (voiceChannel !== serverQueue.voiceChannel) return msg.channel.send(client.messages.wrongVoiceChannel);
2020-02-05 20:02:53 +00:00
}
2020-03-12 11:56:31 +00:00
if (!args[1]) return msg.channel.send(client.messages.noQuery);
2020-02-05 20:02:53 +00:00
const permissions = voiceChannel.permissionsFor(msg.client.user);
if (!permissions.has('CONNECT')) {
2020-03-12 11:56:31 +00:00
return msg.channel.send(client.messages.noPermsConnect);
2020-02-05 20:02:53 +00:00
}
if (!permissions.has('SPEAK')) {
2020-03-12 11:56:31 +00:00
return msg.channel.send(client.messages.noPermsSpeak);
2020-02-05 20:02:53 +00:00
}
if (url.match(/^https?:\/\/(www.youtube.com|youtube.com)\/playlist(.*)$/)) {
2020-03-12 11:56:31 +00:00
const lmsg = await msg.channel.send(client.messages.loadingSongs);
2020-02-05 20:02:53 +00:00
const playlist = await youtube.getPlaylist(url);
const videos = await playlist.getVideos();
for (const video of Object.values(videos)) {
const video2 = await youtube.getVideoByID(video.id);
await client.funcs.handleVideo(video2, msg, voiceChannel, client, true);
}
2020-03-13 14:20:23 +00:00
let message;
message = client.messages.playlistAdded.replace("%TITLE%", playlist.title);
return lmsg.edit(message);
2020-02-05 20:02:53 +00:00
} else {
try {
var video = await youtube.getVideo(url);
} catch (error) {
try {
var videos = await youtube.searchVideos(searchString, 10);
let index = 0;
const embed = new Discord.MessageEmbed()
2020-03-12 11:56:31 +00:00
.setTitle(client.messages.songSelection)
2020-02-05 20:02:53 +00:00
.setDescription(`${videos.map(video2 => `**${++index}** ${he.decode(video2.title)} `).join('\n')}`)
2020-03-12 11:56:31 +00:00
.setFooter(client.messages.provideANumber)
2020-02-05 20:02:53 +00:00
.setColor(client.config.embedColor)
msg.channel.send(embed);
try {
var response = await msg.channel.awaitMessages(message2 => message2.content > 0 && message2.content < 11 && message2.author === msg.author, {
2020-02-11 19:32:27 +00:00
max: 1,
2020-02-05 20:02:53 +00:00
time: 10000,
errors: ['time']
});
} catch (err) {
console.error(err);
2020-03-12 11:56:31 +00:00
return msg.channel.send(client.messages.cancellingVideoSelection);
2020-02-05 20:02:53 +00:00
}
const videoIndex = parseInt(response.first().content);
var video = await youtube.getVideoByID(videos[videoIndex - 1].id);
} catch (err) {
console.error(err);
2020-03-12 11:56:31 +00:00
return msg.channel.send(client.messages.noResults);
2020-02-05 20:02:53 +00:00
}
}
return client.funcs.handleVideo(video, msg, voiceChannel, client, false);
}
}
};