The bot can now actually interact with the API, fixed issues relating to leaving voice channels

This commit is contained in:
TheEssem 2020-09-01 21:52:12 -05:00
parent fdc08575f3
commit a6137436d2
4 changed files with 37 additions and 20 deletions

View file

@ -9,12 +9,12 @@ const storage = multer.diskStorage({
cb(null, "/tmp/"); cb(null, "/tmp/");
}, },
filename: function(req, file, cb) { 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 upload = multer({ storage: storage });
const app = express(); const app = express();
const port = 3000; const port = process.env.API_PORT || 3000;
const formats = ["image/jpeg", "image/png", "image/webp", "image/gif"]; 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 { try {
const data = await magick(object); const data = await magick(object, true);
res.contentType(type).send(data); res.send(data);
} catch (e) { } catch (e) {
next(e); next(e);
} }

View file

@ -16,6 +16,11 @@ module.exports = async (member, oldChannel) => {
} else { } else {
waitMessage.delete(); waitMessage.delete();
connection.player.stop(connection.originalChannel.guild.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);
client.createMessage(connection.originalChannel.id, "🔊 The current voice channel session has ended.");
} }
}); });
} else if (member.id === connection.host) { } else if (member.id === connection.host) {
@ -29,6 +34,11 @@ module.exports = async (member, oldChannel) => {
if (members.length === 0) { if (members.length === 0) {
waitMessage.delete(); waitMessage.delete();
connection.player.stop(connection.originalChannel.guild.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);
client.createMessage(connection.originalChannel.id, "🔊 The current voice channel session has ended.");
} else { } else {
const randomMember = random(members); 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 }); 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.");
} }
} }
}; };

View file

@ -2,22 +2,22 @@ const magick = require("../build/Release/image.node");
const fetch = require("node-fetch"); const fetch = require("node-fetch");
const { promisify } = require("util"); const { promisify } = require("util");
const FormData = require("form-data"); const FormData = require("form-data");
const { readFile } = require("fs").promises; const fs = require("fs");
module.exports = async (object) => { module.exports = async (object, fromAPI = false) => {
if (process.env.API === "true") { if (process.env.API === "true" && !fromAPI) {
const params = []; const params = [];
for (const element of Object.keys(object)) { for (const element of Object.keys(object)) {
params.push(`${element}=${object[element]}`); params.push(`${element}=${object[element]}`);
} }
const form = new FormData(); const form = new FormData();
const data = await readFile(object.path); form.append("image", fs.createReadStream(object.path));
form.append("image", data);
const req = await fetch(`${process.env.API_URL}/${object.cmd}?${params.join("&")}`, { const req = await fetch(`${process.env.API_URL}/${object.cmd}?${params.join("&")}`, {
method: "POST", method: "POST",
body: form body: form,
headers: form.getHeaders()
}); });
return await req.buffer(); return object.cmd === "qrread" ? await req.json() : await req.buffer();
} else { } else {
const data = await promisify(magick[object.cmd])(object); const data = await promisify(magick[object.cmd])(object);
return data; return data;

View file

@ -10,7 +10,7 @@ const nodes = require("../lavanodes.json");
exports.players = new Map(); exports.players = new Map();
const queues = new Map(); exports.queues = new Map();
const skipVotes = new Map(); const skipVotes = new Map();
exports.manager; 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!`); 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 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 { 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!`); 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({ const connection = await this.manager.join({
guild: voiceChannel.guild.id, guild: voiceChannel.guild.id,
channel: voiceChannel.id, channel: voiceChannel.id,
@ -105,21 +105,21 @@ exports.nextSong = async (message, connection, track, info, music, voiceChannel,
this.manager.leave(voiceChannel.guild.id); this.manager.leave(voiceChannel.guild.id);
connection.destroy(); connection.destroy();
this.players.delete(voiceChannel.guild.id); this.players.delete(voiceChannel.guild.id);
queues.delete(voiceChannel.guild.id); this.queues.delete(voiceChannel.guild.id);
logger.error(error); logger.error(error);
}); });
} }
connection.once("end", async (data) => { connection.once("end", async (data) => {
if (data.reason === "REPLACED") return; 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); const newQueue = queue.slice(1);
queues.set(voiceChannel.guild.id, newQueue); this.queues.set(voiceChannel.guild.id, newQueue);
await playingMessage.delete(); await playingMessage.delete();
if (newQueue.length === 0) { if (newQueue.length === 0) {
this.manager.leave(voiceChannel.guild.id); this.manager.leave(voiceChannel.guild.id);
connection.destroy(); connection.destroy();
this.players.delete(voiceChannel.guild.id); 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."); if (music) await client.createMessage(message.channel.id, "🔊 The current voice channel session has ended.");
} else { } 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()); 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; const connection = this.players.get(message.channel.guild.id).player;
connection.destroy(); connection.destroy();
this.players.delete(message.channel.guild.id); 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."); 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).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("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!`; 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 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 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 = []; const trackList = [];