1
0
mirror of https://github.com/musix-org/musix-oss synced 2024-11-15 14:00:19 +00:00
musix-oss/src/commands/search.js

67 lines
3.0 KiB
JavaScript
Raw Normal View History

2020-06-17 13:38:43 +00:00
const ytsr = require('ytsr');
2020-03-24 10:02:07 +00:00
const he = require('he');
module.exports = {
name: 'search',
2020-05-15 16:05:39 +00:00
alias: ["sr", "find"],
2020-03-24 10:02:07 +00:00
usage: '<search word(s)>',
description: 'Search the top 10 queryes and choose one.',
onlyDev: false,
permission: 'none',
2020-07-12 16:08:53 +00:00
category: 'play',
2020-03-24 10:02:07 +00:00
async execute(msg, args, client, Discord, command) {
const searchString = args.slice(1).join(" ");
const queue = client.queue.get(msg.guild.id);
const voiceChannel = msg.member.voice.channel;
if (!queue) {
if (!msg.member.voice.channel) return msg.channel.send(client.messages.noVoiceChannel);
} else {
if (voiceChannel !== queue.voiceChannel) return msg.channel.send(client.messages.wrongVoiceChannel);
}
if (!args[1]) return msg.channel.send(client.messages.noQuery);
if (voiceChannel.full) return msg.channel.send(client.messages.channelFull);
if (!voiceChannel.joinable) return msg.channel.send(client.messages.noPermsConnect);
if (!voiceChannel.speakable) return msg.channel.send(client.messages.noPermsSpeak);
2020-06-17 13:38:43 +00:00
ytsr(searchString, {
limit: 20,
}, async function (err, res) {
2020-03-24 10:02:07 +00:00
if (err) return console.log(err);
2020-06-17 13:38:43 +00:00
if (!res.items[0]) return msg.channel.send(client.messages.noResults);
const videoResults = res.items.filter(item => item.type === "video");
const videos = videoResults.slice(0, 10);
2020-03-24 10:02:07 +00:00
let index = 0;
const embed = new Discord.MessageEmbed()
.setTitle(client.messages.songSelection)
.setDescription(`${videos.map(video2 => `**${++index}** ${he.decode(video2.title)} `).join('\n')}`)
.setFooter(client.messages.provideANumber)
.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, {
max: 1,
time: 10000,
errors: ['time']
});
} catch (err) {
console.error(err);
return msg.channel.send(client.messages.cancellingVideoSelection);
}
const videoIndex = parseInt(response.first().content) - 1;
2020-07-12 16:08:53 +00:00
client.spotify.searchTracks(`track:${videos[videoIndex].title}`)
2020-06-30 17:43:00 +00:00
.then(function (data) {
client.funcs.handleVideo(
videoResults[0].link,
msg,
voiceChannel,
client,
false,
"ytdl",
data.body.tracks.items[0]
);
}, function (err) {
console.log('Something went wrong!', err);
});
2020-06-17 13:38:43 +00:00
return client.funcs.handleVideo(videos[videoIndex].link, msg, voiceChannel, client, false, "ytdl");
2020-03-24 10:02:07 +00:00
})
}
};