diff --git a/mod.ts b/mod.ts index 025babe..d37b75d 100644 --- a/mod.ts +++ b/mod.ts @@ -97,9 +97,7 @@ startBot({ guildDelete: (guild: DiscordenoGuild) => { log(LT.LOG, `Handling leaving guild ${JSON.stringify(guild)}`); sendMessage(config.logChannel, `I have been removed from: ${guild.name} (id: ${guild.id}).`).catch((e: Error) => utils.commonLoggers.messageSendError('mod.ts:99', 'Leave Guild', e)); - dbClient.execute('DELETE FROM allowed_guilds WHERE guildid = ? AND banned = 0', [guild.id]).catch((e) => { - log(LT.ERROR, `Failed to DELETE guild from DB: ${JSON.stringify(e)}`); - }); + dbClient.execute('DELETE FROM allowed_guilds WHERE guildid = ? AND banned = 0', [guild.id]).catch((e) => utils.commonLoggers.dbError('mod.ts:100', 'delete from', e)); }, debug: DEVMODE ? (dmsg) => log(LT.LOG, `Debug Message | ${JSON.stringify(dmsg)}`) : undefined, messageCreate: (message: DiscordenoMessage) => { diff --git a/src/commands/apiCmd.ts b/src/commands/apiCmd.ts index f5d7fba..bfb97de 100644 --- a/src/commands/apiCmd.ts +++ b/src/commands/apiCmd.ts @@ -13,9 +13,7 @@ import utils from '../utils.ts'; export const api = async (message: DiscordenoMessage, args: string[]) => { // Light telemetry to see how many times a command is being run - dbClient.execute(`CALL INC_CNT("api");`).catch((e) => { - log(LT.ERROR, `Failed to call stored procedure INC_CNT: ${JSON.stringify(e)}`); - }); + dbClient.execute(`CALL INC_CNT("api");`).catch((e) => utils.commonLoggers.dbError('apiCmd.ts:16', 'call sproc INC_CNT on', e)); // Local apiArg in lowercase const apiArg = (args[0] || 'help').toLowerCase(); diff --git a/src/commands/apiCmd/allowBlock.ts b/src/commands/apiCmd/allowBlock.ts index ee9cd1c..0b22b0a 100644 --- a/src/commands/apiCmd/allowBlock.ts +++ b/src/commands/apiCmd/allowBlock.ts @@ -12,7 +12,7 @@ import utils from '../../utils.ts'; export const allowBlock = async (message: DiscordenoMessage, apiArg: string) => { let errorOutInitial = false; const guildQuery = await dbClient.query(`SELECT guildid, channelid FROM allowed_guilds WHERE guildid = ? AND channelid = ?`, [message.guildId, message.channelId]).catch((e0) => { - log(LT.ERROR, `Failed to query DB: ${JSON.stringify(e0)}`); + utils.commonLoggers.dbError('allowBlock.ts:15', 'query', e0) message.send(generateApiFailed(apiArg)).catch((e: Error) => utils.commonLoggers.messageSendError('allowBlock.ts:16', message, e)); errorOutInitial = true; }); @@ -23,7 +23,7 @@ export const allowBlock = async (message: DiscordenoMessage, apiArg: string) => // Since guild is not in our DB, add it in await dbClient.execute(`INSERT INTO allowed_guilds(guildid,channelid,active) values(?,?,?)`, [message.guildId, message.channelId, (apiArg === 'allow' || apiArg === 'enable') ? 1 : 0]).catch( (e0) => { - log(LT.ERROR, `Failed to insert into DB: ${JSON.stringify(e0)}`); + utils.commonLoggers.dbError('allowBlock:26', 'insert into', e0) message.send(generateApiFailed(apiArg)).catch((e: Error) => utils.commonLoggers.messageSendError('allowBlock.ts:27', message, e)); errorOut = true; }, @@ -32,7 +32,7 @@ export const allowBlock = async (message: DiscordenoMessage, apiArg: string) => // Since guild is in our DB, update it await dbClient.execute(`UPDATE allowed_guilds SET active = ? WHERE guildid = ? AND channelid = ?`, [(apiArg === 'allow' || apiArg === 'enable') ? 1 : 0, message.guildId, message.channelId]).catch( (e0) => { - log(LT.ERROR, `Failed to update DB: ${JSON.stringify(e0)}`); + utils.commonLoggers.dbError('allowBlock.ts:35', 'update', e0) message.send(generateApiFailed(apiArg)).catch((e: Error) => utils.commonLoggers.messageSendError('allowBlock.ts:36', message, e)); errorOut = true; }, diff --git a/src/commands/apiCmd/deleteGuild.ts b/src/commands/apiCmd/deleteGuild.ts index 904df44..96d6e74 100644 --- a/src/commands/apiCmd/deleteGuild.ts +++ b/src/commands/apiCmd/deleteGuild.ts @@ -12,7 +12,7 @@ import utils from '../../utils.ts'; export const deleteGuild = async (message: DiscordenoMessage) => { let errorOut = false; await dbClient.execute(`DELETE FROM allowed_guilds WHERE guildid = ? AND channelid = ?`, [message.guildId, message.channelId]).catch((e0) => { - log(LT.ERROR, `Failed to query DB: ${JSON.stringify(e0)}`); + utils.commonLoggers.dbError('deleteGuild.ts:15', 'query', e0) message.send({ embeds: [{ color: failColor, diff --git a/src/commands/apiCmd/showHideWarn.ts b/src/commands/apiCmd/showHideWarn.ts index b0085e6..c3f7827 100644 --- a/src/commands/apiCmd/showHideWarn.ts +++ b/src/commands/apiCmd/showHideWarn.ts @@ -12,7 +12,7 @@ import utils from '../../utils.ts'; export const showHideWarn = async (message: DiscordenoMessage, apiArg: string) => { let errorOutInitial = false; const guildQuery = await dbClient.query(`SELECT guildid, channelid FROM allowed_guilds WHERE guildid = ? AND channelid = ?`, [message.guildId, message.channelId]).catch((e0) => { - log(LT.ERROR, `Failed to query DB: ${JSON.stringify(e0)}`); + utils.commonLoggers.dbError('showHideWarn.ts:15', 'query', e0); message.send(generateApiFailed(`${apiArg} on`)).catch((e: Error) => utils.commonLoggers.messageSendError('showHideWarn.ts:16', message, e)); errorOutInitial = true; }); @@ -22,14 +22,14 @@ export const showHideWarn = async (message: DiscordenoMessage, apiArg: string) = if (guildQuery.length === 0) { // Since guild is not in our DB, add it in await dbClient.execute(`INSERT INTO allowed_guilds(guildid,channelid,hidewarn) values(?,?,?)`, [message.guildId, message.channelId, (apiArg === 'hide-warn') ? 1 : 0]).catch((e0) => { - log(LT.ERROR, `Failed to insert into DB: ${JSON.stringify(e0)}`); + utils.commonLoggers.dbError('showHideWarn.ts:25', 'insert inot', e0); message.send(generateApiFailed(`${apiArg} on`)).catch((e: Error) => utils.commonLoggers.messageSendError('showHideWarn.ts:26', message, e)); errorOut = true; }); } else { // Since guild is in our DB, update it await dbClient.execute(`UPDATE allowed_guilds SET hidewarn = ? WHERE guildid = ? AND channelid = ?`, [(apiArg === 'hide-warn') ? 1 : 0, message.guildId, message.channelId]).catch((e0) => { - log(LT.ERROR, `Failed to update DB: ${JSON.stringify(e0)}`); + utils.commonLoggers.dbError('showHideWarn.ts:32', 'update', e0); message.send(generateApiFailed(`${apiArg} on`)).catch((e: Error) => utils.commonLoggers.messageSendError('showHideWarn.ts:33', message, e)); errorOut = true; }); diff --git a/src/commands/apiCmd/status.ts b/src/commands/apiCmd/status.ts index 2ed3856..a3f4031 100644 --- a/src/commands/apiCmd/status.ts +++ b/src/commands/apiCmd/status.ts @@ -13,7 +13,7 @@ export const status = async (message: DiscordenoMessage) => { // Get status of guild from the db let errorOut = false; const guildQuery = await dbClient.query(`SELECT active, banned FROM allowed_guilds WHERE guildid = ? AND channelid = ?`, [message.guildId, message.channelId]).catch((e0) => { - log(LT.ERROR, `Failed to query DB: ${JSON.stringify(e0)}`); + utils.commonLoggers.dbError('status.ts:16', 'query', e0); message.send({ embeds: [{ color: failColor, diff --git a/src/commands/audit.ts b/src/commands/audit.ts index 637d636..ed3127b 100644 --- a/src/commands/audit.ts +++ b/src/commands/audit.ts @@ -13,9 +13,7 @@ import utils from '../utils.ts'; export const audit = async (message: DiscordenoMessage, args: string[]) => { // Light telemetry to see how many times a command is being run - dbClient.execute(`CALL INC_CNT("audit");`).catch((e) => { - log(LT.ERROR, `Failed to call stored procedure INC_CNT: ${JSON.stringify(e)}`); - }); + dbClient.execute(`CALL INC_CNT("audit");`).catch((e) => utils.commonLoggers.dbError('audit.ts:16', 'call sproc INC_CNT on', e)); // Local apiArg in lowercase const auditArg = (args[0] || 'help').toLowerCase(); diff --git a/src/commands/auditCmd/auditDB.ts b/src/commands/auditCmd/auditDB.ts index f082869..2dbb0b3 100644 --- a/src/commands/auditCmd/auditDB.ts +++ b/src/commands/auditCmd/auditDB.ts @@ -16,9 +16,7 @@ export const auditDB = async (message: DiscordenoMessage) => { const m = await message.send(compilingStats); // Get DB statistics - const auditQuery = await dbClient.query(`SELECT * FROM db_size;`).catch((e) => { - log(LT.ERROR, `Failed to query DB: ${JSON.stringify(e)}`); - }); + const auditQuery = await dbClient.query(`SELECT * FROM db_size;`).catch((e) => utils.commonLoggers.dbError('auditDB.ts:19', 'query', e)); // Turn all tables into embed fields, currently only properly will handle 25 tables, but we'll fix that when artificer gets 26 tables const embedFields: Array = []; diff --git a/src/commands/emoji.ts b/src/commands/emoji.ts index 0cf6ebc..9f5b7bc 100644 --- a/src/commands/emoji.ts +++ b/src/commands/emoji.ts @@ -25,9 +25,7 @@ export const emoji = (message: DiscordenoMessage, command: string) => { // If a match gets found if (emji.aliases.indexOf(command || '') > -1) { // Light telemetry to see how many times a command is being run - dbClient.execute(`CALL INC_CNT("emojis");`).catch((e) => { - log(LT.ERROR, `Failed to call stored procedure INC_CNT: ${JSON.stringify(e)}`); - }); + dbClient.execute(`CALL INC_CNT("emojis");`).catch((e) => utils.commonLoggers.dbError('emojis.ts:28', 'call sproc INC_CNT on', e)); // Send the needed emoji message.send(`<${emji.animated ? 'a' : ''}:${emji.name}:${emji.id}>`).catch((e: Error) => utils.commonLoggers.messageSendError('emoji.ts:33', message, e)); diff --git a/src/commands/handleMentions.ts b/src/commands/handleMentions.ts index 43f13b7..68a1b93 100644 --- a/src/commands/handleMentions.ts +++ b/src/commands/handleMentions.ts @@ -14,9 +14,7 @@ export const handleMentions = (message: DiscordenoMessage) => { log(LT.LOG, `Handling @mention message: ${JSON.stringify(message)}`); // Light telemetry to see how many times a command is being run - dbClient.execute(`CALL INC_CNT("mention");`).catch((e) => { - log(LT.ERROR, `Failed to call stored procedure INC_CNT: ${JSON.stringify(e)}`); - }); + dbClient.execute(`CALL INC_CNT("mention");`).catch((e) => utils.commonLoggers.dbError('handleMentions.ts:17', 'call sproc INC_CNT on', e)); message.send({ embeds: [{ diff --git a/src/commands/help.ts b/src/commands/help.ts index e44d5a6..6c04e7b 100644 --- a/src/commands/help.ts +++ b/src/commands/help.ts @@ -12,9 +12,7 @@ import utils from '../utils.ts'; export const help = (message: DiscordenoMessage) => { // Light telemetry to see how many times a command is being run - dbClient.execute(`CALL INC_CNT("help");`).catch((e) => { - log(LT.ERROR, `Failed to call stored procedure INC_CNT: ${JSON.stringify(e)}`); - }); + dbClient.execute(`CALL INC_CNT("help");`).catch((e) => utils.commonLoggers.dbError('htlp.ts:15', 'call sproc INC_CNT on', e)); message.send({ embeds: [{ diff --git a/src/commands/info.ts b/src/commands/info.ts index 8bf6017..449d188 100644 --- a/src/commands/info.ts +++ b/src/commands/info.ts @@ -11,9 +11,7 @@ import utils from '../utils.ts'; export const info = (message: DiscordenoMessage) => { // Light telemetry to see how many times a command is being run - dbClient.execute(`CALL INC_CNT("info");`).catch((e) => { - log(LT.ERROR, `Failed to call stored procedure INC_CNT: ${JSON.stringify(e)}`); - }); + dbClient.execute(`CALL INC_CNT("info");`).catch((e) => utils.commonLoggers.dbError('info.ts:14', 'call sproc INC_CNT on', e)); message.send({ embeds: [{ diff --git a/src/commands/ping.ts b/src/commands/ping.ts index 4fdb70b..32f3aa1 100644 --- a/src/commands/ping.ts +++ b/src/commands/ping.ts @@ -11,9 +11,7 @@ import utils from '../utils.ts'; export const ping = async (message: DiscordenoMessage) => { // Light telemetry to see how many times a command is being run - dbClient.execute(`CALL INC_CNT("ping");`).catch((e) => { - log(LT.ERROR, `Failed to call stored procedure INC_CNT: ${JSON.stringify(e)}`); - }); + dbClient.execute(`CALL INC_CNT("ping");`).catch((e) => utils.commonLoggers.dbError('ping.ts:14', 'call sproc INC_CNT on', e)); // Calculates ping between sending a message and editing it, giving a nice round-trip latency. try { diff --git a/src/commands/privacy.ts b/src/commands/privacy.ts index aad3ca9..cf7577e 100644 --- a/src/commands/privacy.ts +++ b/src/commands/privacy.ts @@ -12,9 +12,7 @@ import utils from '../utils.ts'; export const privacy = (message: DiscordenoMessage) => { // Light telemetry to see how many times a command is being run - dbClient.execute(`CALL INC_CNT("privacy");`).catch((e) => { - log(LT.ERROR, `Failed to call stored procedure INC_CNT: ${JSON.stringify(e)}`); - }); + dbClient.execute(`CALL INC_CNT("privacy");`).catch((e) => utils.commonLoggers.dbError('privacy.ts:15', 'call sproc INC_CNT on', e)); message.send({ embeds: [{ diff --git a/src/commands/report.ts b/src/commands/report.ts index ee891d4..e984748 100644 --- a/src/commands/report.ts +++ b/src/commands/report.ts @@ -14,9 +14,7 @@ import utils from '../utils.ts'; export const report = (message: DiscordenoMessage, args: string[]) => { // Light telemetry to see how many times a command is being run - dbClient.execute(`CALL INC_CNT("report");`).catch((e) => { - log(LT.ERROR, `Failed to call stored procedure INC_CNT: ${JSON.stringify(e)}`); - }); + dbClient.execute(`CALL INC_CNT("report");`).catch((e) => utils.commonLoggers.dbError('report.ts:17', 'call sproc INC_CNT on', e)); if (args.join(' ')) { sendMessage(config.reportChannel, generateReport(args.join(' '))).catch((e: Error) => utils.commonLoggers.messageSendError('report.ts:22', message, e)); diff --git a/src/commands/rip.ts b/src/commands/rip.ts index 1bc41c7..1f9da7d 100644 --- a/src/commands/rip.ts +++ b/src/commands/rip.ts @@ -11,9 +11,7 @@ import utils from '../utils.ts'; export const rip = (message: DiscordenoMessage) => { // Light telemetry to see how many times a command is being run - dbClient.execute(`CALL INC_CNT("rip");`).catch((e) => { - log(LT.ERROR, `Failed to call stored procedure INC_CNT: ${JSON.stringify(e)}`); - }); + dbClient.execute(`CALL INC_CNT("rip");`).catch((e) => utils.commonLoggers.dbError('rip.ts:14', 'call sproc INC_CNT on', e)); message.send({ embeds: [{ diff --git a/src/commands/roll.ts b/src/commands/roll.ts index 239c951..c8e9b90 100644 --- a/src/commands/roll.ts +++ b/src/commands/roll.ts @@ -16,9 +16,7 @@ import utils from '../utils.ts'; export const roll = async (message: DiscordenoMessage, args: string[], command: string) => { // Light telemetry to see how many times a command is being run - dbClient.execute(`CALL INC_CNT("roll");`).catch((e) => { - log(LT.ERROR, `Failed to call stored procedure INC_CNT: ${JSON.stringify(e)}`); - }); + dbClient.execute(`CALL INC_CNT("roll");`).catch((e) => utils.commonLoggers.dbError('roll.ts:19', 'call sproc INC_CNT on', e)); // If DEVMODE is on, only allow this command to be used in the devServer if (DEVMODE && message.guildId !== config.devServer) { diff --git a/src/commands/roll/getModifiers.ts b/src/commands/roll/getModifiers.ts index 2be1016..f4204ec 100644 --- a/src/commands/roll/getModifiers.ts +++ b/src/commands/roll/getModifiers.ts @@ -10,6 +10,7 @@ import { } from '../../../deps.ts'; import { generateRollError } from '../../commandUtils.ts'; import { RollModifiers } from '../../mod.d.ts'; +import utils from '../../utils.ts'; export const getModifiers = (m: DiscordenoMessage, args: string[], command: string, originalCommand: string): RollModifiers => { const errorType = 'Modifiers invalid:'; @@ -62,15 +63,11 @@ export const getModifiers = (m: DiscordenoMessage, args: string[], command: stri } if (modifiers.gms.length < 1) { // If -gm is on and none were found, throw an error - m.edit(generateRollError(errorType, 'Must specifiy at least one GM by @mentioning them')).catch((e) => { - log(LT.ERROR, `Failed to edit message: ${JSON.stringify(m)} | ${JSON.stringify(e)}`); - }); + m.edit(generateRollError(errorType, 'Must specifiy at least one GM by @mentioning them')).catch((e) => utils.commonLoggers.messageEditError('getModifiers.ts:66', m, e)); if (DEVMODE && config.logRolls) { // If enabled, log rolls so we can verify the bots math - dbClient.execute(queries.insertRollLogCmd(0, 1), [originalCommand, 'NoGMsFound', m.id]).catch((e) => { - log(LT.ERROR, `Failed to insert into DB: ${JSON.stringify(e)}`); - }); + dbClient.execute(queries.insertRollLogCmd(0, 1), [originalCommand, 'NoGMsFound', m.id]).catch((e) => utils.commonLoggers.dbError('getModifiers.ts:72', 'insert into', e)); } return modifiers; } @@ -81,15 +78,11 @@ export const getModifiers = (m: DiscordenoMessage, args: string[], command: stri if (!args[i] || args[i].toLowerCase()[0] !== 'd' && args[i].toLowerCase()[0] !== 'a') { // If -o is on and asc or desc was not specified, error out - m.edit(generateRollError(errorType, 'Must specifiy `a` or `d` to order the rolls ascending or descending')).catch((e) => { - log(LT.ERROR, `Failed to edit message: ${JSON.stringify(m)} | ${JSON.stringify(e)}`); - }); + m.edit(generateRollError(errorType, 'Must specifiy `a` or `d` to order the rolls ascending or descending')).catch((e) => utils.commonLoggers.messageEditError('getModifiers.ts:81', m, e)); if (DEVMODE && config.logRolls) { // If enabled, log rolls so we can verify the bots math - dbClient.execute(queries.insertRollLogCmd(0, 1), [originalCommand, 'NoOrderFound', m.id]).catch((e) => { - log(LT.ERROR, `Failed to insert into DB: ${JSON.stringify(e)}`); - }); + dbClient.execute(queries.insertRollLogCmd(0, 1), [originalCommand, 'NoOrderFound', m.id]).catch((e) => utils.commonLoggers.dbError('getModifiers.ts:89', 'insert into', e)); } return modifiers; } @@ -110,15 +103,11 @@ export const getModifiers = (m: DiscordenoMessage, args: string[], command: stri // maxRoll and nominalRoll cannot both be on, throw an error if (modifiers.maxRoll && modifiers.nominalRoll) { - m.edit(generateRollError(errorType, 'Cannot maximise and nominise the roll at the same time')).catch((e) => { - log(LT.ERROR, `Failed to edit message: ${JSON.stringify(m)} | ${JSON.stringify(e)}`); - }); + m.edit(generateRollError(errorType, 'Cannot maximise and nominise the roll at the same time')).catch((e) => utils.commonLoggers.messageEditError('getModifiers.ts:106', m, e)); if (DEVMODE && config.logRolls) { // If enabled, log rolls so we can verify the bots math - dbClient.execute(queries.insertRollLogCmd(0, 1), [originalCommand, 'MaxAndNominal', m.id]).catch((e) => { - log(LT.ERROR, `Failed to insert into DB: ${JSON.stringify(e)}`); - }); + dbClient.execute(queries.insertRollLogCmd(0, 1), [originalCommand, 'MaxAndNominal', m.id]).catch((e) => utils.commonLoggers.dbError('getModifiers.ts:120', 'insert into', e)); } return modifiers; } diff --git a/src/commands/rollHelp.ts b/src/commands/rollHelp.ts index 6dc406b..8547486 100644 --- a/src/commands/rollHelp.ts +++ b/src/commands/rollHelp.ts @@ -12,9 +12,7 @@ import utils from '../utils.ts'; export const rollHelp = (message: DiscordenoMessage) => { // Light telemetry to see how many times a command is being run - dbClient.execute(`CALL INC_CNT("rollhelp");`).catch((e) => { - log(LT.ERROR, `Failed to call stored procedure INC_CNT: ${JSON.stringify(e)}`); - }); + dbClient.execute(`CALL INC_CNT("rollhelp");`).catch((e) => utils.commonLoggers.dbError('rollHelp.ts:15', 'call sproc INC_CNT on', e)); message.send({ embeds: [ diff --git a/src/commands/stats.ts b/src/commands/stats.ts index 54f0dc5..bc2613f 100644 --- a/src/commands/stats.ts +++ b/src/commands/stats.ts @@ -14,20 +14,14 @@ import utils from '../utils.ts'; export const stats = async (message: DiscordenoMessage) => { // Light telemetry to see how many times a command is being run - dbClient.execute(`CALL INC_CNT("stats");`).catch((e) => { - log(LT.ERROR, `Failed to call stored procedure INC_CNT: ${JSON.stringify(e)}`); - }); + dbClient.execute(`CALL INC_CNT("stats");`).catch((e) => utils.commonLoggers.dbError('stats.ts', 'call sproc INC_CNT on', e)); try { const m = await message.send(compilingStats); // Calculate how many times commands have been run - const rollQuery = await dbClient.query(`SELECT count FROM command_cnt WHERE command = "roll";`).catch((e) => { - log(LT.ERROR, `Failed to query DB: ${JSON.stringify(e)}`); - }); - const totalQuery = await dbClient.query(`SELECT SUM(count) as count FROM command_cnt;`).catch((e) => { - log(LT.ERROR, `Failed to query DB: ${JSON.stringify(e)}`); - }); + const rollQuery = await dbClient.query(`SELECT count FROM command_cnt WHERE command = "roll";`).catch((e) => utils.commonLoggers.dbError('stats.ts:23', 'query', e)); + const totalQuery = await dbClient.query(`SELECT SUM(count) as count FROM command_cnt;`).catch((e) => utils.commonLoggers.dbError('stats.ts:24', 'query', e)); const rolls = BigInt(rollQuery[0].count); const total = BigInt(totalQuery[0].count); diff --git a/src/commands/version.ts b/src/commands/version.ts index 08ae361..0ab3385 100644 --- a/src/commands/version.ts +++ b/src/commands/version.ts @@ -12,9 +12,7 @@ import utils from '../utils.ts'; export const version = (message: DiscordenoMessage) => { // Light telemetry to see how many times a command is being run - dbClient.execute(`CALL INC_CNT("version");`).catch((e) => { - log(LT.ERROR, `Failed to call stored procedure INC_CNT: ${JSON.stringify(e)}`); - }); + dbClient.execute(`CALL INC_CNT("version");`).catch((e) => utils.commonLoggers.dbError('version.ts:15', 'call sproc INC_CNT on', e)); message.send({ embeds: [{ diff --git a/src/endpoints/deletes/apiKeyDelete.ts b/src/endpoints/deletes/apiKeyDelete.ts index b14e811..236a4f1 100644 --- a/src/endpoints/deletes/apiKeyDelete.ts +++ b/src/endpoints/deletes/apiKeyDelete.ts @@ -10,6 +10,7 @@ import { sendMessage, } from '../../../deps.ts'; import { generateApiDeleteEmail } from '../../commandUtils.ts'; +import utils from '../../utils.ts'; import stdResp from '../stdResponses.ts'; export const apiKeyDelete = async (requestEvent: Deno.RequestEvent, query: Map, apiUserid: BigInt, apiUserEmail: string, apiUserDelCode: string) => { @@ -21,7 +22,7 @@ export const apiKeyDelete = async (requestEvent: Deno.RequestEvent, query: Map { - log(LT.ERROR, `Failed to insert into database: ${JSON.stringify(e)}`); + utils.commonLoggers.dbError('apiKeyDelete.ts:25', 'insert into', e); requestEvent.respondWith(stdResp.InternalServerError('Channel Clean Failed.')); erroredOut = true; }); @@ -30,7 +31,7 @@ export const apiKeyDelete = async (requestEvent: Deno.RequestEvent, query: Map { - log(LT.ERROR, `Failed to insert into database: ${JSON.stringify(e)}`); + utils.commonLoggers.dbError('apiKeyDelete.ts:34', 'delete from', e); requestEvent.respondWith(stdResp.InternalServerError('Delete Key Failed.')); erroredOut = true; }); @@ -53,7 +54,7 @@ export const apiKeyDelete = async (requestEvent: Deno.RequestEvent, query: Map { - log(LT.ERROR, `Failed to insert into database: ${JSON.stringify(e)}`); + utils.commonLoggers.dbError('apiKeyDelete.ts:57', 'update', e); requestEvent.respondWith(stdResp.InternalServerError('Delete Code Failed')); erroredOut = true; }); diff --git a/src/endpoints/gets/apiChannel.ts b/src/endpoints/gets/apiChannel.ts index e3c6b6c..13fe4b8 100644 --- a/src/endpoints/gets/apiChannel.ts +++ b/src/endpoints/gets/apiChannel.ts @@ -5,6 +5,7 @@ import { LT, } from '../../../deps.ts'; import stdResp from '../stdResponses.ts'; +import utils from '../../utils.ts'; export const apiChannel = async (requestEvent: Deno.RequestEvent, query: Map, apiUserid: BigInt) => { if (query.has('user') && ((query.get('user') || '').length > 0)) { @@ -14,7 +15,7 @@ export const apiChannel = async (requestEvent: Deno.RequestEvent, query: Map { - log(LT.ERROR, `Failed to insert into database: ${JSON.stringify(e)}`); + utils.commonLoggers.dbError('apiChannel.ts', 'query', e); requestEvent.respondWith(stdResp.InternalServerError('Failed to get channels.')); erroredOut = true; }); diff --git a/src/endpoints/gets/apiKey.ts b/src/endpoints/gets/apiKey.ts index 549b832..3bc8578 100644 --- a/src/endpoints/gets/apiKey.ts +++ b/src/endpoints/gets/apiKey.ts @@ -10,6 +10,7 @@ import { sendMessage, } from '../../../deps.ts'; import { generateApiKeyEmail } from '../../commandUtils.ts'; +import utils from '../../utils.ts'; import stdResp from '../stdResponses.ts'; export const apiKey = async (requestEvent: Deno.RequestEvent, query: Map) => { @@ -23,7 +24,7 @@ export const apiKey = async (requestEvent: Deno.RequestEvent, query: Map { - log(LT.ERROR, `Failed to insert into database: ${JSON.stringify(e)}`); + utils.commonLoggers.dbError('apiKey.ts:27', 'insert into', e); requestEvent.respondWith(stdResp.InternalServerError('Failed to store key.')); erroredOut = true; }, diff --git a/src/endpoints/gets/apiKeyAdmin.ts b/src/endpoints/gets/apiKeyAdmin.ts index f77f86f..bda7cdb 100644 --- a/src/endpoints/gets/apiKeyAdmin.ts +++ b/src/endpoints/gets/apiKeyAdmin.ts @@ -8,6 +8,7 @@ import { nanoid, } from '../../../deps.ts'; import stdResp from '../stdResponses.ts'; +import utils from '../../utils.ts'; export const apiKeyAdmin = async (requestEvent: Deno.RequestEvent, query: Map, apiUserid: BigInt) => { if ((query.has('user') && ((query.get('user') || '').length > 0)) && (query.has('a') && ((query.get('a') || '').length > 0))) { @@ -20,7 +21,7 @@ export const apiKeyAdmin = async (requestEvent: Deno.RequestEvent, query: Map { - log(LT.ERROR, `Failed to insert into database: ${JSON.stringify(e)}`); + utils.commonLoggers.dbError('apiKeyAdmin.ts:24', 'insert into', e); requestEvent.respondWith(stdResp.InternalServerError('Failed to store key.')); erroredOut = true; }); diff --git a/src/endpoints/gets/apiRoll.ts b/src/endpoints/gets/apiRoll.ts index 10c54a8..5b0e298 100644 --- a/src/endpoints/gets/apiRoll.ts +++ b/src/endpoints/gets/apiRoll.ts @@ -8,6 +8,7 @@ import { LT, } from '../../../deps.ts'; import { QueuedRoll, RollModifiers } from '../../mod.d.ts'; +import utils from '../../utils.ts'; import { queueRoll } from '../../solver/rollQueue.ts'; import stdResp from '../stdResponses.ts'; @@ -60,9 +61,7 @@ export const apiRoll = async (requestEvent: Deno.RequestEvent, query: Map { - log(LT.ERROR, `Failed to insert into database: ${JSON.stringify(e)}`); - }); + dbClient.execute(queries.insertRollLogCmd(1, 1), [originalCommand, 'EmptyInput', null]).catch((e) => utils.commonLoggers.dbError('apiRoll.ts:65', 'insert', e)); return; } @@ -71,9 +70,7 @@ export const apiRoll = async (requestEvent: Deno.RequestEvent, query: Map { - log(LT.ERROR, `Failed to insert into database: ${JSON.stringify(e)}`); - }); + dbClient.execute(queries.insertRollLogCmd(1, 1), [originalCommand, 'BadOrder', null]).catch((e) => utils.commonLoggers.dbError('apiRoll.ts:66', 'insert', e)); return; } diff --git a/src/endpoints/posts/apiChannelAdd.ts b/src/endpoints/posts/apiChannelAdd.ts index 60ae2be..bc3fa9b 100644 --- a/src/endpoints/posts/apiChannelAdd.ts +++ b/src/endpoints/posts/apiChannelAdd.ts @@ -5,6 +5,7 @@ import { LT, } from '../../../deps.ts'; import stdResp from '../stdResponses.ts'; +import utils from '../../utils.ts'; export const apiChannelAdd = async (requestEvent: Deno.RequestEvent, query: Map, apiUserid: BigInt) => { if ((query.has('user') && ((query.get('user') || '').length > 0)) && (query.has('channel') && ((query.get('channel') || '').length > 0))) { @@ -14,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)}`); + utils.commonLoggers.dbError('apiChannelAdd.ts:17', 'insert into', e); requestEvent.respondWith(stdResp.InternalServerError('Failed to store channel.')); erroredOut = true; }); diff --git a/src/endpoints/puts/apiChannelManageActive.ts b/src/endpoints/puts/apiChannelManageActive.ts index 31a0c2a..d3b0993 100644 --- a/src/endpoints/puts/apiChannelManageActive.ts +++ b/src/endpoints/puts/apiChannelManageActive.ts @@ -5,6 +5,7 @@ import { LT, } from '../../../deps.ts'; import stdResp from '../stdResponses.ts'; +import utils from '../../utils.ts'; export const apiChannelManageActive = async (requestEvent: Deno.RequestEvent, query: Map, apiUserid: BigInt, path: string) => { if ((query.has('channel') && ((query.get('channel') || '').length > 0)) && (query.has('user') && ((query.get('user') || '').length > 0))) { @@ -21,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)}`); + utils.commonLoggers.dbError('apiChannelManageActive.ts:25', 'update', e); requestEvent.respondWith(stdResp.InternalServerError('Failed to update channel.')); erroredOut = true; }); diff --git a/src/endpoints/puts/apiChannelManageBan.ts b/src/endpoints/puts/apiChannelManageBan.ts index 066f6a8..e8375c8 100644 --- a/src/endpoints/puts/apiChannelManageBan.ts +++ b/src/endpoints/puts/apiChannelManageBan.ts @@ -6,6 +6,7 @@ import { LT, } from '../../../deps.ts'; import stdResp from '../stdResponses.ts'; +import utils from '../../utils.ts'; export const apiChannelManageBan = async (requestEvent: Deno.RequestEvent, query: Map, apiUserid: BigInt, path: string) => { if ( @@ -25,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)}`); + utils.commonLoggers.dbError('apiChannelManageBan.ts:28', 'update', e); requestEvent.respondWith(stdResp.InternalServerError('Failed to update channel.')); erroredOut = true; }); diff --git a/src/endpoints/puts/apiKeyManage.ts b/src/endpoints/puts/apiKeyManage.ts index f66930b..0897b12 100644 --- a/src/endpoints/puts/apiKeyManage.ts +++ b/src/endpoints/puts/apiKeyManage.ts @@ -6,6 +6,7 @@ import { LT, } from '../../../deps.ts'; import stdResp from '../stdResponses.ts'; +import utils from '../../utils.ts'; export const apiKeyManage = async (requestEvent: Deno.RequestEvent, query: Map, apiUserid: BigInt, path: string) => { if ((query.has('a') && ((query.get('a') || '').length > 0)) && (query.has('user') && ((query.get('user') || '').length > 0))) { @@ -31,7 +32,7 @@ export const apiKeyManage = async (requestEvent: Deno.RequestEvent, query: Map { - log(LT.ERROR, `Failed to insert into database: ${JSON.stringify(e)}`); + utils.commonLoggers.dbError('apiKeyManage.ts', 'update', e); requestEvent.respondWith(stdResp.InternalServerError(`Failed to ${key} to ${value}.`)); erroredOut = true; }); diff --git a/src/solver/rollQueue.ts b/src/solver/rollQueue.ts index 32887bf..78cc4fe 100644 --- a/src/solver/rollQueue.ts +++ b/src/solver/rollQueue.ts @@ -48,9 +48,7 @@ const handleRollWorker = async (rq: QueuedRoll) => { {}, )).embed, ], - }).catch((e) => { - log(LT.ERROR, `Failed to edit message: ${JSON.stringify(rq.dd.m)} | ${JSON.stringify(e)}`); - }); + }).catch((e) => utils.commonLoggers.messageEditError('rollQueue.ts:51', rq.dd.m, e)); } }, config.limits.workerTimeout); @@ -79,9 +77,7 @@ const handleRollWorker = async (rq: QueuedRoll) => { if (rq.apiRoll || DEVMODE && config.logRolls) { // If enabled, log rolls so we can see what went wrong - dbClient.execute(queries.insertRollLogCmd(rq.apiRoll ? 1 : 0, 1), [rq.originalCommand, returnmsg.errorCode, rq.apiRoll ? null : rq.dd.m.id]).catch((e) => { - log(LT.ERROR, `Failed to insert into DB: ${JSON.stringify(e)}`); - }); + dbClient.execute(queries.insertRollLogCmd(rq.apiRoll ? 1 : 0, 1), [rq.originalCommand, returnmsg.errorCode, rq.apiRoll ? null : rq.dd.m.id]).catch((e) => utils.commonLoggers.dbError('rollQueue.ts:82', 'insert into', e)); } } else { let n: DiscordenoMessage | void; @@ -154,9 +150,7 @@ const handleRollWorker = async (rq: QueuedRoll) => { } if (rq.apiRoll && !apiErroredOut) { - dbClient.execute(queries.insertRollLogCmd(1, 0), [rq.originalCommand, returnmsg.errorCode, n ? n.id : null]).catch((e) => { - log(LT.ERROR, `Failed to insert into DB: ${JSON.stringify(e)}`); - }); + dbClient.execute(queries.insertRollLogCmd(1, 0), [rq.originalCommand, returnmsg.errorCode, n ? n.id : null]).catch((e) => utils.commonLoggers.dbError('rollQueue.ts:155', 'insert into', e)); rq.api.requestEvent.respondWith(stdResp.OK(JSON.stringify( rq.modifiers.count diff --git a/src/utils.ts b/src/utils.ts index 7106ee6..dd68720 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -95,13 +95,18 @@ Available Commands: }; const genericLogger = (level: LT, message: string) => log(level, message); +const messageEditError = (location: string, message: DiscordenoMessage | string, err: Error) => + genericLogger(LT.ERROR, `${location} | Failed to edit message: ${JSON.stringify(message)} | Error: ${err.name} - ${err.message}`); const messageSendError = (location: string, message: DiscordenoMessage | string, err: Error) => - genericLogger(LT.ERROR, `${location} | Failed to send message: ${JSON.stringify(message)} | Error: ${err.name} - ${err.message} `); + genericLogger(LT.ERROR, `${location} | Failed to send message: ${JSON.stringify(message)} | Error: ${err.name} - ${err.message}`); const messageDeleteError = (location: string, message: DiscordenoMessage | string, err: Error) => - genericLogger(LT.ERROR, `${location} | Failed to delete message: ${JSON.stringify(message)} | Error: ${err.name} - ${err.message} `); + genericLogger(LT.ERROR, `${location} | Failed to delete message: ${JSON.stringify(message)} | Error: ${err.name} - ${err.message}`); +const dbError = (location: string, type: string, err: Error) => genericLogger(LT.ERROR, `${location} | Failed to ${type} database | Error: ${err.name} - ${err.message}`); export default { commonLoggers: { + dbError, + messageEditError, messageSendError, messageDeleteError, },