Retry image jobs on fail, deafen the bot when playing audio

This commit is contained in:
Essem 2022-01-26 15:24:10 -06:00
parent 7248621048
commit 593c132555
No known key found for this signature in database
GPG Key ID: 7D497397CC3A2A8C
4 changed files with 27 additions and 15 deletions

View File

@ -89,10 +89,12 @@ class ImageConnection {
}
async onClose() {
for (const promise of this.requests.values()) {
promise.reject("Request ended prematurely due to a closed connection");
for (const [tag, obj] of this.requests.entries()) {
obj.reject("Request ended prematurely due to a closed connection");
this.requests.delete(tag);
if (obj.op === Twait || obj.op === Tcancel) this.njobs--;
}
this.requests.clear();
//this.requests.clear();
if (!this.disconnected) {
logger.warn(`Lost connection to ${this.host}, attempting to reconnect in 5 seconds...`);
await setTimeout(5000);
@ -117,19 +119,19 @@ class ImageConnection {
const str = JSON.stringify(jobobj);
const buf = Buffer.alloc(4);
buf.writeUint32LE(jobid);
return this.do(Tqueue, Buffer.concat([buf, Buffer.from(str)]));
return this.do(Tqueue, jobid, Buffer.concat([buf, Buffer.from(str)]));
}
wait(jobid) {
const buf = Buffer.alloc(4);
buf.writeUint32LE(jobid);
return this.do(Twait, buf);
return this.do(Twait, jobid, buf);
}
cancel(jobid) {
const buf = Buffer.alloc(4);
buf.writeUint32LE(jobid);
return this.do(Tcancel, buf);
return this.do(Tcancel, jobid, buf);
}
async getOutput(jobid) {
@ -157,7 +159,7 @@ class ImageConnection {
return { buffer: Buffer.from(await req.arrayBuffer()), type };
}
async do(op, data) {
async do(op, id, data) {
const buf = Buffer.alloc(1 + 2);
let tag = this.tag++;
if (tag > 65535) tag = this.tag = 0;
@ -165,7 +167,7 @@ class ImageConnection {
buf.writeUint16LE(tag, 1);
this.conn.send(Buffer.concat([buf, data]));
const promise = new Promise((resolve, reject) => {
this.requests.set(tag, { resolve, reject });
this.requests.set(tag, { resolve, reject, id, op });
});
return promise;
}

View File

@ -67,7 +67,7 @@ const getImage = async (image, image2, video, extraReturnTypes, gifv = false) =>
}
} else if (giphyURLs.includes(host)) {
// Can result in an HTML page instead of a GIF
payload.path = `https://media0.giphy.com/media/${image2.split("-").pop()}/giphy.gif`;
payload.path = `https://media0.giphy.com/media/${image2.split("/")[4].split("-").pop()}/giphy.gif`;
} else if (giphyMediaURLs.includes(host)) {
payload.path = `https://media0.giphy.com/media/${image2.split("/")[4]}/giphy.gif`;
} else if (imgurURLs.includes(host)) {

View File

@ -114,11 +114,21 @@ class ImageWorker extends BaseServiceWorker {
if (process.env.API === "true") {
let num = this.nextID++;
if (num > 4294967295) num = this.nextID = 0;
const currentServer = await this.getIdeal(object);
await currentServer.queue(num, object);
await currentServer.wait(num);
const output = await currentServer.getOutput(num);
return output;
for (let i = 0; i < 3; i++) {
const currentServer = await this.getIdeal(object);
try {
await currentServer.queue(num, object);
await currentServer.wait(num);
const output = await currentServer.getOutput(num);
return output;
} catch (e) {
if (i < 2 && e === "Request ended prematurely due to a closed connection") {
continue;
} else {
throw e;
}
}
}
} else {
// Called from command (not using image API)
const worker = new Worker(path.join(path.dirname(fileURLToPath(import.meta.url)), "../image-runner.js"), {

View File

@ -87,7 +87,7 @@ export async function play(client, sound, message, music = false) {
guild: voiceChannel.guild.id,
channel: voiceChannel.id,
node: node.id
});
}, { selfdeaf: true });
if (oldQueue && oldQueue.length !== 0 && music) {
return `Your ${playlistInfo.name ? "playlist" : "tune"} \`${playlistInfo.name ? playlistInfo.name.trim() : (tracks[0].info.title !== "" ? tracks[0].info.title.trim() : "(blank)")}\` has been added to the queue!`;