From 829ec0eceaf1d2aac91ef2d3d05c53745ac113ba Mon Sep 17 00:00:00 2001 From: Ean Milligan Date: Sat, 3 May 2025 09:54:25 -0400 Subject: [PATCH] move getModifiers into artigen, simplify its error handling a lot --- src/artigen/dice/dice.d.ts | 3 +- .../roll => artigen/dice}/getModifiers.ts | 50 ++++--------------- src/commandUtils.ts | 5 +- src/commands/roll.ts | 16 ++++-- src/commands/roll/_index.ts | 5 -- 5 files changed, 28 insertions(+), 51 deletions(-) rename src/{commands/roll => artigen/dice}/getModifiers.ts (55%) delete mode 100644 src/commands/roll/_index.ts diff --git a/src/artigen/dice/dice.d.ts b/src/artigen/dice/dice.d.ts index 75a1485..78b1af3 100644 --- a/src/artigen/dice/dice.d.ts +++ b/src/artigen/dice/dice.d.ts @@ -44,8 +44,9 @@ export interface RollModifiers { order: string; count: boolean; commaTotals: boolean; - valid: boolean; apiWarn: string; + valid: boolean; + error: Error; } // Basic conf interfaces diff --git a/src/commands/roll/getModifiers.ts b/src/artigen/dice/getModifiers.ts similarity index 55% rename from src/commands/roll/getModifiers.ts rename to src/artigen/dice/getModifiers.ts index 8f735e6..f6a1424 100644 --- a/src/commands/roll/getModifiers.ts +++ b/src/artigen/dice/getModifiers.ts @@ -1,19 +1,8 @@ -import { DiscordenoMessage } from '@discordeno'; import { log, LogTypes as LT } from '@Log4Deno'; -import config from '~config'; -import { DEVMODE } from '~flags'; - import { RollModifiers } from 'artigen/dice/dice.d.ts'; -import dbClient from 'db/client.ts'; -import { queries } from 'db/common.ts'; - -import { generateRollError } from 'src/commandUtils.ts'; -import utils from 'src/utils.ts'; - -export const getModifiers = (m: DiscordenoMessage, args: string[], command: string, originalCommand: string): RollModifiers => { - const errorType = 'Modifiers invalid:'; +export const getModifiers = (args: string[]): RollModifiers => { const modifiers: RollModifiers = { noDetails: false, superNoDetails: false, @@ -26,13 +15,14 @@ export const getModifiers = (m: DiscordenoMessage, args: string[], command: stri order: '', count: false, commaTotals: false, - valid: false, apiWarn: '', + valid: false, + error: new Error(), }; // Check if any of the args are command flags and pull those out into the modifiers object for (let i = 0; i < args.length; i++) { - log(LT.LOG, `Checking ${command}${args.join(' ')} for command modifiers ${i}`); + log(LT.LOG, `Checking ${args.join(' ')} for command modifiers ${i}`); let defaultCase = false; switch (args[i].toLowerCase()) { case '-c': @@ -69,14 +59,8 @@ 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 specify 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) => utils.commonLoggers.dbError('getModifiers.ts:72', 'insert into', e)); - } + modifiers.error.name = 'NoGMsFound'; + modifiers.error.message = 'Must specify at least one GM by @mentioning them'; return modifiers; } break; @@ -86,14 +70,8 @@ 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 specify `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) => utils.commonLoggers.dbError('getModifiers.ts:89', 'insert into', e)); - } + modifiers.error.name = 'NoOrderFound'; + modifiers.error.message = 'Must specify `a` or `d` to order the rolls ascending or descending'; return modifiers; } @@ -116,16 +94,8 @@ export const getModifiers = (m: DiscordenoMessage, args: string[], command: stri // maxRoll, minRoll, and nominalRoll cannot be on at same time, throw an error if ([modifiers.maxRoll, modifiers.minRoll, modifiers.nominalRoll].filter((b) => b).length > 1) { - m.edit(generateRollError(errorType, 'Can only use one of the following at a time:\n`maximize`, `minimize`, `nominal`')).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) => utils.commonLoggers.dbError('getModifiers.ts:120', 'insert into', e)); - } + modifiers.error.name = 'MaxAndNominal'; + modifiers.error.message = 'Can only use one of the following at a time:\n`maximize`, `minimize`, `nominal`'; return modifiers; } diff --git a/src/commandUtils.ts b/src/commandUtils.ts index d92ffb7..77681c5 100644 --- a/src/commandUtils.ts +++ b/src/commandUtils.ts @@ -195,7 +195,7 @@ ${config.name} Developer - Ean Milligan`, ], }); -export const generateRollError = (errorType: string, errorMsg: string) => ({ +export const generateRollError = (errorType: string, errorName: string, errorMsg: string) => ({ embeds: [ { color: failColor, @@ -206,6 +206,9 @@ export const generateRollError = (errorType: string, errorMsg: string) => ({ value: `${errorMsg}\n\nPlease try again. If the error is repeated, please report the issue using the \`${config.prefix}report\` command.`, }, ], + footer: { + text: errorName, + }, }, ], }); diff --git a/src/commands/roll.ts b/src/commands/roll.ts index b1f2158..d5cf2a2 100644 --- a/src/commands/roll.ts +++ b/src/commands/roll.ts @@ -4,14 +4,14 @@ import { log, LogTypes as LT } from '@Log4Deno'; import config from '~config'; import { DEVMODE } from '~flags'; +import { getModifiers } from 'artigen/dice/getModifiers.ts'; + import { sendRollRequest } from 'artigen/managers/queueManager.ts'; import dbClient from 'db/client.ts'; import { queries } from 'db/common.ts'; -import rollFuncs from 'commands/roll/_index.ts'; - -import { rollingEmbed, warnColor } from 'src/commandUtils.ts'; +import { generateRollError, rollingEmbed, warnColor } from 'src/commandUtils.ts'; import utils from 'src/utils.ts'; export const roll = async (message: DiscordenoMessage, args: string[], command: string) => { @@ -42,10 +42,18 @@ export const roll = async (message: DiscordenoMessage, args: string[], command: const m = await message.reply(rollingEmbed); // Get modifiers from command - const modifiers = rollFuncs.getModifiers(m, args, command, originalCommand); + const modifiers = getModifiers(args); // Return early if the modifiers were invalid if (!modifiers.valid) { + m.edit(generateRollError('Modifiers invalid:', modifiers.error.name, modifiers.error.message)).catch((e) => utils.commonLoggers.messageEditError('roll.ts:50', m, e)); + + if (DEVMODE && config.logRolls) { + // If enabled, log rolls so we can verify the bots math + dbClient + .execute(queries.insertRollLogCmd(0, 1), [originalCommand, modifiers.error.name, m.id]) + .catch((e) => utils.commonLoggers.dbError('roll.ts:57', 'insert into', e)); + } return; } diff --git a/src/commands/roll/_index.ts b/src/commands/roll/_index.ts deleted file mode 100644 index 77bf87e..0000000 --- a/src/commands/roll/_index.ts +++ /dev/null @@ -1,5 +0,0 @@ -import { getModifiers } from 'commands/roll/getModifiers.ts'; - -export default { - getModifiers, -};