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!
This commit is contained in:
Ean Milligan (Bastion) 2022-05-05 01:46:47 -04:00
parent ce77893d37
commit 541747285f
6 changed files with 58 additions and 26 deletions

View File

@ -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<void> => {
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<void> => {
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<void> => {
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<void> => {
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<void> => {
});
// 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<void> => {
}
// 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)}`);
});

View File

@ -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)}`);
});
}
};

View File

@ -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)}`);
});
}

View File

@ -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);
console.log(args)
if (args[i].toLowerCase()[0] !== "d" && args[i].toLowerCase()[0] !== "a") {
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)}`);
});
}

View File

@ -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.`
}]
}]
});

View File

@ -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})`
};