Cleaned up API responses to make them more standard and easier to customize
This commit is contained in:
parent
0cabfe0c99
commit
d45cc89eec
22
src/api.ts
22
src/api.ts
|
@ -9,12 +9,10 @@ import {
|
|||
// Log4Deno deps
|
||||
log,
|
||||
LT,
|
||||
// httpd deps
|
||||
Status,
|
||||
STATUS_TEXT,
|
||||
} from '../deps.ts';
|
||||
import { dbClient } from './db.ts';
|
||||
import endpoints from './endpoints/_index.ts';
|
||||
import stdResp from './endpoints/stdResponses.ts';
|
||||
|
||||
// start() returns nothing
|
||||
// start initializes and runs the entire API for the bot
|
||||
|
@ -112,7 +110,7 @@ const start = async (): Promise<void> => {
|
|||
break;
|
||||
default:
|
||||
// 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;
|
||||
|
@ -124,7 +122,7 @@ const start = async (): Promise<void> => {
|
|||
break;
|
||||
default:
|
||||
// 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;
|
||||
|
@ -154,7 +152,7 @@ const start = async (): Promise<void> => {
|
|||
break;
|
||||
default:
|
||||
// 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;
|
||||
|
@ -166,13 +164,13 @@ const start = async (): Promise<void> => {
|
|||
break;
|
||||
default:
|
||||
// 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;
|
||||
default:
|
||||
// Alert API user that they messed up
|
||||
requestEvent.respondWith(new Response(STATUS_TEXT.get(Status.MethodNotAllowed), { status: Status.MethodNotAllowed }));
|
||||
requestEvent.respondWith(stdResp.MethodNotAllowed(''));
|
||||
break;
|
||||
}
|
||||
|
||||
|
@ -192,22 +190,22 @@ const start = async (): Promise<void> => {
|
|||
break;
|
||||
default:
|
||||
// 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;
|
||||
default:
|
||||
// Alert API user that they messed up
|
||||
requestEvent.respondWith(new Response(STATUS_TEXT.get(Status.MethodNotAllowed), { status: Status.MethodNotAllowed }));
|
||||
requestEvent.respondWith(stdResp.MethodNotAllowed(''));
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else if (authenticated && rateLimited) {
|
||||
// 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 {
|
||||
// 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(''));
|
||||
}
|
||||
}
|
||||
})();
|
||||
|
|
|
@ -8,11 +8,9 @@ import {
|
|||
nanoid,
|
||||
// Discordeno deps
|
||||
sendMessage,
|
||||
// httpd deps
|
||||
Status,
|
||||
STATUS_TEXT,
|
||||
} from '../../../deps.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) => {
|
||||
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) => {
|
||||
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;
|
||||
});
|
||||
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) => {
|
||||
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;
|
||||
});
|
||||
if (erroredOut) {
|
||||
return;
|
||||
} else {
|
||||
// Send API key as response
|
||||
requestEvent.respondWith(new Response(STATUS_TEXT.get(Status.OK), { status: Status.OK }));
|
||||
// Send OK as response to indicate key deletion was successful
|
||||
requestEvent.respondWith(stdResp.OK(''));
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
// 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 {
|
||||
// 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
|
||||
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)}`);
|
||||
requestEvent.respondWith(new Response(`${STATUS_TEXT.get(Status.InternalServerError)}-8`, { status: Status.InternalServerError }));
|
||||
requestEvent.respondWith(stdResp.InternalServerError(''));
|
||||
erroredOut = true;
|
||||
});
|
||||
if (erroredOut) {
|
||||
|
@ -65,23 +63,23 @@ export const apiKeyDelete = async (requestEvent: Deno.RequestEvent, query: Map<s
|
|||
|
||||
// "Send" the email
|
||||
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;
|
||||
});
|
||||
if (erroredOut) {
|
||||
return;
|
||||
} else {
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// 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 {
|
||||
// Alert API user that they messed up
|
||||
requestEvent.respondWith(new Response(STATUS_TEXT.get(Status.BadRequest), { status: Status.BadRequest }));
|
||||
requestEvent.respondWith(stdResp.BadRequest(''));
|
||||
}
|
||||
};
|
||||
|
|
|
@ -3,10 +3,8 @@ import {
|
|||
// Log4Deno deps
|
||||
log,
|
||||
LT,
|
||||
// httpd deps
|
||||
Status,
|
||||
STATUS_TEXT,
|
||||
} from '../../../deps.ts';
|
||||
import stdResp from '../stdResponses.ts';
|
||||
|
||||
export const apiChannel = async (requestEvent: Deno.RequestEvent, query: Map<string, string>, apiUserid: BigInt) => {
|
||||
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
|
||||
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)}`);
|
||||
requestEvent.respondWith(new Response(`${STATUS_TEXT.get(Status.InternalServerError)}-1`, { status: Status.InternalServerError }));
|
||||
requestEvent.respondWith(stdResp.InternalServerError(''));
|
||||
erroredOut = true;
|
||||
});
|
||||
|
||||
|
@ -26,16 +24,16 @@ export const apiChannel = async (requestEvent: Deno.RequestEvent, query: Map<str
|
|||
} else {
|
||||
// Customized strinification to handle BigInts correctly
|
||||
const returnChannels = JSON.stringify(dbAllowedChannelQuery, (_key, value) => (typeof value === 'bigint' ? value.toString() : value));
|
||||
// Send API key as response
|
||||
requestEvent.respondWith(new Response(returnChannels, { status: Status.OK }));
|
||||
// Send channel list as response
|
||||
requestEvent.respondWith(stdResp.OK(returnChannels));
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
// 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 {
|
||||
// Alert API user that they messed up
|
||||
requestEvent.respondWith(new Response(STATUS_TEXT.get(Status.BadRequest), { status: Status.BadRequest }));
|
||||
requestEvent.respondWith(stdResp.BadRequest(''));
|
||||
}
|
||||
};
|
||||
|
|
|
@ -8,11 +8,9 @@ import {
|
|||
nanoid,
|
||||
// Discordeno deps
|
||||
sendMessage,
|
||||
// httpd deps
|
||||
Status,
|
||||
STATUS_TEXT,
|
||||
} from '../../../deps.ts';
|
||||
import { generateApiKeyEmail } from '../../commandUtils.ts';
|
||||
import stdResp from '../stdResponses.ts';
|
||||
|
||||
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))) {
|
||||
|
@ -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(
|
||||
(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;
|
||||
},
|
||||
);
|
||||
|
@ -38,19 +36,19 @@ export const apiKey = async (requestEvent: Deno.RequestEvent, query: Map<string,
|
|||
|
||||
// "Send" the email
|
||||
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;
|
||||
});
|
||||
|
||||
if (erroredOut) {
|
||||
return;
|
||||
} else {
|
||||
// Send API key as response
|
||||
requestEvent.respondWith(new Response(STATUS_TEXT.get(Status.OK), { status: Status.OK }));
|
||||
// Send basic OK to indicate key has been sent
|
||||
requestEvent.respondWith(stdResp.OK(''));
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
// Alert API user that they messed up
|
||||
requestEvent.respondWith(new Response(STATUS_TEXT.get(Status.BadRequest), { status: Status.BadRequest }));
|
||||
requestEvent.respondWith(stdResp.BadRequest(''));
|
||||
}
|
||||
};
|
||||
|
|
|
@ -6,10 +6,8 @@ import {
|
|||
LT,
|
||||
// nanoid deps
|
||||
nanoid,
|
||||
// httpd deps
|
||||
Status,
|
||||
STATUS_TEXT,
|
||||
} from '../../../deps.ts';
|
||||
import stdResp from '../stdResponses.ts';
|
||||
|
||||
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))) {
|
||||
|
@ -23,7 +21,7 @@ export const apiKeyAdmin = async (requestEvent: Deno.RequestEvent, query: Map<st
|
|||
// Insert new key/user pair into the db
|
||||
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)}`);
|
||||
requestEvent.respondWith(new Response(`${STATUS_TEXT.get(Status.InternalServerError)}-0`, { status: Status.InternalServerError }));
|
||||
requestEvent.respondWith(stdResp.InternalServerError(''));
|
||||
erroredOut = true;
|
||||
});
|
||||
|
||||
|
@ -32,15 +30,15 @@ export const apiKeyAdmin = async (requestEvent: Deno.RequestEvent, query: Map<st
|
|||
return;
|
||||
} else {
|
||||
// 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;
|
||||
}
|
||||
} else {
|
||||
// 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 {
|
||||
// Alert API user that they messed up
|
||||
requestEvent.respondWith(new Response(STATUS_TEXT.get(Status.BadRequest), { status: Status.BadRequest }));
|
||||
requestEvent.respondWith(stdResp.BadRequest(''));
|
||||
}
|
||||
};
|
||||
|
|
|
@ -6,12 +6,10 @@ import {
|
|||
// Log4Deno deps
|
||||
log,
|
||||
LT,
|
||||
// httpd deps
|
||||
Status,
|
||||
STATUS_TEXT,
|
||||
} from '../../../deps.ts';
|
||||
import { QueuedRoll, RollModifiers } from '../../mod.d.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}>`;
|
||||
|
||||
|
@ -23,7 +21,7 @@ export const apiRoll = async (requestEvent: Deno.RequestEvent, query: Map<string
|
|||
) {
|
||||
if (query.has('n') && query.has('m')) {
|
||||
// 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;
|
||||
}
|
||||
|
||||
|
@ -59,7 +57,7 @@ export const apiRoll = async (requestEvent: Deno.RequestEvent, query: Map<string
|
|||
|
||||
if (rollCmd.length === 0) {
|
||||
// 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
|
||||
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')) {
|
||||
// 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
|
||||
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) {
|
||||
// Handle any errors we missed
|
||||
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 {
|
||||
// Alert API user that they messed up
|
||||
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.`,
|
||||
{ status: Status.Forbidden, statusText: STATUS_TEXT.get(Status.Forbidden) },
|
||||
),
|
||||
);
|
||||
}
|
||||
} else {
|
||||
// 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(''));
|
||||
}
|
||||
};
|
||||
|
|
|
@ -3,10 +3,8 @@ import {
|
|||
// Log4Deno deps
|
||||
log,
|
||||
LT,
|
||||
// httpd deps
|
||||
Status,
|
||||
STATUS_TEXT,
|
||||
} from '../../../deps.ts';
|
||||
import stdResp from '../stdResponses.ts';
|
||||
|
||||
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))) {
|
||||
|
@ -17,7 +15,7 @@ export const apiChannelAdd = async (requestEvent: Deno.RequestEvent, query: Map<
|
|||
// 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) => {
|
||||
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;
|
||||
});
|
||||
|
||||
|
@ -25,16 +23,16 @@ export const apiChannelAdd = async (requestEvent: Deno.RequestEvent, query: Map<
|
|||
if (erroredOut) {
|
||||
return;
|
||||
} else {
|
||||
// Send API key as response
|
||||
requestEvent.respondWith(new Response(STATUS_TEXT.get(Status.OK), { status: Status.OK }));
|
||||
// Send OK to indicate modification was successful
|
||||
requestEvent.respondWith(stdResp.OK(''));
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
// 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 {
|
||||
// Alert API user that they messed up
|
||||
requestEvent.respondWith(new Response(STATUS_TEXT.get(Status.BadRequest), { status: Status.BadRequest }));
|
||||
requestEvent.respondWith(stdResp.BadRequest(''));
|
||||
}
|
||||
};
|
||||
|
|
|
@ -3,10 +3,8 @@ import {
|
|||
// Log4Deno deps
|
||||
log,
|
||||
LT,
|
||||
// httpd deps
|
||||
Status,
|
||||
STATUS_TEXT,
|
||||
} from '../../../deps.ts';
|
||||
import stdResp from '../stdResponses.ts';
|
||||
|
||||
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))) {
|
||||
|
@ -24,7 +22,7 @@ export const apiChannelManageActive = async (requestEvent: Deno.RequestEvent, qu
|
|||
// 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) => {
|
||||
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;
|
||||
});
|
||||
|
||||
|
@ -33,15 +31,15 @@ export const apiChannelManageActive = async (requestEvent: Deno.RequestEvent, qu
|
|||
return;
|
||||
} else {
|
||||
// Send API key as response
|
||||
requestEvent.respondWith(new Response(STATUS_TEXT.get(Status.OK), { status: Status.OK }));
|
||||
requestEvent.respondWith(stdResp.OK(''));
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
// 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 {
|
||||
// Alert API user that they messed up
|
||||
requestEvent.respondWith(new Response(STATUS_TEXT.get(Status.BadRequest), { status: Status.BadRequest }));
|
||||
requestEvent.respondWith(stdResp.BadRequest(''));
|
||||
}
|
||||
};
|
||||
|
|
|
@ -4,10 +4,8 @@ import {
|
|||
// Log4Deno deps
|
||||
log,
|
||||
LT,
|
||||
// httpd deps
|
||||
Status,
|
||||
STATUS_TEXT,
|
||||
} from '../../../deps.ts';
|
||||
import stdResp from '../stdResponses.ts';
|
||||
|
||||
export const apiChannelManageBan = async (requestEvent: Deno.RequestEvent, query: Map<string, string>, apiUserid: BigInt, path: string) => {
|
||||
if (
|
||||
|
@ -28,7 +26,7 @@ export const apiChannelManageBan = async (requestEvent: Deno.RequestEvent, query
|
|||
// 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) => {
|
||||
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;
|
||||
});
|
||||
|
||||
|
@ -36,16 +34,16 @@ export const apiChannelManageBan = async (requestEvent: Deno.RequestEvent, query
|
|||
if (erroredOut) {
|
||||
return;
|
||||
} else {
|
||||
// Send API key as response
|
||||
requestEvent.respondWith(new Response(STATUS_TEXT.get(Status.OK), { status: Status.OK }));
|
||||
// Send OK to indicate modification was successful
|
||||
requestEvent.respondWith(stdResp.OK(''));
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
// 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 {
|
||||
// Alert API user that they messed up
|
||||
requestEvent.respondWith(new Response(STATUS_TEXT.get(Status.BadRequest), { status: Status.BadRequest }));
|
||||
requestEvent.respondWith(stdResp.BadRequest(''));
|
||||
}
|
||||
};
|
||||
|
|
|
@ -4,10 +4,8 @@ import {
|
|||
// Log4Deno deps
|
||||
log,
|
||||
LT,
|
||||
// httpd deps
|
||||
Status,
|
||||
STATUS_TEXT,
|
||||
} from '../../../deps.ts';
|
||||
import stdResp from '../stdResponses.ts';
|
||||
|
||||
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))) {
|
||||
|
@ -32,7 +30,7 @@ export const apiKeyManage = async (requestEvent: Deno.RequestEvent, query: Map<s
|
|||
// Execute the DB modification
|
||||
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)}`);
|
||||
requestEvent.respondWith(new Response(`${STATUS_TEXT.get(Status.InternalServerError)}-3`, { status: Status.InternalServerError }));
|
||||
requestEvent.respondWith(stdResp.InternalServerError(''));
|
||||
erroredOut = true;
|
||||
});
|
||||
|
||||
|
@ -40,16 +38,16 @@ export const apiKeyManage = async (requestEvent: Deno.RequestEvent, query: Map<s
|
|||
if (erroredOut) {
|
||||
return;
|
||||
} else {
|
||||
// Send API key as response
|
||||
requestEvent.respondWith(new Response(STATUS_TEXT.get(Status.OK), { status: Status.OK }));
|
||||
// Send OK as response to indicate modification was successful
|
||||
requestEvent.respondWith(stdResp.OK(''));
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
// 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 {
|
||||
// Alert API user that they messed up
|
||||
requestEvent.respondWith(new Response(STATUS_TEXT.get(Status.BadRequest), { status: Status.BadRequest }));
|
||||
requestEvent.respondWith(stdResp.BadRequest(''));
|
||||
}
|
||||
};
|
||||
|
|
|
@ -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),
|
||||
};
|
|
@ -10,13 +10,11 @@ import {
|
|||
// Discordeno deps
|
||||
sendDirectMessage,
|
||||
sendMessage,
|
||||
// httpd deps
|
||||
Status,
|
||||
STATUS_TEXT,
|
||||
} from '../../deps.ts';
|
||||
import { SolvedRoll } from '../solver/solver.d.ts';
|
||||
import { QueuedRoll, RollModifiers } from '../mod.d.ts';
|
||||
import { generateCountDetailsEmbed, generateDMFailed, generateRollEmbed, infoColor2, rollingEmbed } from '../commandUtils.ts';
|
||||
import stdResp from '../endpoints/stdResponses.ts';
|
||||
|
||||
let currentWorkers = 0;
|
||||
const rollQueue: Array<QueuedRoll> = [];
|
||||
|
@ -35,12 +33,7 @@ const handleRollWorker = async (rq: QueuedRoll) => {
|
|||
rollWorker.terminate();
|
||||
currentWorkers--;
|
||||
if (rq.apiRoll) {
|
||||
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) },
|
||||
),
|
||||
);
|
||||
rq.api.requestEvent.respondWith(stdResp.RequestTimeout('Roll took too long to process, try breaking roll down into simpler parts'));
|
||||
} else {
|
||||
rq.dd.m.edit({
|
||||
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 (returnmsg.error) {
|
||||
if (rq.apiRoll) {
|
||||
rq.api.requestEvent.respondWith(
|
||||
new Response(
|
||||
returnmsg.errorMsg,
|
||||
{ status: Status.InternalServerError, statusText: STATUS_TEXT.get(Status.InternalServerError) },
|
||||
),
|
||||
);
|
||||
rq.api.requestEvent.respondWith(stdResp.InternalServerError(returnmsg.errorMsg));
|
||||
} else {
|
||||
rq.dd.m.edit({ embeds: [pubEmbedDetails.embed] });
|
||||
}
|
||||
|
@ -102,12 +90,7 @@ const handleRollWorker = async (rq: QueuedRoll) => {
|
|||
embeds: [pubEmbedDetails.embed],
|
||||
}).catch(() => {
|
||||
apiErroredOut = true;
|
||||
rq.api.requestEvent.respondWith(
|
||||
new Response(
|
||||
'Message failed to send - location 0.',
|
||||
{ status: Status.InternalServerError, statusText: STATUS_TEXT.get(Status.InternalServerError) },
|
||||
),
|
||||
);
|
||||
rq.api.requestEvent.respondWith(stdResp.InternalServerError('Message failed to send - location 0.'));
|
||||
});
|
||||
} else {
|
||||
// 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],
|
||||
}).catch(() => {
|
||||
apiErroredOut = true;
|
||||
rq.api.requestEvent.respondWith(
|
||||
new Response(
|
||||
'Message failed to send - location 1.',
|
||||
{ status: Status.InternalServerError, statusText: STATUS_TEXT.get(Status.InternalServerError) },
|
||||
),
|
||||
);
|
||||
rq.api.requestEvent.respondWith(stdResp.InternalServerError('Message failed to send - location 1.'));
|
||||
});
|
||||
} else {
|
||||
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)}`);
|
||||
});
|
||||
|
||||
rq.api.requestEvent.respondWith(
|
||||
new Response(
|
||||
JSON.stringify(
|
||||
rq.modifiers.count ? { counts: countEmbed, details: pubEmbedDetails } : { details: pubEmbedDetails },
|
||||
),
|
||||
{ status: Status.OK, statusText: STATUS_TEXT.get(Status.OK) },
|
||||
),
|
||||
);
|
||||
rq.api.requestEvent.respondWith(stdResp.OK(JSON.stringify(
|
||||
rq.modifiers.count
|
||||
? {
|
||||
counts: countEmbed,
|
||||
details: pubEmbedDetails,
|
||||
}
|
||||
: {
|
||||
details: pubEmbedDetails,
|
||||
},
|
||||
)));
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
log(LT.ERROR, `Unddandled Error: ${JSON.stringify(e)}`);
|
||||
if (rq.apiRoll && !apiErroredOut) {
|
||||
rq.api.requestEvent.respondWith(
|
||||
new Response(
|
||||
JSON.stringify(e),
|
||||
{ status: Status.InternalServerError, statusText: STATUS_TEXT.get(Status.InternalServerError) },
|
||||
),
|
||||
);
|
||||
rq.api.requestEvent.respondWith(stdResp.InternalServerError(JSON.stringify(e)));
|
||||
}
|
||||
}
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue