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,10 +10,10 @@ import {
Status, Status,
STATUS_TEXT, STATUS_TEXT,
} from '../../../deps.ts'; } from '../../../deps.ts';
import { RollModifiers, QueuedRoll } from '../../mod.d.ts'; import { QueuedRoll, RollModifiers } from '../../mod.d.ts';
import { queueRoll } from '../../solver/rollQueue.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}>`; 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}>`;
export const apiRoll = async (requestEvent: Deno.RequestEvent, query: Map<string, string>, apiUserid: BigInt) => { export const apiRoll = async (requestEvent: Deno.RequestEvent, query: Map<string, string>, apiUserid: BigInt) => {
// Make sure query contains all the needed parts // Make sure query contains all the needed parts
@ -97,13 +97,15 @@ export const apiRoll = async (requestEvent: Deno.RequestEvent, query: Map<string
}; };
// Parse the roll and get the return text // Parse the roll and get the return text
await queueRoll(<QueuedRoll> { await queueRoll(
apiRoll: true, <QueuedRoll> {
api: { requestEvent, channelId: BigInt(query.get('channel') || '0'), userId: BigInt(query.get('user') || '')}, apiRoll: true,
rollCmd, api: { requestEvent, channelId: BigInt(query.get('channel') || '0'), userId: BigInt(query.get('user') || '') },
modifiers, rollCmd,
originalCommand, modifiers,
}); originalCommand,
},
);
} catch (err) { } catch (err) {
// Handle any errors we missed // Handle any errors we missed
log(LT.ERROR, `Unhandled Error: ${JSON.stringify(err)}`); log(LT.ERROR, `Unhandled Error: ${JSON.stringify(err)}`);
@ -111,10 +113,12 @@ export const apiRoll = async (requestEvent: Deno.RequestEvent, query: Map<string
} }
} else { } else {
// Alert API user that they messed up // Alert API user that they messed up
requestEvent.respondWith(new Response( requestEvent.respondWith(
`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.`, new Response(
{ status: Status.Forbidden, statusText: STATUS_TEXT.get(Status.Forbidden) } `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) },
),
);
} }
} else { } else {
// Alert API user that they shouldn't be doing this // Alert API user that they shouldn't be doing this

View File

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