From 541747285f28b79e31e83d5170ec654528faca10 Mon Sep 17 00:00:00 2001 From: "Ean Milligan (Bastion)" Date: Thu, 5 May 2022 01:46:47 -0400 Subject: [PATCH] Minor adjustments report command now requires a description moved an insert query that was duplicated many times to new constant in the db.ts import improved error reporting on roll modifiers, they now are fancy EMBEDS! --- src/api.ts | 14 +++++++------- src/commands/report.ts | 18 ++++++++++++------ src/commands/roll.ts | 8 ++++---- src/commands/roll/getModifiers.ts | 22 +++++++++++++--------- src/constantCmds.ts | 18 ++++++++++++++++++ src/db.ts | 4 ++++ 6 files changed, 58 insertions(+), 26 deletions(-) diff --git a/src/api.ts b/src/api.ts index 8aece4d..f9f25e4 100644 --- a/src/api.ts +++ b/src/api.ts @@ -16,7 +16,7 @@ import { nanoid } from "../deps.ts"; -import { dbClient } from "./db.ts"; +import { dbClient, queries } from "./db.ts"; import solver from "./solver.ts"; import { LogTypes as LT } from "./utils.enums.ts"; import utils from "./utils.ts"; @@ -215,7 +215,7 @@ const start = async (): Promise => { requestEvent.respondWith(new Response(STATUS_TEXT.get(Status.BadRequest), { status: Status.BadRequest })); // Always log API rolls for abuse detection - dbClient.execute("INSERT INTO roll_log(input,result,resultid,api,error) values(?,?,?,1,1)", [originalCommand, "EmptyInput", null]).catch(e => { + dbClient.execute(queries.insertRollLogCmd(1, 1), [originalCommand, "EmptyInput", null]).catch(e => { utils.log(LT.ERROR, `Failed to insert into database: ${JSON.stringify(e)}`); }); break; @@ -226,7 +226,7 @@ const start = async (): Promise => { requestEvent.respondWith(new Response(STATUS_TEXT.get(Status.BadRequest), { status: Status.BadRequest })); // Always log API rolls for abuse detection - dbClient.execute("INSERT INTO roll_log(input,result,resultid,api,error) values(?,?,?,1,1)", [originalCommand, "BadOrder", null]).catch(e => { + dbClient.execute(queries.insertRollLogCmd(1, 1), [originalCommand, "BadOrder", null]).catch(e => { utils.log(LT.ERROR, `Failed to insert into database: ${JSON.stringify(e)}`); }); break; @@ -247,7 +247,7 @@ const start = async (): Promise => { requestEvent.respondWith(new Response(returnmsg.errorMsg, { status: Status.InternalServerError })); // Always log API rolls for abuse detection - dbClient.execute("INSERT INTO roll_log(input,result,resultid,api,error) values(?,?,?,1,1)", [originalCommand, returnmsg.errorCode, null]).catch(e => { + dbClient.execute(queries.insertRollLogCmd(1, 1), [originalCommand, returnmsg.errorCode, null]).catch(e => { utils.log(LT.ERROR, `Failed to insert into database: ${JSON.stringify(e)}`); }); break; @@ -277,7 +277,7 @@ const start = async (): Promise => { requestEvent.respondWith(new Response(STATUS_TEXT.get(Status.BadRequest), { status: Status.BadRequest })); // Always log API rolls for abuse detection - dbClient.execute("INSERT INTO roll_log(input,result,resultid,api,error) values(?,?,?,1,1)", [originalCommand, "NoGMsSent", null]).catch(e => { + dbClient.execute(queries.insertRollLogCmd(1, 1), [originalCommand, "NoGMsSent", null]).catch(e => { utils.log(LT.ERROR, `Failed to insert into database: ${JSON.stringify(e)}`); }); break; @@ -340,7 +340,7 @@ const start = async (): Promise => { }); // Always log API rolls for abuse detection - dbClient.execute("INSERT INTO roll_log(input,result,resultid,api,error) values(?,?,?,1,0)", [originalCommand, returnText, ((typeof m === "object") ? m.id : null)]).catch(e => { + dbClient.execute(queries.insertRollLogCmd(1, 0), [originalCommand, returnText, ((typeof m === "object") ? m.id : null)]).catch(e => { utils.log(LT.ERROR, `Failed to insert into database: ${JSON.stringify(e)}`); }); @@ -385,7 +385,7 @@ const start = async (): Promise => { } // If enabled, log rolls so we can verify the bots math - dbClient.execute("INSERT INTO roll_log(input,result,resultid,api,error) values(?,?,?,1,0)", [originalCommand, returnText, ((typeof m === "object") ? m.id : null)]).catch(e => { + dbClient.execute(queries.insertRollLogCmd(1, 0), [originalCommand, returnText, ((typeof m === "object") ? m.id : null)]).catch(e => { utils.log(LT.ERROR, `Failed to insert into database: ${JSON.stringify(e)}`); }); diff --git a/src/commands/report.ts b/src/commands/report.ts index 6bbf162..cbadbbe 100644 --- a/src/commands/report.ts +++ b/src/commands/report.ts @@ -11,10 +11,16 @@ export const report = (message: DiscordenoMessage, args: string[]) => { utils.log(LT.ERROR, `Failed to call stored procedure INC_CNT: ${JSON.stringify(e)}`); }); - sendMessage(config.reportChannel, generateReport(args.join(" "))).catch(e => { - utils.log(LT.ERROR, `Failed to send message: ${JSON.stringify(message)} | ${JSON.stringify(e)}`); - }); - message.send(constantCmds.report).catch(e => { - utils.log(LT.ERROR, `Failed to send message: ${JSON.stringify(message)} | ${JSON.stringify(e)}`); - }); + if (args.join(" ")) { + sendMessage(config.reportChannel, generateReport(args.join(" "))).catch(e => { + utils.log(LT.ERROR, `Failed to send message: ${JSON.stringify(message)} | ${JSON.stringify(e)}`); + }); + message.send(constantCmds.report).catch(e => { + utils.log(LT.ERROR, `Failed to send message: ${JSON.stringify(message)} | ${JSON.stringify(e)}`); + }); + } else { + message.send(constantCmds.reportFail).catch(e => { + utils.log(LT.ERROR, `Failed to send message: ${JSON.stringify(message)} | ${JSON.stringify(e)}`); + }); + } }; diff --git a/src/commands/roll.ts b/src/commands/roll.ts index 7ceb085..d876533 100644 --- a/src/commands/roll.ts +++ b/src/commands/roll.ts @@ -1,6 +1,6 @@ import config from "../../config.ts"; import { DEVMODE } from "../../flags.ts"; -import { dbClient } from "../db.ts"; +import { dbClient, queries } from "../db.ts"; import { DiscordenoMessage, sendDirectMessage } from "../../deps.ts"; import utils from "../utils.ts"; import solver from "../solver.ts"; @@ -49,7 +49,7 @@ export const roll = async (message: DiscordenoMessage, args: string[], command: if (DEVMODE && config.logRolls) { // If enabled, log rolls so we can verify the bots math - dbClient.execute("INSERT INTO roll_log(input,result,resultid,api,error) values(?,?,?,0,1)", [originalCommand, returnmsg.errorCode, m.id]).catch(e => { + dbClient.execute(queries.insertRollLogCmd(0, 1), [originalCommand, returnmsg.errorCode, m.id]).catch(e => { utils.log(LT.ERROR, `Failed to insert into DB: ${JSON.stringify(e)}`); }); } @@ -102,7 +102,7 @@ export const roll = async (message: DiscordenoMessage, args: string[], command: if (DEVMODE && config.logRolls) { // If enabled, log rolls so we can verify the bots math - dbClient.execute("INSERT INTO roll_log(input,result,resultid,api,error) values(?,?,?,0,0)", [originalCommand, returnText, m.id]).catch(e => { + dbClient.execute(queries.insertRollLogCmd(0, 0), [originalCommand, returnText, m.id]).catch(e => { utils.log(LT.ERROR, `Failed to insert into DB: ${JSON.stringify(e)}`); }); } @@ -135,7 +135,7 @@ export const roll = async (message: DiscordenoMessage, args: string[], command: if (DEVMODE && config.logRolls) { // If enabled, log rolls so we can verify the bots math - dbClient.execute("INSERT INTO roll_log(input,result,resultid,api,error) values(?,?,?,0,0)", [originalCommand, returnText, m.id]).catch(e => { + dbClient.execute(queries.insertRollLogCmd(0, 0), [originalCommand, returnText, m.id]).catch(e => { utils.log(LT.ERROR, `Failed to insert into DB: ${JSON.stringify(e)}`); }); } diff --git a/src/commands/roll/getModifiers.ts b/src/commands/roll/getModifiers.ts index 16d1f2e..29eeb9c 100644 --- a/src/commands/roll/getModifiers.ts +++ b/src/commands/roll/getModifiers.ts @@ -1,12 +1,14 @@ import config from "../../../config.ts"; import { DEVMODE } from "../../../flags.ts"; -import { dbClient } from "../../db.ts"; +import { dbClient, queries } from "../../db.ts"; import { DiscordenoMessage } from "../../../deps.ts"; +import { generateRollError } from "../../constantCmds.ts"; import utils from "../../utils.ts"; import { LogTypes as LT } from "../../utils.enums.ts"; import { RollModifiers } from "../../mod.d.ts"; export const getModifiers = (m: DiscordenoMessage, args: string[], command: string, originalCommand: string): RollModifiers => { + const errorType = "Modifiers invalid:"; const modifiers: RollModifiers = { noDetails: false, spoiler: "", @@ -58,11 +60,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("Error: Must specifiy at least one GM by mentioning them"); + m.edit(generateRollError(errorType, "Must specifiy at least one GM by @mentioning them")); if (DEVMODE && config.logRolls) { // If enabled, log rolls so we can verify the bots math - dbClient.execute("INSERT INTO roll_log(input,result,resultid,api,error) values(?,?,?,0,1)", [originalCommand, "NoGMsFound", m.id]).catch(e => { + dbClient.execute(queries.insertRollLogCmd(0, 1), [originalCommand, "NoGMsFound", m.id]).catch(e => { utils.log(LT.ERROR, `Failed to insert into DB: ${JSON.stringify(e)}`); }); } @@ -73,15 +75,17 @@ export const getModifiers = (m: DiscordenoMessage, args: string[], command: stri i--; break; case "-o": + console.log(args) args.splice(i, 1); - - if (args[i].toLowerCase()[0] !== "d" && args[i].toLowerCase()[0] !== "a") { + console.log(args) + + 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("Error: Must specifiy a or d to order the rolls ascending or descending"); + m.edit(generateRollError(errorType, "Must specifiy `a` or `d` to order the rolls ascending or descending")); if (DEVMODE && config.logRolls) { // If enabled, log rolls so we can verify the bots math - dbClient.execute("INSERT INTO roll_log(input,result,resultid,api,error) values(?,?,?,0,1)", [originalCommand, "NoOrderFound", m.id]).catch(e => { + dbClient.execute(queries.insertRollLogCmd(0, 1), [originalCommand, "NoOrderFound", m.id]).catch(e => { utils.log(LT.ERROR, `Failed to insert into DB: ${JSON.stringify(e)}`); }); } @@ -100,11 +104,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("Error: Cannot maximise and nominise the roll at the same time"); + m.edit(generateRollError(errorType, "Cannot maximise and nominise the roll at the same time")); if (DEVMODE && config.logRolls) { // If enabled, log rolls so we can verify the bots math - dbClient.execute("INSERT INTO roll_log(input,result,resultid,api,error) values(?,?,?,0,1)", [originalCommand, "MaxAndNominal", m.id]).catch(e => { + dbClient.execute(queries.insertRollLogCmd(0, 1), [originalCommand, "MaxAndNominal", m.id]).catch(e => { utils.log(LT.ERROR, `Failed to insert into DB: ${JSON.stringify(e)}`); }); } diff --git a/src/constantCmds.ts b/src/constantCmds.ts index 93de93e..50e3b38 100644 --- a/src/constantCmds.ts +++ b/src/constantCmds.ts @@ -167,6 +167,14 @@ export const constantCmds = { }] }] }, + reportFail: { + embeds: [{ + fields: [{ + name: "Please provide a short description of what failed", + value: "Providing a short description helps my developer quickly diagnose what went wrong." + }] + }] + }, rip: { embeds: [{ fields: [{ @@ -426,3 +434,13 @@ export const generateApiDeleteEmail = (email: string, deleteCode: string) => ({ ] }] }); + +export const generateRollError = (errorType: string, errorMsg: string) => ({ + embeds: [{ + title: "Roll command encountered the following error:", + fields: [{ + name: errorType, + value: `${errorMsg}\n\nPlease try again. If the error is repeated, please report the issue using the \`[[report\` command.` + }] + }] +}); diff --git a/src/db.ts b/src/db.ts index cf7b7ce..c77d32a 100644 --- a/src/db.ts +++ b/src/db.ts @@ -9,3 +9,7 @@ export const dbClient = await new Client().connect({ username: config.db.username, password: config.db.password }); + +export const queries = { + insertRollLogCmd: (api: number, error: number) => `INSERT INTO roll_log(input,result,resultid,api,error) values(?,?,?,${api},${error})` +};