From a6137436d20df50dde59249c8ebdd4846d9e2578 Mon Sep 17 00:00:00 2001 From: TheEssem Date: Tue, 1 Sep 2020 21:52:12 -0500 Subject: [PATCH] The bot can now actually interact with the API, fixed issues relating to leaving voice channels --- api/index.js | 8 ++++---- events/voiceChannelLeave.js | 17 +++++++++++++++++ utils/image.js | 14 +++++++------- utils/soundplayer.js | 18 +++++++++--------- 4 files changed, 37 insertions(+), 20 deletions(-) diff --git a/api/index.js b/api/index.js index 5f23a3a..744ae10 100644 --- a/api/index.js +++ b/api/index.js @@ -9,12 +9,12 @@ const storage = multer.diskStorage({ cb(null, "/tmp/"); }, filename: function(req, file, cb) { - cb(null, Date.now() + path.extname(file.originalname)); //Appending extension + cb(null, Date.now() + path.extname(file.originalname)); } }); const upload = multer({ storage: storage }); const app = express(); -const port = 3000; +const port = process.env.API_PORT || 3000; const formats = ["image/jpeg", "image/png", "image/webp", "image/gif"]; @@ -39,8 +39,8 @@ app.post("/:method", upload.single("image"), async (req, res, next) => { } try { - const data = await magick(object); - res.contentType(type).send(data); + const data = await magick(object, true); + res.send(data); } catch (e) { next(e); } diff --git a/events/voiceChannelLeave.js b/events/voiceChannelLeave.js index 519d0f5..508cdf5 100644 --- a/events/voiceChannelLeave.js +++ b/events/voiceChannelLeave.js @@ -16,6 +16,11 @@ module.exports = async (member, oldChannel) => { } else { waitMessage.delete(); connection.player.stop(connection.originalChannel.guild.id); + soundPlayer.manager.leave(connection.originalChannel.guild.id); + connection.player.destroy(); + soundPlayer.players.delete(connection.originalChannel.guild.id); + soundPlayer.queues.delete(connection.originalChannel.guild.id); + client.createMessage(connection.originalChannel.id, "🔊 The current voice channel session has ended."); } }); } else if (member.id === connection.host) { @@ -29,6 +34,11 @@ module.exports = async (member, oldChannel) => { if (members.length === 0) { waitMessage.delete(); connection.player.stop(connection.originalChannel.guild.id); + soundPlayer.manager.leave(connection.originalChannel.guild.id); + connection.player.destroy(); + soundPlayer.players.delete(connection.originalChannel.guild.id); + soundPlayer.queues.delete(connection.originalChannel.guild.id); + client.createMessage(connection.originalChannel.id, "🔊 The current voice channel session has ended."); } else { const randomMember = random(members); soundPlayer.players.set(connection.voiceChannel.guild.id, { player: connection.player, type: connection.type, host: randomMember.id, voiceChannel: connection.voiceChannel, originalChannel: connection.originalChannel }); @@ -36,6 +46,13 @@ module.exports = async (member, oldChannel) => { } } }); + } else if (member.id === client.user.id) { + connection.player.stop(connection.originalChannel.guild.id); + soundPlayer.manager.leave(connection.originalChannel.guild.id); + connection.player.destroy(); + soundPlayer.players.delete(connection.originalChannel.guild.id); + soundPlayer.queues.delete(connection.originalChannel.guild.id); + await client.createMessage(connection.originalChannel.id, "🔊 The current voice channel session has ended."); } } }; \ No newline at end of file diff --git a/utils/image.js b/utils/image.js index 584abe0..3df6d4b 100644 --- a/utils/image.js +++ b/utils/image.js @@ -2,22 +2,22 @@ const magick = require("../build/Release/image.node"); const fetch = require("node-fetch"); const { promisify } = require("util"); const FormData = require("form-data"); -const { readFile } = require("fs").promises; +const fs = require("fs"); -module.exports = async (object) => { - if (process.env.API === "true") { +module.exports = async (object, fromAPI = false) => { + if (process.env.API === "true" && !fromAPI) { const params = []; for (const element of Object.keys(object)) { params.push(`${element}=${object[element]}`); } const form = new FormData(); - const data = await readFile(object.path); - form.append("image", data); + form.append("image", fs.createReadStream(object.path)); const req = await fetch(`${process.env.API_URL}/${object.cmd}?${params.join("&")}`, { method: "POST", - body: form + body: form, + headers: form.getHeaders() }); - return await req.buffer(); + return object.cmd === "qrread" ? await req.json() : await req.buffer(); } else { const data = await promisify(magick[object.cmd])(object); return data; diff --git a/utils/soundplayer.js b/utils/soundplayer.js index 9570ba9..3c700be 100644 --- a/utils/soundplayer.js +++ b/utils/soundplayer.js @@ -10,7 +10,7 @@ const nodes = require("../lavanodes.json"); exports.players = new Map(); -const queues = new Map(); +exports.queues = new Map(); const skipVotes = new Map(); exports.manager; @@ -54,9 +54,9 @@ exports.play = async (sound, message, music = false) => { if (!music && this.manager.voiceStates.has(message.channel.guild.id) && this.players.get(message.channel.guild.id).type === "music") return client.createMessage(message.channel.id, `${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 = queues.get(voiceChannel.guild.id); + 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!`); - queues.set(voiceChannel.guild.id, oldQueue ? [...oldQueue, tracks[0].track] : [tracks[0].track]); + this.queues.set(voiceChannel.guild.id, oldQueue ? [...oldQueue, tracks[0].track] : [tracks[0].track]); const connection = await this.manager.join({ guild: voiceChannel.guild.id, channel: voiceChannel.id, @@ -105,21 +105,21 @@ exports.nextSong = async (message, connection, track, info, music, voiceChannel, this.manager.leave(voiceChannel.guild.id); connection.destroy(); this.players.delete(voiceChannel.guild.id); - queues.delete(voiceChannel.guild.id); + this.queues.delete(voiceChannel.guild.id); logger.error(error); }); } connection.once("end", async (data) => { if (data.reason === "REPLACED") return; - const queue = queues.get(voiceChannel.guild.id); + const queue = this.queues.get(voiceChannel.guild.id); const newQueue = queue.slice(1); - queues.set(voiceChannel.guild.id, newQueue); + this.queues.set(voiceChannel.guild.id, newQueue); await playingMessage.delete(); if (newQueue.length === 0) { this.manager.leave(voiceChannel.guild.id); connection.destroy(); this.players.delete(voiceChannel.guild.id); - queues.delete(voiceChannel.guild.id); + this.queues.delete(voiceChannel.guild.id); if (music) await client.createMessage(message.channel.id, "🔊 The current voice channel session has ended."); } 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()); @@ -136,7 +136,7 @@ exports.stop = async (message) => { const connection = this.players.get(message.channel.guild.id).player; connection.destroy(); this.players.delete(message.channel.guild.id); - queues.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."); }; @@ -206,7 +206,7 @@ exports.queue = async (message) => { 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.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 = queues.get(message.channel.guild.id); + 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 trackList = [];