This commit is contained in:
Ean Milligan (Bastion) 2022-06-20 20:55:40 -04:00
parent df8c31d6d3
commit 6e448907ee
2 changed files with 56 additions and 40 deletions

View File

@ -10,7 +10,7 @@ import {
Status,
STATUS_TEXT,
} from '../../../deps.ts';
import { RollModifiers, QueuedRoll } from '../../mod.d.ts';
import { QueuedRoll, RollModifiers } from '../../mod.d.ts';
import { queueRoll } from '../../solver/rollQueue.ts';
const apiWarning = `The following roll was conducted using my built in API. If someone in this channel did not request this roll, please report API abuse here: <${config.api.supportURL}>`;
@ -97,13 +97,15 @@ export const apiRoll = async (requestEvent: Deno.RequestEvent, query: Map<string
};
// Parse the roll and get the return text
await queueRoll(<QueuedRoll> {
await queueRoll(
<QueuedRoll> {
apiRoll: true,
api: { requestEvent, channelId: BigInt(query.get('channel') || '0'), userId: BigInt(query.get('user') || '') },
rollCmd,
modifiers,
originalCommand,
});
},
);
} catch (err) {
// Handle any errors we missed
log(LT.ERROR, `Unhandled Error: ${JSON.stringify(err)}`);
@ -111,10 +113,12 @@ export const apiRoll = async (requestEvent: Deno.RequestEvent, query: Map<string
}
} else {
// Alert API user that they messed up
requestEvent.respondWith(new Response(
requestEvent.respondWith(
new Response(
`Verify you are a member of the guild you are sending this roll to. If you are, the ${config.name} may not have that registered, please send a message in the guild so ${config.name} can register this. This registration is temporary, so if you see this error again, just poke your server again.`,
{ status: Status.Forbidden, statusText: STATUS_TEXT.get(Status.Forbidden) }
));
{ status: Status.Forbidden, statusText: STATUS_TEXT.get(Status.Forbidden) },
),
);
}
} else {
// Alert API user that they shouldn't be doing this

View File

@ -35,10 +35,12 @@ const handleRollWorker = async (rq: QueuedRoll) => {
rollWorker.terminate();
currentWorkers--;
if (rq.apiRoll) {
rq.api.requestEvent.respondWith(new Response(
rq.api.requestEvent.respondWith(
new Response(
'Roll took too long to process, try breaking roll down into simpler parts',
{ status: Status.RequestTimeout, statusText: STATUS_TEXT.get(Status.RequestTimeout) }
));
{ status: Status.RequestTimeout, statusText: STATUS_TEXT.get(Status.RequestTimeout) },
),
);
} else {
rq.dd.m.edit({
embeds: [
@ -74,10 +76,12 @@ const handleRollWorker = async (rq: QueuedRoll) => {
// If there was an error, report it to the user in hopes that they can determine what they did wrong
if (returnmsg.error) {
if (rq.apiRoll) {
rq.api.requestEvent.respondWith(new Response(
rq.api.requestEvent.respondWith(
new Response(
returnmsg.errorMsg,
{ status: Status.InternalServerError, statusText: STATUS_TEXT.get(Status.InternalServerError) }
));
{ status: Status.InternalServerError, statusText: STATUS_TEXT.get(Status.InternalServerError) },
),
);
} else {
rq.dd.m.edit({ embeds: [pubEmbedDetails.embed] });
}
@ -95,13 +99,15 @@ const handleRollWorker = async (rq: QueuedRoll) => {
if (rq.apiRoll) {
n = await sendMessage(rq.api.channelId, {
content: rq.modifiers.apiWarn,
embeds: [pubEmbedDetails.embed]
embeds: [pubEmbedDetails.embed],
}).catch(() => {
apiErroredOut = true;
rq.api.requestEvent.respondWith(new Response(
rq.api.requestEvent.respondWith(
new Response(
'Message failed to send.',
{ status: Status.InternalServerError, statusText: STATUS_TEXT.get(Status.InternalServerError) }
));
{ status: Status.InternalServerError, statusText: STATUS_TEXT.get(Status.InternalServerError) },
),
);
});
} else {
// Send the public embed to correct channel
@ -142,13 +148,15 @@ const handleRollWorker = async (rq: QueuedRoll) => {
if (rq.apiRoll) {
n = await sendMessage(rq.api.channelId, {
content: rq.modifiers.apiWarn,
embeds: rq.modifiers.count ? [pubEmbedDetails.embed, countEmbed] : [pubEmbedDetails.embed]
embeds: rq.modifiers.count ? [pubEmbedDetails.embed, countEmbed] : [pubEmbedDetails.embed],
}).catch(() => {
apiErroredOut = true;
rq.api.requestEvent.respondWith(new Response(
rq.api.requestEvent.respondWith(
new Response(
'Message failed to send.',
{ status: Status.InternalServerError, statusText: STATUS_TEXT.get(Status.InternalServerError) }
));
{ status: Status.InternalServerError, statusText: STATUS_TEXT.get(Status.InternalServerError) },
),
);
});
} else {
n = await rq.dd.m.edit({
@ -165,21 +173,25 @@ const handleRollWorker = async (rq: QueuedRoll) => {
}
if (!apiErroredOut) {
rq.api.requestEvent.respondWith(new Response(
rq.api.requestEvent.respondWith(
new Response(
JSON.stringify(
rq.modifiers.count ? { counts: countEmbed, details: pubEmbedDetails } : { details: pubEmbedDetails }
rq.modifiers.count ? { counts: countEmbed, details: pubEmbedDetails } : { details: pubEmbedDetails },
),
{ status: Status.OK, statusText: STATUS_TEXT.get(Status.OK) }
));
{ status: Status.OK, statusText: STATUS_TEXT.get(Status.OK) },
),
);
}
}
} catch (e) {
log(LT.ERROR, `Unddandled Error: ${JSON.stringify(e)}`);
if (rq.apiRoll && !apiErroredOut) {
rq.api.requestEvent.respondWith(new Response(
rq.api.requestEvent.respondWith(
new Response(
JSON.stringify(e),
{ status: Status.InternalServerError, statusText: STATUS_TEXT.get(Status.InternalServerError) }
));
{ status: Status.InternalServerError, statusText: STATUS_TEXT.get(Status.InternalServerError) },
),
);
}
}
});