From ec8a7d3009d8bbbeed8a0c4e484e01b392cac456 Mon Sep 17 00:00:00 2001 From: Hazem Krimi Date: Mon, 11 May 2020 15:14:26 +0100 Subject: [PATCH] completed seek command --- commands/music/seek.js | 23 ++++++++++++-- index.js | 71 ++++++++++++++++++++++++++++++------------ 2 files changed, 72 insertions(+), 22 deletions(-) diff --git a/commands/music/seek.js b/commands/music/seek.js index aa17194..8aecc65 100644 --- a/commands/music/seek.js +++ b/commands/music/seek.js @@ -9,6 +9,14 @@ module.exports = class JoinCommand extends Command { group: 'music', description: 'seeks to a timestamp in the track', guildOnly: true, + args: [ + { + key: 'query', + prompt: 'where do you want to seek?', + type: 'string', + validate: query => query.length > 0 && query.match(/(\d+:)?\d{2}:\d{2}/) + } + ], throttling: { usages: 1, duration: 3 @@ -16,7 +24,7 @@ module.exports = class JoinCommand extends Command { }); } - run = async message => { + run = async(message, { query }) => { try { if (!message.member.voice.channel) { const embed = new MessageEmbed().setColor('#ff0000').setTitle(':x: You need to join a voice channel first'); @@ -27,7 +35,18 @@ module.exports = class JoinCommand extends Command { return await message.say({ embed }); } else { - // TODO seek logic goes here + const seekDuration = message.guild.formatDurationObject(query); + const seekSeconds = seekDuration.hours * 3600 + seekDuration.minutes * 60 + seekDuration.seconds; + const nowplayingSeconds = message.guild.music.nowPlaying.duration.hours * 3600 + message.guild.music.nowPlaying.duration.minutes * 60 + message.guild.music.nowPlaying.duration.seconds; + if (seekSeconds > nowplayingSeconds) { + const embed = new MessageEmbed().setColor('#ff0000').setTitle(':x: Seek time is greater than track time'); + return await message.say({ embed }); + } + message.guild.music.seek = seekSeconds; + message.guild.music.nowPlaying.playingFor = { hours: seekDuration.hours, minutes: seekDuration.minutes, seconds: seekDuration.seconds, string: message.guild.formatDurationString(seekDuration) }; + await message.guild.play(message.guild.music.queue, message); + const embed = new MessageEmbed().setColor('#000099').setTitle(`:musical_note: Sought to ${query}`); + return await message.say({ embed }); } } catch (err) { console.error(err); diff --git a/index.js b/index.js index 6e4e24f..9ebb403 100644 --- a/index.js +++ b/index.js @@ -2,7 +2,7 @@ const { CommandoClient } = require('discord.js-commando'); const { Structures } = require('discord.js'); const { MessageEmbed } = require('discord.js'); const path = require('path'); -const ytdl = require('ytdl-core-discord'); +const ytdl = require('ytdl-core'); Structures.extend('Guild', Guild => { class MusicGuild extends Guild { @@ -14,6 +14,7 @@ Structures.extend('Guild', Guild => { nowPlaying: null, volume: 1, dispatcher: null, + seek: null, sfx: { earrape: false } @@ -22,41 +23,57 @@ Structures.extend('Guild', Guild => { play = async (queue, message) => { try { - if (queue.length === 0) { + if (!message.guild.music.seek && queue.length === 0) { const embed = new MessageEmbed().setColor('#ff0000').setTitle(`:x: Error occured: ${err.message}`); return await message.say({ embed }); } - const voiceChannel = queue[0].voiceChannel; + const voiceChannel = !message.guild.music.seek ? queue[0].voiceChannel : message.guild.music.nowPlaying.voiceChannel; const connection = await voiceChannel.join(); let dispatcher; - switch (queue[0].type) { - case 'youtube': { dispatcher = connection.play(await ytdl(queue[0].link, { quality: 'highestaudio' }), { type: 'opus' }); break; } - case 'facebook': { dispatcher = connection.play(queue[0].link); break; } - case 'search': { dispatcher = connection.play(await ytdl(queue[0].link), { type: 'opus' }); break; } - case 'other': { dispatcher = connection.play(queue[0].link); break; } + if (message.guild.music.seek) { + switch (message.guild.music.nowPlaying.type) { + case 'youtube': { dispatcher = connection.play(ytdl(message.guild.music.nowPlaying.link, { quality: 'highestaudio' }), { seek: message.guild.music.seek }); break; } + case 'facebook': { dispatcher = connection.play(message.guild.music.nowPlaying.link, { seek: message.guild.music.seek }); break; } + case 'search': { dispatcher = connection.play(ytdl(message.guild.music.nowPlaying.link, { quality: 'highestaudio' }), { seek: message.guild.music.seek }); break; } + case 'other': { dispatcher = connection.play(message.guild.music.nowPlaying.link, { seek: message.guild.music.seek }); break; } + } + } else { + switch (queue[0].type) { + case 'youtube': { dispatcher = connection.play(ytdl(queue[0].link, { quality: 'highestaudio' })); break; } + case 'facebook': { dispatcher = connection.play(queue[0].link); break; } + case 'search': { dispatcher = connection.play(ytdl(queue[0].link, { quality: 'highestaudio' })); break; } + case 'other': { dispatcher = connection.play(queue[0].link); break; } + } } dispatcher.on('start', async () => { - message.guild.music.nowPlaying = queue[0]; + message.guild.music.nowPlaying = !message.guild.music.seek ? queue[0] : message.guild.music.nowPlaying; message.guild.startCounter(message); message.guild.music.dispatcher = dispatcher; dispatcher.setVolume(message.guild.music.volume); - const embed = new MessageEmbed().setColor('#000099').setTitle(`:arrow_forward: Play`).addField('Now playing', queue[0].title); - if (queue[0].type === 'youtube' || queue[0].type === 'search') embed.setThumbnail(queue[0].thumbnail); - if (queue[0].type === 'youtube' || queue[0].type === 'search' || queue[0].type === 'facebook') embed.addField('By', queue[0].by); - embed.addField('Duration', queue[0].duration.string); - await message.say({ embed }); + if (!message.guild.music.seek) { + const embed = new MessageEmbed().setColor('#000099').setTitle(`:arrow_forward: Play`).addField('Now playing', queue[0].title); + if (queue[0].type === 'youtube' || queue[0].type === 'search') embed.setThumbnail(queue[0].thumbnail); + if (queue[0].type === 'youtube' || queue[0].type === 'search' || queue[0].type === 'facebook') embed.addField('By', queue[0].by); + embed.addField('Duration', queue[0].duration.string); + await message.say({ embed }); + } + message.guild.music.seek = null; return queue.shift(); }); dispatcher.on('finish', async () => { - if (queue.length >= 1) return this.play(queue, message); + if (queue.length >= 1) { + message.guild.music.seek = null; + return this.play(queue, message); + } else { message.guild.music.isPlaying = false; message.guild.music.nowPlaying = null; message.guild.music.dispatcher = null; + message.guild.music.seek = null; voiceChannel.leave(); const embed = new MessageEmbed().setColor('#000099').setTitle(':musical_note: Queue ended'); return await message.say({ embed }); @@ -68,6 +85,7 @@ Structures.extend('Guild', Guild => { message.guild.music.isPlaying = false; message.guild.music.nowPlaying = false; message.guild.music.dispatcher = null; + message.guild.music.seek = null; voiceChannel.leave(); throw err; }); @@ -89,7 +107,7 @@ Structures.extend('Guild', Guild => { hours: message.guild.music.nowPlaying.playingFor.hours, minutes: message.guild.music.nowPlaying.playingFor.minutes + 1, seconds: 0, - string: this.formatDuration({ + string: this.formatDurationString({ hours: message.guild.music.nowPlaying.playingFor.hours, minutes: message.guild.music.nowPlaying.playingFor.minutes + 1, seconds: 0 @@ -101,7 +119,7 @@ Structures.extend('Guild', Guild => { hours: message.guild.music.nowPlaying.playingFor.hours + 1, minutes: 0, seconds: 0, - string: this.formatDuration({ + string: this.formatDurationString({ hours: message.guild.music.nowPlaying.playingFor.hours + 1, minutes: 0, seconds: 0 @@ -113,7 +131,7 @@ Structures.extend('Guild', Guild => { hours: message.guild.music.nowPlaying.playingFor.hours, minutes: message.guild.music.nowPlaying.playingFor.minutes, seconds: message.guild.music.nowPlaying.playingFor.seconds + 1, - string: this.formatDuration({ + string: this.formatDurationString({ hours: message.guild.music.nowPlaying.playingFor.hours, minutes: message.guild.music.nowPlaying.playingFor.minutes, seconds: message.guild.music.nowPlaying.playingFor.seconds + 1 @@ -128,9 +146,20 @@ Structures.extend('Guild', Guild => { } } - formatDuration = durationObject => { + formatDurationString = durationObject => { return `${durationObject.hours < 10 ? '0' + durationObject.hours : durationObject.hours ? durationObject.hours : '00'}:${durationObject.minutes < 10 ? '0' + durationObject.minutes : durationObject.minutes ? durationObject.minutes : '00'}:${durationObject.seconds < 10 ? '0' + durationObject.seconds : durationObject.seconds ? durationObject.seconds : '00'}`; } + + formatDurationObject = durationString => { + if (!durationString.match(/(\d+:)?\d{2}:\d{2}/)) return; + const time = durationString.split(':').map(t => parseInt(t)); + + switch(time.length) { + case 3: return { hours: time[0], minutes: time[1], seconds: time[2] }; + case 2: return { hours: 0, minutes: time[0], seconds: time[1] }; + case 1: return { hours: 0, minutes: 0, seconds: time[0] }; + } + } } return MusicGuild; @@ -156,4 +185,6 @@ client.once('ready', () => { client.login(process.env.BOT_TOKEN); -client.on('error', console.error); \ No newline at end of file +client.on('error', console.error); + +process.on('unhandledRejection', error => console.error('Uncaught Promise Rejection', error)); \ No newline at end of file