Retry image jobs on fail, deafen the bot when playing audio
This commit is contained in:
parent
7248621048
commit
593c132555
4 changed files with 27 additions and 15 deletions
|
@ -89,10 +89,12 @@ class ImageConnection {
|
||||||
}
|
}
|
||||||
|
|
||||||
async onClose() {
|
async onClose() {
|
||||||
for (const promise of this.requests.values()) {
|
for (const [tag, obj] of this.requests.entries()) {
|
||||||
promise.reject("Request ended prematurely due to a closed connection");
|
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) {
|
if (!this.disconnected) {
|
||||||
logger.warn(`Lost connection to ${this.host}, attempting to reconnect in 5 seconds...`);
|
logger.warn(`Lost connection to ${this.host}, attempting to reconnect in 5 seconds...`);
|
||||||
await setTimeout(5000);
|
await setTimeout(5000);
|
||||||
|
@ -117,19 +119,19 @@ class ImageConnection {
|
||||||
const str = JSON.stringify(jobobj);
|
const str = JSON.stringify(jobobj);
|
||||||
const buf = Buffer.alloc(4);
|
const buf = Buffer.alloc(4);
|
||||||
buf.writeUint32LE(jobid);
|
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) {
|
wait(jobid) {
|
||||||
const buf = Buffer.alloc(4);
|
const buf = Buffer.alloc(4);
|
||||||
buf.writeUint32LE(jobid);
|
buf.writeUint32LE(jobid);
|
||||||
return this.do(Twait, buf);
|
return this.do(Twait, jobid, buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
cancel(jobid) {
|
cancel(jobid) {
|
||||||
const buf = Buffer.alloc(4);
|
const buf = Buffer.alloc(4);
|
||||||
buf.writeUint32LE(jobid);
|
buf.writeUint32LE(jobid);
|
||||||
return this.do(Tcancel, buf);
|
return this.do(Tcancel, jobid, buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
async getOutput(jobid) {
|
async getOutput(jobid) {
|
||||||
|
@ -157,7 +159,7 @@ class ImageConnection {
|
||||||
return { buffer: Buffer.from(await req.arrayBuffer()), type };
|
return { buffer: Buffer.from(await req.arrayBuffer()), type };
|
||||||
}
|
}
|
||||||
|
|
||||||
async do(op, data) {
|
async do(op, id, data) {
|
||||||
const buf = Buffer.alloc(1 + 2);
|
const buf = Buffer.alloc(1 + 2);
|
||||||
let tag = this.tag++;
|
let tag = this.tag++;
|
||||||
if (tag > 65535) tag = this.tag = 0;
|
if (tag > 65535) tag = this.tag = 0;
|
||||||
|
@ -165,7 +167,7 @@ class ImageConnection {
|
||||||
buf.writeUint16LE(tag, 1);
|
buf.writeUint16LE(tag, 1);
|
||||||
this.conn.send(Buffer.concat([buf, data]));
|
this.conn.send(Buffer.concat([buf, data]));
|
||||||
const promise = new Promise((resolve, reject) => {
|
const promise = new Promise((resolve, reject) => {
|
||||||
this.requests.set(tag, { resolve, reject });
|
this.requests.set(tag, { resolve, reject, id, op });
|
||||||
});
|
});
|
||||||
return promise;
|
return promise;
|
||||||
}
|
}
|
||||||
|
|
|
@ -67,7 +67,7 @@ const getImage = async (image, image2, video, extraReturnTypes, gifv = false) =>
|
||||||
}
|
}
|
||||||
} else if (giphyURLs.includes(host)) {
|
} else if (giphyURLs.includes(host)) {
|
||||||
// Can result in an HTML page instead of a GIF
|
// 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)) {
|
} else if (giphyMediaURLs.includes(host)) {
|
||||||
payload.path = `https://media0.giphy.com/media/${image2.split("/")[4]}/giphy.gif`;
|
payload.path = `https://media0.giphy.com/media/${image2.split("/")[4]}/giphy.gif`;
|
||||||
} else if (imgurURLs.includes(host)) {
|
} else if (imgurURLs.includes(host)) {
|
||||||
|
|
|
@ -114,11 +114,21 @@ class ImageWorker extends BaseServiceWorker {
|
||||||
if (process.env.API === "true") {
|
if (process.env.API === "true") {
|
||||||
let num = this.nextID++;
|
let num = this.nextID++;
|
||||||
if (num > 4294967295) num = this.nextID = 0;
|
if (num > 4294967295) num = this.nextID = 0;
|
||||||
|
for (let i = 0; i < 3; i++) {
|
||||||
const currentServer = await this.getIdeal(object);
|
const currentServer = await this.getIdeal(object);
|
||||||
|
try {
|
||||||
await currentServer.queue(num, object);
|
await currentServer.queue(num, object);
|
||||||
await currentServer.wait(num);
|
await currentServer.wait(num);
|
||||||
const output = await currentServer.getOutput(num);
|
const output = await currentServer.getOutput(num);
|
||||||
return output;
|
return output;
|
||||||
|
} catch (e) {
|
||||||
|
if (i < 2 && e === "Request ended prematurely due to a closed connection") {
|
||||||
|
continue;
|
||||||
|
} else {
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
// Called from command (not using image API)
|
// Called from command (not using image API)
|
||||||
const worker = new Worker(path.join(path.dirname(fileURLToPath(import.meta.url)), "../image-runner.js"), {
|
const worker = new Worker(path.join(path.dirname(fileURLToPath(import.meta.url)), "../image-runner.js"), {
|
||||||
|
|
|
@ -87,7 +87,7 @@ export async function play(client, sound, message, music = false) {
|
||||||
guild: voiceChannel.guild.id,
|
guild: voiceChannel.guild.id,
|
||||||
channel: voiceChannel.id,
|
channel: voiceChannel.id,
|
||||||
node: node.id
|
node: node.id
|
||||||
});
|
}, { selfdeaf: true });
|
||||||
|
|
||||||
if (oldQueue && oldQueue.length !== 0 && music) {
|
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!`;
|
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!`;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue