Cleaned up API responses to make them more standard and easier to customize

This commit is contained in:
Ean Milligan (Bastion) 2022-06-21 21:33:24 -04:00
parent 0cabfe0c99
commit d45cc89eec
12 changed files with 103 additions and 130 deletions

View File

@ -9,12 +9,10 @@ import {
// Log4Deno deps // Log4Deno deps
log, log,
LT, LT,
// httpd deps
Status,
STATUS_TEXT,
} from '../deps.ts'; } from '../deps.ts';
import { dbClient } from './db.ts'; import { dbClient } from './db.ts';
import endpoints from './endpoints/_index.ts'; import endpoints from './endpoints/_index.ts';
import stdResp from './endpoints/stdResponses.ts';
// start() returns nothing // start() returns nothing
// start initializes and runs the entire API for the bot // start initializes and runs the entire API for the bot
@ -112,7 +110,7 @@ const start = async (): Promise<void> => {
break; break;
default: default:
// Alert API user that they messed up // Alert API user that they messed up
requestEvent.respondWith(new Response(STATUS_TEXT.get(Status.NotFound), { status: Status.NotFound })); requestEvent.respondWith(stdResp.NotFound(''));
break; break;
} }
break; break;
@ -124,7 +122,7 @@ const start = async (): Promise<void> => {
break; break;
default: default:
// Alert API user that they messed up // Alert API user that they messed up
requestEvent.respondWith(new Response(STATUS_TEXT.get(Status.NotFound), { status: Status.NotFound })); requestEvent.respondWith(stdResp.NotFound(''));
break; break;
} }
break; break;
@ -154,7 +152,7 @@ const start = async (): Promise<void> => {
break; break;
default: default:
// Alert API user that they messed up // Alert API user that they messed up
requestEvent.respondWith(new Response(STATUS_TEXT.get(Status.NotFound), { status: Status.NotFound })); requestEvent.respondWith(stdResp.NotFound(''));
break; break;
} }
break; break;
@ -166,13 +164,13 @@ const start = async (): Promise<void> => {
break; break;
default: default:
// Alert API user that they messed up // Alert API user that they messed up
requestEvent.respondWith(new Response(STATUS_TEXT.get(Status.NotFound), { status: Status.NotFound })); requestEvent.respondWith(stdResp.NotFound(''));
break; break;
} }
break; break;
default: default:
// Alert API user that they messed up // Alert API user that they messed up
requestEvent.respondWith(new Response(STATUS_TEXT.get(Status.MethodNotAllowed), { status: Status.MethodNotAllowed })); requestEvent.respondWith(stdResp.MethodNotAllowed(''));
break; break;
} }
@ -192,22 +190,22 @@ const start = async (): Promise<void> => {
break; break;
default: default:
// Alert API user that they messed up // Alert API user that they messed up
requestEvent.respondWith(new Response(STATUS_TEXT.get(Status.NotFound), { status: Status.NotFound })); requestEvent.respondWith(stdResp.NotFound(''));
break; break;
} }
break; break;
default: default:
// Alert API user that they messed up // Alert API user that they messed up
requestEvent.respondWith(new Response(STATUS_TEXT.get(Status.MethodNotAllowed), { status: Status.MethodNotAllowed })); requestEvent.respondWith(stdResp.MethodNotAllowed(''));
break; break;
} }
} }
} else if (authenticated && rateLimited) { } else if (authenticated && rateLimited) {
// Alert API user that they are doing this too often // Alert API user that they are doing this too often
requestEvent.respondWith(new Response(STATUS_TEXT.get(Status.TooManyRequests), { status: Status.TooManyRequests })); requestEvent.respondWith(stdResp.TooManyRequests('Slow down, servers are expensive and this bot is free to use.'));
} else { } else {
// Alert API user that they shouldn't be doing this // Alert API user that they shouldn't be doing this
requestEvent.respondWith(new Response(STATUS_TEXT.get(Status.Forbidden), { status: Status.Forbidden })); requestEvent.respondWith(stdResp.Forbidden(''));
} }
} }
})(); })();

View File

@ -8,11 +8,9 @@ import {
nanoid, nanoid,
// Discordeno deps // Discordeno deps
sendMessage, sendMessage,
// httpd deps
Status,
STATUS_TEXT,
} from '../../../deps.ts'; } from '../../../deps.ts';
import { generateApiDeleteEmail } from '../../commandUtils.ts'; import { generateApiDeleteEmail } from '../../commandUtils.ts';
import stdResp from '../stdResponses.ts';
export const apiKeyDelete = async (requestEvent: Deno.RequestEvent, query: Map<string, string>, apiUserid: BigInt, apiUserEmail: string, apiUserDelCode: string) => { export const apiKeyDelete = async (requestEvent: Deno.RequestEvent, query: Map<string, string>, apiUserid: BigInt, apiUserEmail: string, apiUserDelCode: string) => {
if (query.has('user') && ((query.get('user') || '').length > 0) && query.has('email') && ((query.get('email') || '').length > 0)) { if (query.has('user') && ((query.get('user') || '').length > 0) && query.has('email') && ((query.get('email') || '').length > 0)) {
@ -24,7 +22,7 @@ export const apiKeyDelete = async (requestEvent: Deno.RequestEvent, query: Map<s
await dbClient.execute('DELETE FROM allowed_channels WHERE userid = ?', [apiUserid]).catch((e) => { await dbClient.execute('DELETE FROM allowed_channels WHERE userid = ?', [apiUserid]).catch((e) => {
log(LT.ERROR, `Failed to insert into database: ${JSON.stringify(e)}`); log(LT.ERROR, `Failed to insert into database: ${JSON.stringify(e)}`);
requestEvent.respondWith(new Response(`${STATUS_TEXT.get(Status.InternalServerError)}-6`, { status: Status.InternalServerError })); requestEvent.respondWith(stdResp.InternalServerError(''));
erroredOut = true; erroredOut = true;
}); });
if (erroredOut) { if (erroredOut) {
@ -33,19 +31,19 @@ export const apiKeyDelete = async (requestEvent: Deno.RequestEvent, query: Map<s
await dbClient.execute('DELETE FROM all_keys WHERE userid = ?', [apiUserid]).catch((e) => { await dbClient.execute('DELETE FROM all_keys WHERE userid = ?', [apiUserid]).catch((e) => {
log(LT.ERROR, `Failed to insert into database: ${JSON.stringify(e)}`); log(LT.ERROR, `Failed to insert into database: ${JSON.stringify(e)}`);
requestEvent.respondWith(new Response(`${STATUS_TEXT.get(Status.InternalServerError)}-7`, { status: Status.InternalServerError })); requestEvent.respondWith(stdResp.InternalServerError(''));
erroredOut = true; erroredOut = true;
}); });
if (erroredOut) { if (erroredOut) {
return; return;
} else { } else {
// Send API key as response // Send OK as response to indicate key deletion was successful
requestEvent.respondWith(new Response(STATUS_TEXT.get(Status.OK), { status: Status.OK })); requestEvent.respondWith(stdResp.OK(''));
return; return;
} }
} else { } else {
// Alert API user that they shouldn't be doing this // Alert API user that they shouldn't be doing this
requestEvent.respondWith(new Response(STATUS_TEXT.get(Status.Forbidden), { status: Status.Forbidden })); requestEvent.respondWith(stdResp.Forbidden(''));
} }
} else { } else {
// User does not have their delete code yet, so we need to generate one and email it to them // User does not have their delete code yet, so we need to generate one and email it to them
@ -56,7 +54,7 @@ export const apiKeyDelete = async (requestEvent: Deno.RequestEvent, query: Map<s
// Execute the DB modification // Execute the DB modification
await dbClient.execute('UPDATE all_keys SET deleteCode = ? WHERE userid = ?', [deleteCode, apiUserid]).catch((e) => { await dbClient.execute('UPDATE all_keys SET deleteCode = ? WHERE userid = ?', [deleteCode, apiUserid]).catch((e) => {
log(LT.ERROR, `Failed to insert into database: ${JSON.stringify(e)}`); log(LT.ERROR, `Failed to insert into database: ${JSON.stringify(e)}`);
requestEvent.respondWith(new Response(`${STATUS_TEXT.get(Status.InternalServerError)}-8`, { status: Status.InternalServerError })); requestEvent.respondWith(stdResp.InternalServerError(''));
erroredOut = true; erroredOut = true;
}); });
if (erroredOut) { if (erroredOut) {
@ -65,23 +63,23 @@ export const apiKeyDelete = async (requestEvent: Deno.RequestEvent, query: Map<s
// "Send" the email // "Send" the email
await sendMessage(config.api.email, generateApiDeleteEmail(apiUserEmail, deleteCode)).catch(() => { await sendMessage(config.api.email, generateApiDeleteEmail(apiUserEmail, deleteCode)).catch(() => {
requestEvent.respondWith(new Response('Message 30 failed to send.', { status: Status.InternalServerError })); requestEvent.respondWith(stdResp.InternalServerError('Failed to send email.'));
erroredOut = true; erroredOut = true;
}); });
if (erroredOut) { if (erroredOut) {
return; return;
} else { } else {
// Send API key as response // Send API key as response
requestEvent.respondWith(new Response(STATUS_TEXT.get(Status.FailedDependency), { status: Status.FailedDependency })); requestEvent.respondWith(stdResp.FailedDependency('Please look for an email containing a Delete Key and run this query again with said key.'));
return; return;
} }
} }
} else { } else {
// Alert API user that they shouldn't be doing this // Alert API user that they shouldn't be doing this
requestEvent.respondWith(new Response(STATUS_TEXT.get(Status.Forbidden), { status: Status.Forbidden })); requestEvent.respondWith(stdResp.Forbidden(''));
} }
} else { } else {
// Alert API user that they messed up // Alert API user that they messed up
requestEvent.respondWith(new Response(STATUS_TEXT.get(Status.BadRequest), { status: Status.BadRequest })); requestEvent.respondWith(stdResp.BadRequest(''));
} }
}; };

View File

@ -3,10 +3,8 @@ import {
// Log4Deno deps // Log4Deno deps
log, log,
LT, LT,
// httpd deps
Status,
STATUS_TEXT,
} from '../../../deps.ts'; } from '../../../deps.ts';
import stdResp from '../stdResponses.ts';
export const apiChannel = async (requestEvent: Deno.RequestEvent, query: Map<string, string>, apiUserid: BigInt) => { export const apiChannel = async (requestEvent: Deno.RequestEvent, query: Map<string, string>, apiUserid: BigInt) => {
if (query.has('user') && ((query.get('user') || '').length > 0)) { if (query.has('user') && ((query.get('user') || '').length > 0)) {
@ -17,7 +15,7 @@ export const apiChannel = async (requestEvent: Deno.RequestEvent, query: Map<str
// Get all channels userid has authorized // Get all channels userid has authorized
const dbAllowedChannelQuery = await dbClient.query('SELECT * FROM allowed_channels WHERE userid = ?', [apiUserid]).catch((e) => { const dbAllowedChannelQuery = await dbClient.query('SELECT * FROM allowed_channels WHERE userid = ?', [apiUserid]).catch((e) => {
log(LT.ERROR, `Failed to insert into database: ${JSON.stringify(e)}`); log(LT.ERROR, `Failed to insert into database: ${JSON.stringify(e)}`);
requestEvent.respondWith(new Response(`${STATUS_TEXT.get(Status.InternalServerError)}-1`, { status: Status.InternalServerError })); requestEvent.respondWith(stdResp.InternalServerError(''));
erroredOut = true; erroredOut = true;
}); });
@ -26,16 +24,16 @@ export const apiChannel = async (requestEvent: Deno.RequestEvent, query: Map<str
} else { } else {
// Customized strinification to handle BigInts correctly // Customized strinification to handle BigInts correctly
const returnChannels = JSON.stringify(dbAllowedChannelQuery, (_key, value) => (typeof value === 'bigint' ? value.toString() : value)); const returnChannels = JSON.stringify(dbAllowedChannelQuery, (_key, value) => (typeof value === 'bigint' ? value.toString() : value));
// Send API key as response // Send channel list as response
requestEvent.respondWith(new Response(returnChannels, { status: Status.OK })); requestEvent.respondWith(stdResp.OK(returnChannels));
return; return;
} }
} else { } else {
// Alert API user that they shouldn't be doing this // Alert API user that they shouldn't be doing this
requestEvent.respondWith(new Response(STATUS_TEXT.get(Status.Forbidden), { status: Status.Forbidden })); requestEvent.respondWith(stdResp.Forbidden(''));
} }
} else { } else {
// Alert API user that they messed up // Alert API user that they messed up
requestEvent.respondWith(new Response(STATUS_TEXT.get(Status.BadRequest), { status: Status.BadRequest })); requestEvent.respondWith(stdResp.BadRequest(''));
} }
}; };

View File

@ -8,11 +8,9 @@ import {
nanoid, nanoid,
// Discordeno deps // Discordeno deps
sendMessage, sendMessage,
// httpd deps
Status,
STATUS_TEXT,
} from '../../../deps.ts'; } from '../../../deps.ts';
import { generateApiKeyEmail } from '../../commandUtils.ts'; import { generateApiKeyEmail } from '../../commandUtils.ts';
import stdResp from '../stdResponses.ts';
export const apiKey = async (requestEvent: Deno.RequestEvent, query: Map<string, string>) => { export const apiKey = async (requestEvent: Deno.RequestEvent, query: Map<string, string>) => {
if ((query.has('user') && ((query.get('user') || '').length > 0)) && (query.has('email') && ((query.get('email') || '').length > 0))) { if ((query.has('user') && ((query.get('user') || '').length > 0)) && (query.has('email') && ((query.get('email') || '').length > 0))) {
@ -26,7 +24,7 @@ export const apiKey = async (requestEvent: Deno.RequestEvent, query: Map<string,
await dbClient.execute('INSERT INTO all_keys(userid,apiKey,email) values(?,?,?)', [BigInt(query.get('user') || '0'), newKey, (query.get('email') || '').toLowerCase()]).catch( await dbClient.execute('INSERT INTO all_keys(userid,apiKey,email) values(?,?,?)', [BigInt(query.get('user') || '0'), newKey, (query.get('email') || '').toLowerCase()]).catch(
(e) => { (e) => {
log(LT.ERROR, `Failed to insert into database: ${JSON.stringify(e)}`); log(LT.ERROR, `Failed to insert into database: ${JSON.stringify(e)}`);
requestEvent.respondWith(new Response(`${STATUS_TEXT.get(Status.InternalServerError)}-9`, { status: Status.InternalServerError })); requestEvent.respondWith(stdResp.InternalServerError(''));
erroredOut = true; erroredOut = true;
}, },
); );
@ -38,19 +36,19 @@ export const apiKey = async (requestEvent: Deno.RequestEvent, query: Map<string,
// "Send" the email // "Send" the email
await sendMessage(config.api.email, generateApiKeyEmail(query.get('email') || 'no email', newKey)).catch(() => { await sendMessage(config.api.email, generateApiKeyEmail(query.get('email') || 'no email', newKey)).catch(() => {
requestEvent.respondWith(new Response('Message 31 failed to send.', { status: Status.InternalServerError })); requestEvent.respondWith(stdResp.InternalServerError('Failed to send email.'));
erroredOut = true; erroredOut = true;
}); });
if (erroredOut) { if (erroredOut) {
return; return;
} else { } else {
// Send API key as response // Send basic OK to indicate key has been sent
requestEvent.respondWith(new Response(STATUS_TEXT.get(Status.OK), { status: Status.OK })); requestEvent.respondWith(stdResp.OK(''));
return; return;
} }
} else { } else {
// Alert API user that they messed up // Alert API user that they messed up
requestEvent.respondWith(new Response(STATUS_TEXT.get(Status.BadRequest), { status: Status.BadRequest })); requestEvent.respondWith(stdResp.BadRequest(''));
} }
}; };

View File

@ -6,10 +6,8 @@ import {
LT, LT,
// nanoid deps // nanoid deps
nanoid, nanoid,
// httpd deps
Status,
STATUS_TEXT,
} from '../../../deps.ts'; } from '../../../deps.ts';
import stdResp from '../stdResponses.ts';
export const apiKeyAdmin = async (requestEvent: Deno.RequestEvent, query: Map<string, string>, apiUserid: BigInt) => { export const apiKeyAdmin = async (requestEvent: Deno.RequestEvent, query: Map<string, string>, apiUserid: BigInt) => {
if ((query.has('user') && ((query.get('user') || '').length > 0)) && (query.has('a') && ((query.get('a') || '').length > 0))) { if ((query.has('user') && ((query.get('user') || '').length > 0)) && (query.has('a') && ((query.get('a') || '').length > 0))) {
@ -23,7 +21,7 @@ export const apiKeyAdmin = async (requestEvent: Deno.RequestEvent, query: Map<st
// Insert new key/user pair into the db // Insert new key/user pair into the db
await dbClient.execute('INSERT INTO all_keys(userid,apiKey) values(?,?)', [apiUserid, newKey]).catch((e) => { await dbClient.execute('INSERT INTO all_keys(userid,apiKey) values(?,?)', [apiUserid, newKey]).catch((e) => {
log(LT.ERROR, `Failed to insert into database: ${JSON.stringify(e)}`); log(LT.ERROR, `Failed to insert into database: ${JSON.stringify(e)}`);
requestEvent.respondWith(new Response(`${STATUS_TEXT.get(Status.InternalServerError)}-0`, { status: Status.InternalServerError })); requestEvent.respondWith(stdResp.InternalServerError(''));
erroredOut = true; erroredOut = true;
}); });
@ -32,15 +30,15 @@ export const apiKeyAdmin = async (requestEvent: Deno.RequestEvent, query: Map<st
return; return;
} else { } else {
// Send API key as response // Send API key as response
requestEvent.respondWith(new Response(JSON.stringify({ 'key': newKey, 'userid': query.get('user') }), { status: Status.OK })); requestEvent.respondWith(stdResp.OK(JSON.stringify({ 'key': newKey, 'userid': query.get('user') })));
return; return;
} }
} else { } else {
// Only allow the db admin to use this API // Only allow the db admin to use this API
requestEvent.respondWith(new Response(STATUS_TEXT.get(Status.Forbidden), { status: Status.Forbidden })); requestEvent.respondWith(stdResp.Forbidden(''));
} }
} else { } else {
// Alert API user that they messed up // Alert API user that they messed up
requestEvent.respondWith(new Response(STATUS_TEXT.get(Status.BadRequest), { status: Status.BadRequest })); requestEvent.respondWith(stdResp.BadRequest(''));
} }
}; };

View File

@ -6,12 +6,10 @@ import {
// Log4Deno deps // Log4Deno deps
log, log,
LT, LT,
// httpd deps
Status,
STATUS_TEXT,
} from '../../../deps.ts'; } from '../../../deps.ts';
import { QueuedRoll, RollModifiers } from '../../mod.d.ts'; import { QueuedRoll, RollModifiers } from '../../mod.d.ts';
import { queueRoll } from '../../solver/rollQueue.ts'; import { queueRoll } from '../../solver/rollQueue.ts';
import stdResp from '../stdResponses.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}>`;
@ -23,7 +21,7 @@ export const apiRoll = async (requestEvent: Deno.RequestEvent, query: Map<string
) { ) {
if (query.has('n') && query.has('m')) { if (query.has('n') && query.has('m')) {
// Alert API user that they shouldn't be doing this // Alert API user that they shouldn't be doing this
requestEvent.respondWith(new Response(STATUS_TEXT.get(Status.BadRequest), { status: Status.BadRequest })); requestEvent.respondWith(stdResp.BadRequest(''));
return; return;
} }
@ -59,7 +57,7 @@ export const apiRoll = async (requestEvent: Deno.RequestEvent, query: Map<string
if (rollCmd.length === 0) { if (rollCmd.length === 0) {
// Alert API user that they messed up // Alert API user that they messed up
requestEvent.respondWith(new Response(STATUS_TEXT.get(Status.BadRequest), { status: Status.BadRequest })); requestEvent.respondWith(stdResp.BadRequest(''));
// Always log API rolls for abuse detection // Always log API rolls for abuse detection
dbClient.execute(queries.insertRollLogCmd(1, 1), [originalCommand, 'EmptyInput', null]).catch((e) => { dbClient.execute(queries.insertRollLogCmd(1, 1), [originalCommand, 'EmptyInput', null]).catch((e) => {
@ -70,7 +68,7 @@ export const apiRoll = async (requestEvent: Deno.RequestEvent, query: Map<string
if (query.has('o') && (query.get('o')?.toLowerCase() !== 'd' && query.get('o')?.toLowerCase() !== 'a')) { if (query.has('o') && (query.get('o')?.toLowerCase() !== 'd' && query.get('o')?.toLowerCase() !== 'a')) {
// Alert API user that they messed up // Alert API user that they messed up
requestEvent.respondWith(new Response(STATUS_TEXT.get(Status.BadRequest), { status: Status.BadRequest })); requestEvent.respondWith(stdResp.BadRequest(''));
// Always log API rolls for abuse detection // Always log API rolls for abuse detection
dbClient.execute(queries.insertRollLogCmd(1, 1), [originalCommand, 'BadOrder', null]).catch((e) => { dbClient.execute(queries.insertRollLogCmd(1, 1), [originalCommand, 'BadOrder', null]).catch((e) => {
@ -109,19 +107,18 @@ export const apiRoll = async (requestEvent: Deno.RequestEvent, query: Map<string
} 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)}`);
requestEvent.respondWith(new Response(STATUS_TEXT.get(Status.InternalServerError), { status: Status.InternalServerError })); requestEvent.respondWith(stdResp.InternalServerError(''));
} }
} else { } else {
// Alert API user that they messed up // Alert API user that they messed up
requestEvent.respondWith( requestEvent.respondWith(
new Response( stdResp.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.`, `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
requestEvent.respondWith(new Response(STATUS_TEXT.get(Status.BadRequest), { status: Status.BadRequest })); requestEvent.respondWith(stdResp.BadRequest(''));
} }
}; };

View File

@ -3,10 +3,8 @@ import {
// Log4Deno deps // Log4Deno deps
log, log,
LT, LT,
// httpd deps
Status,
STATUS_TEXT,
} from '../../../deps.ts'; } from '../../../deps.ts';
import stdResp from '../stdResponses.ts';
export const apiChannelAdd = async (requestEvent: Deno.RequestEvent, query: Map<string, string>, apiUserid: BigInt) => { export const apiChannelAdd = async (requestEvent: Deno.RequestEvent, query: Map<string, string>, apiUserid: BigInt) => {
if ((query.has('user') && ((query.get('user') || '').length > 0)) && (query.has('channel') && ((query.get('channel') || '').length > 0))) { if ((query.has('user') && ((query.get('user') || '').length > 0)) && (query.has('channel') && ((query.get('channel') || '').length > 0))) {
@ -17,7 +15,7 @@ export const apiChannelAdd = async (requestEvent: Deno.RequestEvent, query: Map<
// Insert new user/channel pair into the db // Insert new user/channel pair into the db
await dbClient.execute('INSERT INTO allowed_channels(userid,channelid) values(?,?)', [apiUserid, BigInt(query.get('channel') || '0')]).catch((e) => { await dbClient.execute('INSERT INTO allowed_channels(userid,channelid) values(?,?)', [apiUserid, BigInt(query.get('channel') || '0')]).catch((e) => {
log(LT.ERROR, `Failed to insert into database: ${JSON.stringify(e)}`); log(LT.ERROR, `Failed to insert into database: ${JSON.stringify(e)}`);
requestEvent.respondWith(new Response(`${STATUS_TEXT.get(Status.InternalServerError)}-2`, { status: Status.InternalServerError })); requestEvent.respondWith(stdResp.InternalServerError(''));
erroredOut = true; erroredOut = true;
}); });
@ -25,16 +23,16 @@ export const apiChannelAdd = async (requestEvent: Deno.RequestEvent, query: Map<
if (erroredOut) { if (erroredOut) {
return; return;
} else { } else {
// Send API key as response // Send OK to indicate modification was successful
requestEvent.respondWith(new Response(STATUS_TEXT.get(Status.OK), { status: Status.OK })); requestEvent.respondWith(stdResp.OK(''));
return; return;
} }
} else { } else {
// Alert API user that they shouldn't be doing this // Alert API user that they shouldn't be doing this
requestEvent.respondWith(new Response(STATUS_TEXT.get(Status.Forbidden), { status: Status.Forbidden })); requestEvent.respondWith(stdResp.Forbidden(''));
} }
} else { } else {
// Alert API user that they messed up // Alert API user that they messed up
requestEvent.respondWith(new Response(STATUS_TEXT.get(Status.BadRequest), { status: Status.BadRequest })); requestEvent.respondWith(stdResp.BadRequest(''));
} }
}; };

View File

@ -3,10 +3,8 @@ import {
// Log4Deno deps // Log4Deno deps
log, log,
LT, LT,
// httpd deps
Status,
STATUS_TEXT,
} from '../../../deps.ts'; } from '../../../deps.ts';
import stdResp from '../stdResponses.ts';
export const apiChannelManageActive = async (requestEvent: Deno.RequestEvent, query: Map<string, string>, apiUserid: BigInt, path: string) => { export const apiChannelManageActive = async (requestEvent: Deno.RequestEvent, query: Map<string, string>, apiUserid: BigInt, path: string) => {
if ((query.has('channel') && ((query.get('channel') || '').length > 0)) && (query.has('user') && ((query.get('user') || '').length > 0))) { if ((query.has('channel') && ((query.get('channel') || '').length > 0)) && (query.has('user') && ((query.get('user') || '').length > 0))) {
@ -24,7 +22,7 @@ export const apiChannelManageActive = async (requestEvent: Deno.RequestEvent, qu
// Update the requested entry // Update the requested entry
await dbClient.execute('UPDATE allowed_channels SET active = ? WHERE userid = ? AND channelid = ?', [value, apiUserid, BigInt(query.get('channel') || '0')]).catch((e) => { await dbClient.execute('UPDATE allowed_channels SET active = ? WHERE userid = ? AND channelid = ?', [value, apiUserid, BigInt(query.get('channel') || '0')]).catch((e) => {
log(LT.ERROR, `Failed to insert into database: ${JSON.stringify(e)}`); log(LT.ERROR, `Failed to insert into database: ${JSON.stringify(e)}`);
requestEvent.respondWith(new Response(`${STATUS_TEXT.get(Status.InternalServerError)}-5`, { status: Status.InternalServerError })); requestEvent.respondWith(stdResp.InternalServerError(''));
erroredOut = true; erroredOut = true;
}); });
@ -33,15 +31,15 @@ export const apiChannelManageActive = async (requestEvent: Deno.RequestEvent, qu
return; return;
} else { } else {
// Send API key as response // Send API key as response
requestEvent.respondWith(new Response(STATUS_TEXT.get(Status.OK), { status: Status.OK })); requestEvent.respondWith(stdResp.OK(''));
return; return;
} }
} else { } else {
// Alert API user that they shouldn't be doing this // Alert API user that they shouldn't be doing this
requestEvent.respondWith(new Response(STATUS_TEXT.get(Status.Forbidden), { status: Status.Forbidden })); requestEvent.respondWith(stdResp.Forbidden(''));
} }
} else { } else {
// Alert API user that they messed up // Alert API user that they messed up
requestEvent.respondWith(new Response(STATUS_TEXT.get(Status.BadRequest), { status: Status.BadRequest })); requestEvent.respondWith(stdResp.BadRequest(''));
} }
}; };

View File

@ -4,10 +4,8 @@ import {
// Log4Deno deps // Log4Deno deps
log, log,
LT, LT,
// httpd deps
Status,
STATUS_TEXT,
} from '../../../deps.ts'; } from '../../../deps.ts';
import stdResp from '../stdResponses.ts';
export const apiChannelManageBan = async (requestEvent: Deno.RequestEvent, query: Map<string, string>, apiUserid: BigInt, path: string) => { export const apiChannelManageBan = async (requestEvent: Deno.RequestEvent, query: Map<string, string>, apiUserid: BigInt, path: string) => {
if ( if (
@ -28,7 +26,7 @@ export const apiChannelManageBan = async (requestEvent: Deno.RequestEvent, query
// Execute the DB modification // Execute the DB modification
await dbClient.execute('UPDATE allowed_channels SET banned = ? WHERE userid = ? AND channelid = ?', [value, apiUserid, BigInt(query.get('channel') || '0')]).catch((e) => { await dbClient.execute('UPDATE allowed_channels SET banned = ? WHERE userid = ? AND channelid = ?', [value, apiUserid, BigInt(query.get('channel') || '0')]).catch((e) => {
log(LT.ERROR, `Failed to insert into database: ${JSON.stringify(e)}`); log(LT.ERROR, `Failed to insert into database: ${JSON.stringify(e)}`);
requestEvent.respondWith(new Response(`${STATUS_TEXT.get(Status.InternalServerError)}-4`, { status: Status.InternalServerError })); requestEvent.respondWith(stdResp.InternalServerError(''));
erroredOut = true; erroredOut = true;
}); });
@ -36,16 +34,16 @@ export const apiChannelManageBan = async (requestEvent: Deno.RequestEvent, query
if (erroredOut) { if (erroredOut) {
return; return;
} else { } else {
// Send API key as response // Send OK to indicate modification was successful
requestEvent.respondWith(new Response(STATUS_TEXT.get(Status.OK), { status: Status.OK })); requestEvent.respondWith(stdResp.OK(''));
return; return;
} }
} else { } else {
// Alert API user that they shouldn't be doing this // Alert API user that they shouldn't be doing this
requestEvent.respondWith(new Response(STATUS_TEXT.get(Status.Forbidden), { status: Status.Forbidden })); requestEvent.respondWith(stdResp.Forbidden(''));
} }
} else { } else {
// Alert API user that they messed up // Alert API user that they messed up
requestEvent.respondWith(new Response(STATUS_TEXT.get(Status.BadRequest), { status: Status.BadRequest })); requestEvent.respondWith(stdResp.BadRequest(''));
} }
}; };

View File

@ -4,10 +4,8 @@ import {
// Log4Deno deps // Log4Deno deps
log, log,
LT, LT,
// httpd deps
Status,
STATUS_TEXT,
} from '../../../deps.ts'; } from '../../../deps.ts';
import stdResp from '../stdResponses.ts';
export const apiKeyManage = async (requestEvent: Deno.RequestEvent, query: Map<string, string>, apiUserid: BigInt, path: string) => { export const apiKeyManage = async (requestEvent: Deno.RequestEvent, query: Map<string, string>, apiUserid: BigInt, path: string) => {
if ((query.has('a') && ((query.get('a') || '').length > 0)) && (query.has('user') && ((query.get('user') || '').length > 0))) { if ((query.has('a') && ((query.get('a') || '').length > 0)) && (query.has('user') && ((query.get('user') || '').length > 0))) {
@ -32,7 +30,7 @@ export const apiKeyManage = async (requestEvent: Deno.RequestEvent, query: Map<s
// Execute the DB modification // Execute the DB modification
await dbClient.execute('UPDATE all_keys SET ?? = ? WHERE userid = ?', [key, value, apiUserid]).catch((e) => { await dbClient.execute('UPDATE all_keys SET ?? = ? WHERE userid = ?', [key, value, apiUserid]).catch((e) => {
log(LT.ERROR, `Failed to insert into database: ${JSON.stringify(e)}`); log(LT.ERROR, `Failed to insert into database: ${JSON.stringify(e)}`);
requestEvent.respondWith(new Response(`${STATUS_TEXT.get(Status.InternalServerError)}-3`, { status: Status.InternalServerError })); requestEvent.respondWith(stdResp.InternalServerError(''));
erroredOut = true; erroredOut = true;
}); });
@ -40,16 +38,16 @@ export const apiKeyManage = async (requestEvent: Deno.RequestEvent, query: Map<s
if (erroredOut) { if (erroredOut) {
return; return;
} else { } else {
// Send API key as response // Send OK as response to indicate modification was successful
requestEvent.respondWith(new Response(STATUS_TEXT.get(Status.OK), { status: Status.OK })); requestEvent.respondWith(stdResp.OK(''));
return; return;
} }
} else { } else {
// Alert API user that they shouldn't be doing this // Alert API user that they shouldn't be doing this
requestEvent.respondWith(new Response(STATUS_TEXT.get(Status.Forbidden), { status: Status.Forbidden })); requestEvent.respondWith(stdResp.Forbidden(''));
} }
} else { } else {
// Alert API user that they messed up // Alert API user that they messed up
requestEvent.respondWith(new Response(STATUS_TEXT.get(Status.BadRequest), { status: Status.BadRequest })); requestEvent.respondWith(stdResp.BadRequest(''));
} }
}; };

View File

@ -0,0 +1,19 @@
import {
// httpd deps
Status,
STATUS_TEXT,
} from '../../deps.ts';
const genericResponse = (customText: string, status: Status) => new Response(customText || STATUS_TEXT.get(status), { status: status, statusText: STATUS_TEXT.get(status) });
export default {
BadRequest: (customText: string) => genericResponse(customText, Status.BadRequest),
FailedDependency: (customText: string) => genericResponse(customText, Status.FailedDependency),
InternalServerError: (customText: string) => genericResponse(customText, Status.InternalServerError),
Forbidden: (customText: string) => genericResponse(customText, Status.Forbidden),
MethodNotAllowed: (customText: string) => genericResponse(customText, Status.MethodNotAllowed),
NotFound: (customText: string) => genericResponse(customText, Status.NotFound),
OK: (customText: string) => genericResponse(customText, Status.OK),
RequestTimeout: (customText: string) => genericResponse(customText, Status.RequestTimeout),
TooManyRequests: (customText: string) => genericResponse(customText, Status.TooManyRequests),
};

View File

@ -10,13 +10,11 @@ import {
// Discordeno deps // Discordeno deps
sendDirectMessage, sendDirectMessage,
sendMessage, sendMessage,
// httpd deps
Status,
STATUS_TEXT,
} from '../../deps.ts'; } from '../../deps.ts';
import { SolvedRoll } from '../solver/solver.d.ts'; import { SolvedRoll } from '../solver/solver.d.ts';
import { QueuedRoll, RollModifiers } from '../mod.d.ts'; import { QueuedRoll, RollModifiers } from '../mod.d.ts';
import { generateCountDetailsEmbed, generateDMFailed, generateRollEmbed, infoColor2, rollingEmbed } from '../commandUtils.ts'; import { generateCountDetailsEmbed, generateDMFailed, generateRollEmbed, infoColor2, rollingEmbed } from '../commandUtils.ts';
import stdResp from '../endpoints/stdResponses.ts';
let currentWorkers = 0; let currentWorkers = 0;
const rollQueue: Array<QueuedRoll> = []; const rollQueue: Array<QueuedRoll> = [];
@ -35,12 +33,7 @@ const handleRollWorker = async (rq: QueuedRoll) => {
rollWorker.terminate(); rollWorker.terminate();
currentWorkers--; currentWorkers--;
if (rq.apiRoll) { if (rq.apiRoll) {
rq.api.requestEvent.respondWith( rq.api.requestEvent.respondWith(stdResp.RequestTimeout('Roll took too long to process, try breaking roll down into simpler parts'));
new Response(
'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: [
@ -76,12 +69,7 @@ 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( rq.api.requestEvent.respondWith(stdResp.InternalServerError(returnmsg.errorMsg));
new Response(
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] });
} }
@ -102,12 +90,7 @@ const handleRollWorker = async (rq: QueuedRoll) => {
embeds: [pubEmbedDetails.embed], embeds: [pubEmbedDetails.embed],
}).catch(() => { }).catch(() => {
apiErroredOut = true; apiErroredOut = true;
rq.api.requestEvent.respondWith( rq.api.requestEvent.respondWith(stdResp.InternalServerError('Message failed to send - location 0.'));
new Response(
'Message failed to send - location 0.',
{ 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
@ -151,12 +134,7 @@ const handleRollWorker = async (rq: QueuedRoll) => {
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( rq.api.requestEvent.respondWith(stdResp.InternalServerError('Message failed to send - location 1.'));
new Response(
'Message failed to send - location 1.',
{ status: Status.InternalServerError, statusText: STATUS_TEXT.get(Status.InternalServerError) },
),
);
}); });
} else { } else {
n = await rq.dd.m.edit({ n = await rq.dd.m.edit({
@ -177,25 +155,22 @@ const handleRollWorker = async (rq: QueuedRoll) => {
log(LT.ERROR, `Failed to insert into DB: ${JSON.stringify(e)}`); log(LT.ERROR, `Failed to insert into DB: ${JSON.stringify(e)}`);
}); });
rq.api.requestEvent.respondWith( rq.api.requestEvent.respondWith(stdResp.OK(JSON.stringify(
new Response( rq.modifiers.count
JSON.stringify( ? {
rq.modifiers.count ? { counts: countEmbed, details: pubEmbedDetails } : { details: pubEmbedDetails }, counts: countEmbed,
), details: pubEmbedDetails,
{ status: Status.OK, statusText: STATUS_TEXT.get(Status.OK) }, }
), : {
); details: pubEmbedDetails,
},
)));
} }
} }
} 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( rq.api.requestEvent.respondWith(stdResp.InternalServerError(JSON.stringify(e)));
new Response(
JSON.stringify(e),
{ status: Status.InternalServerError, statusText: STATUS_TEXT.get(Status.InternalServerError) },
),
);
} }
} }
}); });