Caption now supports more unicode characters, more api work, fixed multiple bugs

This commit is contained in:
TheEssem 2020-11-05 15:40:18 -06:00
parent a03d3a5e79
commit 0600cf230f
11 changed files with 174 additions and 50 deletions

View file

@ -4,32 +4,45 @@ const { promisify } = require("util");
const AbortController = require("abort-controller");
const fileType = require("file-type");
const execPromise = promisify(require("child_process").exec);
const servers = require("../servers.json").image;
const formats = ["image/jpeg", "image/png", "image/webp", "image/gif"];
exports.run = async (object, fromAPI = false) => {
if (process.env.API === "true" && !fromAPI) {
const req = await fetch(`${process.env.API_URL}/run`, {
const currentServer = servers[Math.floor(Math.random() * servers.length)];
const req = await fetch(`${currentServer}/run`, {
method: "POST",
body: JSON.stringify(object),
headers: {
"Content-Type": "application/json"
}
});
const buffer = await req.buffer();
console.log(buffer.toString());
if (buffer.toString() === "nogif") return {
const json = await req.json();
if (json.status === "nogif") return {
buffer: "nogif",
type: null
};
return {
buffer: buffer,
type: req.headers.get("content-type").split("/")[1]
};
let data;
while (!data) {
const statusReq = await fetch(`${currentServer}/status?id=${json.id}`);
const statusJSON = await statusReq.json();
if (statusJSON.status === "success") {
const imageReq = await fetch(`${currentServer}/image?id=${json.id}`);
data = {
buffer: await imageReq.buffer(),
type: imageReq.headers.get("content-type").split("/")[1]
};
} else if (statusJSON.status === "error") {
throw new Error(statusJSON.error);
}
}
return data;
} else {
let type;
if (!fromAPI && object.path) {
type = (object.type ? object.type : await this.getType(object.path)).split("/")[1];
const newType = (object.type ? object.type : await this.getType(object.path));
type = newType ? newType.split("/")[1] : "png";
if (type !== "gif" && object.onlyGIF) return {
buffer: "nogif",
type: null

View file

@ -1,4 +1,5 @@
const fetch = require("node-fetch");
const execPromise = require("util").promisify(require("child_process").exec);
// gets the proper image paths
const getImage = async (image, image2, gifv = false) => {
@ -8,10 +9,15 @@ const getImage = async (image, image2, gifv = false) => {
path: image
};
if (gifv) {
if (image2.includes("tenor.com") && process.env.TENOR !== "") {
const data = await fetch(`https://api.tenor.com/v1/gifs?ids=${image2.split("-").pop()}&key=${process.env.TENOR}`);
const json = await data.json();
payload.path = json.results[0].media[0].gif.url;
if (image2.includes("tenor.com")) {
if (process.env.TENOR !== "") {
const data = await fetch(`https://api.tenor.com/v1/gifs?ids=${image2.split("-").pop()}&key=${process.env.TENOR}`);
const json = await data.json();
payload.path = json.results[0].media[0].gif.url;
} else {
const delay = (await execPromise(`ffprobe -v 0 -of csv=p=0 -select_streams v:0 -show_entries stream=r_frame_rate ${image}`)).stdout.replace("\n", "");
payload.delay = (100 / delay.split("/")[0]) * delay.split("/")[1];
}
} else if (image2.includes("giphy.com")) {
payload.path = `https://media0.giphy.com/media/${image2.split("-").pop()}/giphy.gif`;
} else if (image2.includes("imgur.com")) {

View file

@ -6,7 +6,7 @@ const moment = require("moment");
require("moment-duration-format");
const { Manager } = require("@lavacord/eris");
const nodes = require("../lavanodes.json");
const nodes = require("../servers.json").lava;
exports.players = new Map();
@ -51,7 +51,8 @@ exports.play = async (sound, message, music = false) => {
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!`);
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 (!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 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!`);
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);
@ -59,11 +60,16 @@ exports.play = async (sound, message, music = false) => {
if (music) {
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,
node: node.id
});
let connection;
if (player) {
connection = player;
} else {
connection = await this.manager.join({
guild: voiceChannel.guild.id,
channel: voiceChannel.id,
node: node.id
});
}
if (oldQueue && music) {
client.createMessage(message.channel.id, `${message.author.mention}, your tune has been added to the queue!`);
@ -101,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 });
if (inQueue) {
if (inQueue && connection.listeners("error").length === 0) {
connection.on("error", (error) => {
if (playingMessage.channel.messages.get(playingMessage.id)) playingMessage.delete();
this.manager.leave(voiceChannel.guild.id);
@ -111,24 +117,26 @@ exports.nextSong = async (message, connection, track, info, music, voiceChannel,
logger.error(error);
});
}
connection.on("end", async (data) => {
if (data.reason === "REPLACED") return;
const queue = this.queues.get(voiceChannel.guild.id);
const newQueue = queue ? queue.slice(1) : [];
this.queues.set(voiceChannel.guild.id, newQueue);
if (newQueue.length === 0) {
this.manager.leave(voiceChannel.guild.id);
connection.destroy();
this.players.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 (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);
if (playingMessage.channel.messages.get(playingMessage.id)) await playingMessage.delete();
}
});
if (connection.listeners("end").length === 0) {
connection.on("end", async (data) => {
if (data.reason === "REPLACED") return;
const queue = this.queues.get(voiceChannel.guild.id);
const newQueue = queue ? queue.slice(1) : [];
this.queues.set(voiceChannel.guild.id, newQueue);
if (newQueue.length === 0) {
this.manager.leave(voiceChannel.guild.id);
connection.destroy();
this.players.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 (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);
if (playingMessage.channel.messages.get(playingMessage.id)) await playingMessage.delete();
}
});
}
};
exports.stop = async (message) => {