move getModifiers into artigen, simplify its error handling a lot

This commit is contained in:
Ean Milligan 2025-05-03 09:54:25 -04:00
parent 9bd757741b
commit 829ec0ecea
5 changed files with 28 additions and 51 deletions

View File

@ -44,8 +44,9 @@ export interface RollModifiers {
order: string; order: string;
count: boolean; count: boolean;
commaTotals: boolean; commaTotals: boolean;
valid: boolean;
apiWarn: string; apiWarn: string;
valid: boolean;
error: Error;
} }
// Basic conf interfaces // Basic conf interfaces

View File

@ -1,19 +1,8 @@
import { DiscordenoMessage } from '@discordeno';
import { log, LogTypes as LT } from '@Log4Deno'; import { log, LogTypes as LT } from '@Log4Deno';
import config from '~config';
import { DEVMODE } from '~flags';
import { RollModifiers } from 'artigen/dice/dice.d.ts'; import { RollModifiers } from 'artigen/dice/dice.d.ts';
import dbClient from 'db/client.ts'; export const getModifiers = (args: string[]): RollModifiers => {
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:';
const modifiers: RollModifiers = { const modifiers: RollModifiers = {
noDetails: false, noDetails: false,
superNoDetails: false, superNoDetails: false,
@ -26,13 +15,14 @@ export const getModifiers = (m: DiscordenoMessage, args: string[], command: stri
order: '', order: '',
count: false, count: false,
commaTotals: false, commaTotals: false,
valid: false,
apiWarn: '', apiWarn: '',
valid: false,
error: new Error(),
}; };
// Check if any of the args are command flags and pull those out into the modifiers object // 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++) { 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; let defaultCase = false;
switch (args[i].toLowerCase()) { switch (args[i].toLowerCase()) {
case '-c': case '-c':
@ -69,14 +59,8 @@ export const getModifiers = (m: DiscordenoMessage, args: string[], command: stri
} }
if (modifiers.gms.length < 1) { if (modifiers.gms.length < 1) {
// If -gm is on and none were found, throw an error // 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)); modifiers.error.name = 'NoGMsFound';
modifiers.error.message = 'Must specify at least one GM by @mentioning them';
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));
}
return modifiers; return modifiers;
} }
break; 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 (!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 // 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)); modifiers.error.name = 'NoOrderFound';
modifiers.error.message = 'Must specify `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(queries.insertRollLogCmd(0, 1), [originalCommand, 'NoOrderFound', m.id])
.catch((e) => utils.commonLoggers.dbError('getModifiers.ts:89', 'insert into', e));
}
return modifiers; 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 // 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) { 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) => modifiers.error.name = 'MaxAndNominal';
utils.commonLoggers.messageEditError('getModifiers.ts:106', m, e) modifiers.error.message = 'Can only use one of the following at a time:\n`maximize`, `minimize`, `nominal`';
);
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));
}
return modifiers; return modifiers;
} }

View File

@ -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: [ embeds: [
{ {
color: failColor, 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.`, value: `${errorMsg}\n\nPlease try again. If the error is repeated, please report the issue using the \`${config.prefix}report\` command.`,
}, },
], ],
footer: {
text: errorName,
},
}, },
], ],
}); });

View File

@ -4,14 +4,14 @@ import { log, LogTypes as LT } from '@Log4Deno';
import config from '~config'; import config from '~config';
import { DEVMODE } from '~flags'; import { DEVMODE } from '~flags';
import { getModifiers } from 'artigen/dice/getModifiers.ts';
import { sendRollRequest } from 'artigen/managers/queueManager.ts'; import { sendRollRequest } from 'artigen/managers/queueManager.ts';
import dbClient from 'db/client.ts'; import dbClient from 'db/client.ts';
import { queries } from 'db/common.ts'; import { queries } from 'db/common.ts';
import rollFuncs from 'commands/roll/_index.ts'; import { generateRollError, rollingEmbed, warnColor } from 'src/commandUtils.ts';
import { rollingEmbed, warnColor } from 'src/commandUtils.ts';
import utils from 'src/utils.ts'; import utils from 'src/utils.ts';
export const roll = async (message: DiscordenoMessage, args: string[], command: string) => { 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); const m = await message.reply(rollingEmbed);
// Get modifiers from command // Get modifiers from command
const modifiers = rollFuncs.getModifiers(m, args, command, originalCommand); const modifiers = getModifiers(args);
// Return early if the modifiers were invalid // Return early if the modifiers were invalid
if (!modifiers.valid) { 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; return;
} }

View File

@ -1,5 +0,0 @@
import { getModifiers } from 'commands/roll/getModifiers.ts';
export default {
getModifiers,
};