mirror of
https://github.com/hazemKrimi/discord-bot.git
synced 2026-05-01 18:30:25 +00:00
completed search command
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
const { Command } = require('discord.js-commando');
|
||||
const { MessageEmbed } = require('discord.js');
|
||||
const Youtube = require('simple-youtube-api');
|
||||
const youtube = new Youtube(process.env.YOUTUBE_API_KEY);
|
||||
|
||||
module.exports = class JoinCommand extends Command {
|
||||
constructor(client) {
|
||||
@@ -9,6 +11,14 @@ module.exports = class JoinCommand extends Command {
|
||||
group: 'music',
|
||||
description: 'search and play a track in youtube',
|
||||
guildOnly: true,
|
||||
args: [
|
||||
{
|
||||
key: 'query',
|
||||
prompt: 'what do you want to listen to?',
|
||||
type: 'string',
|
||||
validate: query => query.length > 0
|
||||
}
|
||||
],
|
||||
throttling: {
|
||||
usages: 1,
|
||||
duration: 3
|
||||
@@ -16,18 +26,70 @@ module.exports = class JoinCommand extends Command {
|
||||
});
|
||||
}
|
||||
|
||||
run = async message => {
|
||||
run = async(message, { query }) => {
|
||||
try {
|
||||
if (!message.member.voice.channel) {
|
||||
const voiceChannel = message.member.voice.channel;
|
||||
|
||||
if (!voiceChannel) {
|
||||
const embed = new MessageEmbed().setColor('#ff0000').setTitle(':x: You need to join a voice channel first');
|
||||
return await message.say({ embed });
|
||||
}
|
||||
else if (!message.guild.music.isPlaying) {
|
||||
const embed = new MessageEmbed().setColor('#ff0000').setTitle(':x: Play something first');
|
||||
else {
|
||||
const videos = await youtube.searchVideos(query, 5);
|
||||
if (videos.length < 5) {
|
||||
const embed = new MessageEmbed().setColor('#ff0000').setTitle(':x: Nothing found, get more specific');
|
||||
return await message.say({ embed });
|
||||
}
|
||||
|
||||
const embed = new MessageEmbed().setColor('#000099').setTitle(`:mag: Search`);
|
||||
|
||||
videos.forEach((video, index) => {
|
||||
embed.addField(index + 1, `${video.title} By ${video.channel.title}`);
|
||||
});
|
||||
embed.addField(0, 'exit');
|
||||
|
||||
await message.say({ embed });
|
||||
|
||||
let choice;
|
||||
let index;
|
||||
|
||||
try {
|
||||
choice = await message.channel.awaitMessages(msg => (msg.content >= 0 && msg.content < 6), { max: 1, maxProcessed: 1, time: 60000, errors: ['time'] });
|
||||
index = parseInt(choice.first().content);
|
||||
} catch(err) {
|
||||
const embed = new MessageEmbed().setColor('#ff0000').setTitle(`:x: Invalid choice or time\' up`);
|
||||
return await message.say({ embed });
|
||||
}
|
||||
|
||||
if (parseInt(choice.first().content) === 0) {
|
||||
const embed = new MessageEmbed().setColor('#000099').setTitle(`:mag: Search canceled`);
|
||||
return await message.say({ embed });
|
||||
}
|
||||
|
||||
const video = await youtube.getVideoByID(videos[index - 1].raw.id.videoId);
|
||||
const title = video.title;
|
||||
const by = video.channel.title;
|
||||
const durationString = message.guild.formatDuration(video.duration);
|
||||
const thumbnail = video.thumbnails.high.url;
|
||||
const data = {
|
||||
type: 'search',
|
||||
link: `https://www.youtube.com/watch?v=${video.id}`,
|
||||
title,
|
||||
by,
|
||||
duration: durationString !== '00:00:00' ? { hours: video.duration.hours, minutes: video.duration.minutes, seconds: video.duration.seconds, string: durationString } : 'Live Stream',
|
||||
thumbnail,
|
||||
voiceChannel
|
||||
};
|
||||
|
||||
message.guild.music.queue.push(data);
|
||||
|
||||
if (message.guild.music.isPlaying === false || message.guild.music.isPlaying === undefined) {
|
||||
message.guild.music.isPlaying = true;
|
||||
return message.guild.play(message.guild.music.queue, message);
|
||||
} else {
|
||||
const embed = new MessageEmbed().setColor('#000099').setTitle(`:arrow_forward: Added "${data.title}" to queue`);
|
||||
return await message.say({ embed });
|
||||
}
|
||||
else {
|
||||
// TODO search logic goes here
|
||||
}
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
|
||||
Reference in New Issue
Block a user