improve api response for /roll

This commit is contained in:
Ean Milligan 2025-07-23 00:16:33 -04:00
parent d6c5dfee77
commit a37f5eb7d5
2 changed files with 39 additions and 21 deletions

View File

@ -25,6 +25,7 @@ import stdResp from 'endpoints/stdResponses.ts';
import { InteractionValueSeparator } from 'events/interactionCreate.ts'; import { InteractionValueSeparator } from 'events/interactionCreate.ts';
import utils from 'utils/utils.ts'; import utils from 'utils/utils.ts';
import { STATUS_CODE, STATUS_TEXT } from '@std/http/status';
const getUserIdForEmbed = (rollRequest: QueuedRoll): bigint => { const getUserIdForEmbed = (rollRequest: QueuedRoll): bigint => {
if (rollRequest.apiRoll) return rollRequest.api.userId; if (rollRequest.apiRoll) return rollRequest.api.userId;
@ -60,10 +61,11 @@ export const onWorkerComplete = async (workerMessage: MessageEvent<SolvedRoll>,
const gmEmbeds: Embed[] = [gmEmbedDetails.embed]; const gmEmbeds: Embed[] = [gmEmbedDetails.embed];
const pubAttachments: FileContent[] = pubEmbedDetails.hasAttachment ? [pubEmbedDetails.attachment] : []; const pubAttachments: FileContent[] = pubEmbedDetails.hasAttachment ? [pubEmbedDetails.attachment] : [];
const gmAttachments: FileContent[] = gmEmbedDetails.hasAttachment ? [gmEmbedDetails.attachment] : []; const gmAttachments: FileContent[] = gmEmbedDetails.hasAttachment ? [gmEmbedDetails.attachment] : [];
let countEmbed, rollDistEmbed;
// Handle adding count embed to correct list // Handle adding count embed to correct list
if (rollRequest.modifiers.count) { if (rollRequest.modifiers.count) {
const countEmbed = generateCountDetailsEmbed(returnMsg.counts); countEmbed = generateCountDetailsEmbed(returnMsg.counts);
if (rollRequest.modifiers.gmRoll) { if (rollRequest.modifiers.gmRoll) {
gmEmbeds.push(countEmbed.embed); gmEmbeds.push(countEmbed.embed);
gmRespCharCount += countEmbed.charCount; gmRespCharCount += countEmbed.charCount;
@ -75,7 +77,7 @@ export const onWorkerComplete = async (workerMessage: MessageEvent<SolvedRoll>,
// Handle adding rollDist embed to correct list // Handle adding rollDist embed to correct list
if (rollRequest.modifiers.rollDist) { if (rollRequest.modifiers.rollDist) {
const rollDistEmbed = generateRollDistsEmbed(returnMsg.rollDistributions); rollDistEmbed = generateRollDistsEmbed(returnMsg.rollDistributions);
if (rollRequest.modifiers.gmRoll) { if (rollRequest.modifiers.gmRoll) {
gmEmbeds.push(rollDistEmbed.embed); gmEmbeds.push(rollDistEmbed.embed);
rollDistEmbed.hasAttachment && gmAttachments.push(rollDistEmbed.attachment); rollDistEmbed.hasAttachment && gmAttachments.push(rollDistEmbed.attachment);
@ -248,20 +250,33 @@ Please click on "<@${botId}> *Click to see attachment*" above this message to se
.execute(queries.insertRollLogCmd(1, 0), [rollRequest.originalCommand, returnMsg.errorCode, newMsg ? newMsg.id : null]) .execute(queries.insertRollLogCmd(1, 0), [rollRequest.originalCommand, returnMsg.errorCode, newMsg ? newMsg.id : null])
.catch((e) => utils.commonLoggers.dbError('rollQueue.ts:155', 'insert into', e)); .catch((e) => utils.commonLoggers.dbError('rollQueue.ts:155', 'insert into', e));
const headers = new Headers();
headers.append('Content-Type', 'text/json');
apiResolve && apiResolve &&
apiResolve( apiResolve(
stdResp.OK( new Response(
JSON.stringify( JSON.stringify({
rollRequest.modifiers.count discordEmbeds: {
? { rollResponse: pubEmbedDetails,
counts: returnMsg.counts, countsResponse: countEmbed ?? null,
details: pubEmbedDetails, rollDistResponse: rollDistEmbed ?? null,
}
: {
details: pubEmbedDetails,
}, },
), rawData: {
), roll: {
raw: returnMsg.line1,
results: returnMsg.line2,
details: returnMsg.line3,
},
counts: rollRequest.modifiers.count ? returnMsg.counts : null,
rollDistributions: returnMsg.rollDistributions.entries().toArray(),
},
}),
{
status: STATUS_CODE.OK,
statusText: STATUS_TEXT[STATUS_CODE.OK],
headers,
}
)
); );
} }
} catch (e) { } catch (e) {
@ -274,11 +289,10 @@ Please click on "<@${botId}> *Click to see attachment*" above this message to se
rollRequest.dd.authorId, rollRequest.dd.authorId,
<SolvedRoll>{ <SolvedRoll>{
error: true, error: true,
errorMsg: errorMsg: `Something weird went wrong, likely the requested roll is too complex and caused the response to be too large for Discord. Try breaking the request down into smaller messages and try again.\n\nIf this error continues to come up, please \`${config.prefix}report\` this to my developer.`,
`Something weird went wrong, likely the requested roll is too complex and caused the response to be too large for Discord. Try breaking the request down into smaller messages and try again.\n\nIf this error continues to come up, please \`${config.prefix}report\` this to my developer.`,
errorCode: 'UnhandledWorkerComplete', errorCode: 'UnhandledWorkerComplete',
}, },
<RollModifiers> {}, <RollModifiers>{}
) )
).embed, ).embed,
], ],

View File

@ -119,7 +119,7 @@ export const apiRoll = async (query: Map<string, string>, apiUserid: bigint): Pr
const [name, rawSides] = shape.split(':').filter((x) => x); const [name, rawSides] = shape.split(':').filter((x) => x);
if (!name || !rawSides) { if (!name || !rawSides) {
return stdResp.BadRequest( return stdResp.BadRequest(
'cd specified with invalid pattern. Must be in format of `name:[side1,side2,...,sideN]`. If multiple custom dice shapes are needed, use a `;` to separate the list', 'cd specified with invalid pattern. Must be in format of `name:[side1,side2,...,sideN]`. If multiple custom dice shapes are needed, use a `;` to separate the list'
); );
} }
@ -181,9 +181,13 @@ export const apiRoll = async (query: Map<string, string>, apiUserid: bigint): Pr
} }
} else { } else {
// Alert API user that they messed up // Alert API user that they messed up
return stdResp.Forbidden( return stdResp.Forbidden(`Verify you are a member of the guild you are sending this roll to.
`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.`,
); If you are, ${config.name} may not have that guild actively cached.
Please send a message in the guild so ${config.name} can cache this guild. ${config.name}'s cache will clear out inactive guilds after some time, so if you see this error again, just poke the guild again.
If you still see this error after "poking" your guild, please verify the channel allows API Rolls (${config.prefix}api status).`);
} }
} else { } else {
// Alert API user that they shouldn't be doing this // Alert API user that they shouldn't be doing this