diff --git a/commands/loop.js b/commands/loop.js index 16d0f4f..f3bcfeb 100644 --- a/commands/loop.js +++ b/commands/loop.js @@ -1,22 +1,10 @@ -const magick = require("../utils/image.js"); +const soundPlayer = require("../utils/soundplayer.js"); exports.run = async (message) => { - message.channel.sendTyping(); - const image = await require("../utils/imagedetect.js")(message); - if (image === undefined) return `${message.author.mention}, you need to provide a GIF to loop!`; - const { buffer, type } = await magick.run({ - cmd: "freeze", - path: image.path, - loop: true, - onlyGIF: true - }); - if (buffer === "nogif") return `${message.author.mention}, that isn't a GIF!`; - return { - file: buffer, - name: `loop.${type}` - }; + return await soundPlayer.loop(message); }; -exports.aliases = ["unfreeze"]; -exports.category = 5; -exports.help = "Makes a GIF loop endlessly"; \ No newline at end of file +exports.aliases = ["toggleloop", "repeat"]; +exports.category = 7; +exports.help = "Loops the music"; +exports.requires = "sound"; \ No newline at end of file diff --git a/commands/nowplaying.js b/commands/nowplaying.js index 9b137a8..8f9d214 100644 --- a/commands/nowplaying.js +++ b/commands/nowplaying.js @@ -1,7 +1,7 @@ const soundPlayer = require("../utils/soundplayer.js"); exports.run = async (message) => { - await soundPlayer.playing(message); + return await soundPlayer.playing(message); }; exports.aliases = ["playing", "np"]; diff --git a/commands/pause.js b/commands/pause.js index bc20b0f..ee216da 100644 --- a/commands/pause.js +++ b/commands/pause.js @@ -1,7 +1,7 @@ const soundPlayer = require("../utils/soundplayer.js"); exports.run = async (message) => { - await soundPlayer.pause(message); + return await soundPlayer.pause(message); }; exports.aliases = ["resume"]; diff --git a/commands/play.js b/commands/play.js index c4710f4..757e438 100644 --- a/commands/play.js +++ b/commands/play.js @@ -6,7 +6,7 @@ exports.run = async (message, args) => { if (!args[0]) return `${message.author.mention}, you need to provide what you want to play!`; const query = args.join(" ").trim(); const search = urlRegex.test(query) ? query : (searchRegex.test(query) ? query : `ytsearch:${query}`); - await soundPlayer.play(encodeURIComponent(search), message, true); + return await soundPlayer.play(encodeURIComponent(search), message, true); }; exports.aliases = ["p"]; diff --git a/commands/queue.js b/commands/queue.js index 14bc541..6af2fc9 100644 --- a/commands/queue.js +++ b/commands/queue.js @@ -1,7 +1,7 @@ const soundPlayer = require("../utils/soundplayer.js"); exports.run = async (message) => { - await soundPlayer.queue(message); + return await soundPlayer.queue(message); }; exports.aliases = ["q"]; diff --git a/commands/skip.js b/commands/skip.js index 9bc4c55..337e6a1 100644 --- a/commands/skip.js +++ b/commands/skip.js @@ -1,7 +1,7 @@ const soundPlayer = require("../utils/soundplayer.js"); exports.run = async (message) => { - await soundPlayer.skip(message); + return await soundPlayer.skip(message); }; exports.category = 7; diff --git a/commands/stop.js b/commands/stop.js index 29ee1c7..22e2663 100644 --- a/commands/stop.js +++ b/commands/stop.js @@ -1,7 +1,7 @@ const soundPlayer = require("../utils/soundplayer.js"); exports.run = async (message) => { - await soundPlayer.stop(message); + return await soundPlayer.stop(message); }; exports.aliases = ["disconnect"]; diff --git a/commands/unfreeze.js b/commands/unfreeze.js new file mode 100644 index 0000000..00589a8 --- /dev/null +++ b/commands/unfreeze.js @@ -0,0 +1,21 @@ +const magick = require("../utils/image.js"); + +exports.run = async (message) => { + message.channel.sendTyping(); + const image = await require("../utils/imagedetect.js")(message); + if (image === undefined) return `${message.author.mention}, you need to provide a GIF to unfreeze!`; + const { buffer, type } = await magick.run({ + cmd: "freeze", + path: image.path, + loop: true, + onlyGIF: true + }); + if (buffer === "nogif") return `${message.author.mention}, that isn't a GIF!`; + return { + file: buffer, + name: `unfreeze.${type}` + }; +}; + +exports.category = 5; +exports.help = "Unfreezes a GIF"; \ No newline at end of file diff --git a/utils/soundplayer.js b/utils/soundplayer.js index f6c642a..519be6b 100644 --- a/utils/soundplayer.js +++ b/utils/soundplayer.js @@ -47,22 +47,22 @@ exports.connect = async () => { }; exports.play = async (sound, message, music = false) => { - if (!message.member.voiceState.channelID) return client.createMessage(message.channel.id, `${message.author.mention}, you need to be in a voice channel first!`); - if (!message.channel.guild.members.get(client.user.id).permission.has("voiceConnect") || !message.channel.permissionsOf(client.user.id).has("voiceConnect")) return client.createMessage(message.channel.id, `${message.author.mention}, I can't join this voice channel!`); + if (!message.member.voiceState.channelID) return `${message.author.mention}, you need to be in a voice channel first!`; + if (!message.channel.guild.members.get(client.user.id).permission.has("voiceConnect") || !message.channel.permissionsOf(client.user.id).has("voiceConnect")) return `${message.author.mention}, I can't join this voice channel!`; const voiceChannel = message.channel.guild.channels.get(message.member.voiceState.channelID); - if (!voiceChannel.permissionsOf(client.user.id).has("voiceConnect")) return client.createMessage(message.channel.id, `${message.author.mention}, I don't have permission to join this voice channel!`); + if (!voiceChannel.permissionsOf(client.user.id).has("voiceConnect")) return `${message.author.mention}, I don't have permission to join this voice channel!`; const player = this.players.get(message.channel.guild.id); - if (!music && this.manager.voiceStates.has(message.channel.guild.id) && (player && player.type === "music")) return client.createMessage(message.channel.id, `${message.author.mention}, I can't play a sound effect while playing music!`); + if (!music && this.manager.voiceStates.has(message.channel.guild.id) && (player && player.type === "music")) return `${message.author.mention}, I can't play a sound effect while playing music!`; const node = this.manager.idealNodes[0]; const { tracks } = await fetch(`http://${node.host}:${node.port}/loadtracks?identifier=${sound}`, { headers: { Authorization: node.password } }).then(res => res.json()); const oldQueue = this.queues.get(voiceChannel.guild.id); - if (tracks.length === 0) return client.createMessage(message.channel.id, `${message.author.mention}, I couldn't find that song!`); + if (tracks.length === 0) return `${message.author.mention}, I couldn't find that song!`; if (music) { this.queues.set(voiceChannel.guild.id, oldQueue ? [...oldQueue, tracks[0].track] : [tracks[0].track]); } let connection; if (player) { - connection = player; + connection = player.player; } else { connection = await this.manager.join({ guild: voiceChannel.guild.id, @@ -72,13 +72,14 @@ exports.play = async (sound, message, music = false) => { } if (oldQueue && music) { - client.createMessage(message.channel.id, `${message.author.mention}, your tune has been added to the queue!`); + return `${message.author.mention}, your tune has been added to the queue!`; } else { - this.nextSong(message, connection, tracks[0].track, tracks[0].info, music, voiceChannel); + this.nextSong(message, connection, tracks[0].track, tracks[0].info, music, voiceChannel, player ? player.loop : false); + return; } }; -exports.nextSong = async (message, connection, track, info, music, voiceChannel, inQueue = false) => { +exports.nextSong = async (message, connection, track, info, music, voiceChannel, loop = false, inQueue = false) => { const parts = Math.floor((0 / info.length) * 10); const playingMessage = await client.createMessage(message.channel.id, !music ? "🔊 Playing sound..." : { "embed": { @@ -106,7 +107,7 @@ exports.nextSong = async (message, connection, track, info, music, voiceChannel, } }); await connection.play(track); - this.players.set(voiceChannel.guild.id, { player: connection, type: music ? "music" : "sound", host: message.author.id, voiceChannel: voiceChannel, originalChannel: message.channel }); + this.players.set(voiceChannel.guild.id, { player: connection, type: music ? "music" : "sound", host: message.author.id, voiceChannel: voiceChannel, originalChannel: message.channel, loop: loop }); if (inQueue && connection.listeners("error").length === 0) { connection.on("error", (error) => { if (playingMessage.channel.messages.get(playingMessage.id)) playingMessage.delete(); @@ -121,7 +122,14 @@ exports.nextSong = async (message, connection, track, info, music, voiceChannel, connection.on("end", async (data) => { if (data.reason === "REPLACED") return; const queue = this.queues.get(voiceChannel.guild.id); - const newQueue = queue ? queue.slice(1) : []; + const isLooping = this.players.get(voiceChannel.guild.id).loop; + let newQueue; + if (isLooping) { + queue.push(queue.shift()); + newQueue = queue; + } else { + newQueue = queue ? queue.slice(1) : []; + } this.queues.set(voiceChannel.guild.id, newQueue); if (newQueue.length === 0) { this.manager.leave(voiceChannel.guild.id); @@ -132,7 +140,7 @@ exports.nextSong = async (message, connection, track, info, music, voiceChannel, if (playingMessage.channel.messages.get(playingMessage.id)) await playingMessage.delete(); } else { const track = await fetch(`http://${connection.node.host}:${connection.node.port}/decodetrack?track=${encodeURIComponent(newQueue[0])}`, { headers: { Authorization: connection.node.password } }).then(res => res.json()); - this.nextSong(message, connection, newQueue[0], track, music, voiceChannel, true); + this.nextSong(message, connection, newQueue[0], track, music, voiceChannel, isLooping, true); if (playingMessage.channel.messages.get(playingMessage.id)) await playingMessage.delete(); } }); @@ -140,24 +148,24 @@ exports.nextSong = async (message, connection, track, info, music, voiceChannel, }; exports.stop = async (message) => { - if (!message.member.voiceState.channelID) return client.createMessage(message.channel.id, `${message.author.mention}, you need to be in a voice channel first!`); - if (!message.channel.guild.members.get(client.user.id).voiceState.channelID) return client.createMessage(message.channel.id, `${message.author.mention}, I'm not in a voice channel!`); - if (this.players.get(message.channel.guild.id).host !== message.author.id) return client.createMessage(message.channel.id, `${message.author.mention}, only the current voice session host can stop the music!`); + if (!message.member.voiceState.channelID) return `${message.author.mention}, you need to be in a voice channel first!`; + if (!message.channel.guild.members.get(client.user.id).voiceState.channelID) return `${message.author.mention}, I'm not in a voice channel!`; + if (this.players.get(message.channel.guild.id).host !== message.author.id) return `${message.author.mention}, only the current voice session host can stop the music!`; this.manager.leave(message.channel.guild.id); const connection = this.players.get(message.channel.guild.id).player; connection.destroy(); this.players.delete(message.channel.guild.id); this.queues.delete(message.channel.guild.id); - await client.createMessage(message.channel.id, "🔊 The current voice channel session has ended."); + return "🔊 The current voice channel session has ended."; }; exports.skip = async (message) => { - if (!message.member.voiceState.channelID) return client.createMessage(message.channel.id, `${message.author.mention}, you need to be in a voice channel first!`); - if (!message.channel.guild.members.get(client.user.id).voiceState.channelID) return client.createMessage(message.channel.id, `${message.author.mention}, I'm not in a voice channel!`); + if (!message.member.voiceState.channelID) return `${message.author.mention}, you need to be in a voice channel first!`; + if (!message.channel.guild.members.get(client.user.id).voiceState.channelID) return `${message.author.mention}, I'm not in a voice channel!`; const player = this.players.get(message.channel.guild.id); if (player.host !== message.author.id) { const votes = skipVotes.has(message.channel.guild.id) ? skipVotes.get(message.channel.guild.id) : { count: 0, ids: [] }; - if (votes.ids.includes(message.author.id)) return client.createMessage(message.channel.id, `${message.author.mention}, you've already voted to skip!`); + if (votes.ids.includes(message.author.id)) return `${message.author.mention}, you've already voted to skip!`; const newObject = { count: votes.count + 1, ids: [...votes.ids, message.author.id].filter(item => !!item) @@ -166,31 +174,32 @@ exports.skip = async (message) => { player.player.stop(message.channel.guild.id); skipVotes.set(message.channel.guild.id, { count: 0, ids: [] }); } else { - await client.createMessage(message.channel.id, `🔊 Voted to skip song (${votes.count + 1}/3 people have voted).`); skipVotes.set(message.channel.guild.id, newObject); + return `🔊 Voted to skip song (${votes.count + 1}/3 people have voted).`; } } else { player.player.stop(message.channel.guild.id); + return; } }; exports.pause = async (message) => { - if (!message.member.voiceState.channelID) return client.createMessage(message.channel.id, `${message.author.mention}, you need to be in a voice channel first!`); - if (!message.channel.guild.members.get(client.user.id).voiceState.channelID) return client.createMessage(message.channel.id, `${message.author.mention}, I'm not in a voice channel!`); - if (this.players.get(message.channel.guild.id).host !== message.author.id) return client.createMessage(message.channel.id, `${message.author.mention}, only the current voice session host can pause/resume the music!`); + if (!message.member.voiceState.channelID) return `${message.author.mention}, you need to be in a voice channel first!`; + if (!message.channel.guild.members.get(client.user.id).voiceState.channelID) return `${message.author.mention}, I'm not in a voice channel!`; + if (this.players.get(message.channel.guild.id).host !== message.author.id) return `${message.author.mention}, only the current voice session host can pause/resume the music!`; const player = this.players.get(message.channel.guild.id).player; player.pause(!player.paused ? true : false); - await client.createMessage(message.channel.id, `🔊 The player has been ${!player.paused ? "paused" : "resumed"}.`); + return `🔊 The player has been ${!player.paused ? "paused" : "resumed"}.`; }; exports.playing = async (message) => { - if (!message.member.voiceState.channelID) return client.createMessage(message.channel.id, `${message.author.mention}, you need to be in a voice channel first!`); - if (!message.channel.guild.members.get(client.user.id).voiceState.channelID) return client.createMessage(message.channel.id, `${message.author.mention}, I'm not in a voice channel!`); + if (!message.member.voiceState.channelID) return `${message.author.mention}, you need to be in a voice channel first!`; + if (!message.channel.guild.members.get(client.user.id).voiceState.channelID) return `${message.author.mention}, I'm not in a voice channel!`; const player = this.players.get(message.channel.guild.id).player; - if (!player) return client.createMessage(message.channel.id, `${message.author.mention}, I'm not playing anything!`); + if (!player) return `${message.author.mention}, I'm not playing anything!`; const track = await fetch(`http://${player.node.host}:${player.node.port}/decodetrack?track=${encodeURIComponent(player.track)}`, { headers: { Authorization: player.node.password } }).then(res => res.json()); const parts = Math.floor((player.state.position / track.length) * 10); - await client.createMessage(message.channel.id, { + return { "embed": { "color": 16711680, "author": { @@ -214,17 +223,17 @@ exports.playing = async (message) => { "value": `${moment.duration(player.state.position).format("m:ss", { trim: false })}/${track.isStream ? "∞" : moment.duration(track.length).format("m:ss", { trim: false })}` }] } - }); + }; }; exports.queue = async (message) => { - if (!message.member.voiceState.channelID) return client.createMessage(message.channel.id, `${message.author.mention}, you need to be in a voice channel first!`); - if (!message.channel.guild.members.get(client.user.id).voiceState.channelID) return client.createMessage(message.channel.id, `${message.author.mention}, I'm not in a voice channel!`); + if (!message.member.voiceState.channelID) return `${message.author.mention}, you need to be in a voice channel first!`; + if (!message.channel.guild.members.get(client.user.id).voiceState.channelID) return `${message.author.mention}, I'm not in a voice channel!`; if (!message.channel.guild.members.get(client.user.id).permission.has("addReactions") && !message.channel.permissionsOf(client.user.id).has("addReactions")) return `${message.author.mention}, I don't have the \`Add Reactions\` permission!`; if (!message.channel.guild.members.get(client.user.id).permission.has("embedLinks") && !message.channel.permissionsOf(client.user.id).has("embedLinks")) return `${message.author.mention}, I don't have the \`Embed Links\` permission!`; const queue = this.queues.get(message.channel.guild.id); - const player = this.players.get(message.channel.guild.id).player; - const tracks = await fetch(`http://${player.node.host}:${player.node.port}/decodetracks`, { method: "POST", body: JSON.stringify(queue), headers: { Authorization: player.node.password, "Content-Type": "application/json" } }).then(res => res.json()); + const player = this.players.get(message.channel.guild.id); + const tracks = await fetch(`http://${player.player.node.host}:${player.player.node.port}/decodetracks`, { method: "POST", body: JSON.stringify(queue), headers: { Authorization: player.player.node.password, "Content-Type": "application/json" } }).then(res => res.json()); const trackList = []; const firstTrack = tracks.shift(); for (const [i, track] of tracks.entries()) { @@ -250,6 +259,9 @@ exports.queue = async (message) => { "fields": [{ "name": "🎶 Now Playing", "value": `${firstTrack.info.author} - **${firstTrack.info.title}** (${firstTrack.info.isStream ? "∞" : moment.duration(firstTrack.info.length).format("m:ss", { trim: false })})` + }, { + "name": "🔁 Looping?", + "value": player.loop ? "Yes" : "No" }, { "name": "🗒️ Queue", "value": value !== "del" ? value.join("\n") : "There's nothing in the queue!" @@ -259,4 +271,14 @@ exports.queue = async (message) => { } if (embeds.length === 0) return `${message.author.mention}, there's nothing in the queue!`; return paginator(message, embeds); +}; + +exports.loop = async (message) => { + if (!message.member.voiceState.channelID) return `${message.author.mention}, you need to be in a voice channel first!`; + if (!message.channel.guild.members.get(client.user.id).voiceState.channelID) return `${message.author.mention}, I'm not in a voice channel!`; + if (this.players.get(message.channel.guild.id).host !== message.author.id) return `${message.author.mention}, only the current voice session host can loop the music!`; + const object = this.players.get(message.channel.guild.id); + object.loop = !object.loop; + this.players.set(message.channel.guild.id, object); + return object.loop ? "🔊 The player is now looping." : "🔊 The player is no longer looping."; }; \ No newline at end of file