diff --git a/example/index.ts b/example/index.ts index 1f39dad..0d08b94 100644 --- a/example/index.ts +++ b/example/index.ts @@ -7,7 +7,7 @@ const client = new Client({ }); const player = new Player(client); -player.on("trackStart", (queue, track) => console.log(`Now playing: ${track.title} in ${queue.guild.name}!`)); +// player.on("trackStart", (queue, track) => console.log(`Now playing: ${track.title} in ${queue.guild.name}!`)); client.on("ready", () => console.log("Bot is online!")); @@ -20,15 +20,48 @@ client.on("message", async message => { if (!conn) return; return void message.channel.send(`Now Playing: **${conn.current.title}** (Played **${Math.floor(conn.connection.streamTime / 1000)} seconds**)`); } + if (message.content.startsWith("!pause") && message.guild.me.voice.channelID) { + const conn = player.getQueue(message.guild.id); + if (!conn) return; + conn.setPaused(true); + return void message.channel.send("Paused!"); + } + if (message.content.startsWith("!resume") && message.guild.me.voice.channelID) { + const conn = player.getQueue(message.guild.id); + if (!conn) return; + conn.setPaused(false); + return void message.channel.send("Resumed!"); + } + if (message.content.startsWith("!skip") && message.guild.me.voice.channelID) { + const conn = player.getQueue(message.guild.id); + if (!conn) return; + conn.skip(); + return void message.channel.send("Done!"); + } + if (message.content.startsWith("!queue") && message.guild.me.voice.channelID) { + const conn = player.getQueue(message.guild.id); + if (!conn) return; + return void message.channel.send({ content: conn.toString(), split: true }); + } + if (message.content.startsWith("!vol") && message.guild.me.voice.channelID) { + const conn = player.getQueue(message.guild.id); + if (!conn) return; + conn.connection.setVolume(parseInt(message.content.slice(4).trim())); + return void message.channel.send("Volume changed!"); + } if (message.content.startsWith("!p") && message.member.voice.channelID) { const queue = player.createQueue(message.guild); const song = await player.search(message.content.slice(2).trim(), message.author).then(x => x[0]); + queue.addTrack(song); if (!queue.connection) { queue.connect(message.member.voice.channel) .then(async q => { - await q.play(song); + await q.play(); + message.channel.send(`🎶 | Playing: **${song.title}**!`); }); + } else { + message.channel.send(`🎶 | Queued: **${song.title}**!`); } } }); diff --git a/src/Player.ts b/src/Player.ts index 023db0a..bd0a006 100644 --- a/src/Player.ts +++ b/src/Player.ts @@ -47,20 +47,20 @@ class DiscordPlayer extends EventEmitter { }); return videos.map((m) => { - (m as any).source = "youtube"; - return new Track(this, { - title: m.title, - description: m.description, - author: m.channel?.name, - url: m.url, - requestedBy: requestedBy, - thumbnail: m.thumbnail?.displayThumbnailURL("maxresdefault"), - views: m.views, - fromPlaylist: false, - duration: m.durationFormatted, - raw: m - }) + (m as any).source = "youtube"; + return new Track(this, { + title: m.title, + description: m.description, + author: m.channel?.name, + url: m.url, + requestedBy: requestedBy, + thumbnail: m.thumbnail?.displayThumbnailURL("maxresdefault"), + views: m.views, + fromPlaylist: false, + duration: m.durationFormatted, + raw: m }); + }); } default: return []; diff --git a/src/Structures/Queue.ts b/src/Structures/Queue.ts index d9a3f82..4a2d096 100644 --- a/src/Structures/Queue.ts +++ b/src/Structures/Queue.ts @@ -60,7 +60,8 @@ class Queue { skip() { if (!this.connection) return false; - return this.connection.end(); + this.connection.end(); + return true; } addTrack(track: Track) { @@ -71,6 +72,11 @@ class Queue { this.tracks.push(...tracks); } + setPaused(paused?: boolean) { + if (!this.connection) return false; + return paused ? this.connection.pause() : this.connection.resume(); + } + async play(src?: Track) { if (!this.connection || !this.connection.voiceConnection) throw new Error("Voice connection is not available, use .connect()!"); @@ -108,7 +114,7 @@ class Queue { }); } - const dispatcher = this.connection.playStream(resource); + const dispatcher = await this.connection.playStream(resource); dispatcher.setVolume(this.options.initialVolume); dispatcher.on("start", () => { diff --git a/src/VoiceInterface/BasicStreamDispatcher.ts b/src/VoiceInterface/BasicStreamDispatcher.ts index a64ab46..c506bd8 100644 --- a/src/VoiceInterface/BasicStreamDispatcher.ts +++ b/src/VoiceInterface/BasicStreamDispatcher.ts @@ -103,7 +103,9 @@ class BasicStreamDispatcher extends EventEmitter { * Disconnects from voice */ disconnect() { - this.voiceConnection.destroy(); + try { + this.voiceConnection.destroy(); + } catch {} } /** @@ -125,19 +127,21 @@ class BasicStreamDispatcher extends EventEmitter { * Play stream * @param {AudioResource} resource The audio resource to play */ - playStream(resource: AudioResource = this.audioResource) { + async playStream(resource: AudioResource = this.audioResource) { if (!resource) throw new PlayerError("Audio resource is not available!"); if (!this.audioResource) this.audioResource = resource; + if (this.voiceConnection.state.status !== VoiceConnectionStatus.Ready) await entersState(this.voiceConnection, VoiceConnectionStatus.Ready, 20000); this.audioPlayer.play(resource); return this; } setVolume(value: number) { - if (!this.audioResource) return; + if (!this.audioResource || isNaN(value) || value < 0 || value > Infinity) return false; // ye boi logarithmic ✌ this.audioResource.volume.setVolumeLogarithmic(value / 200); + return true; } get streamTime() {