mirror of
https://github.com/Burn-E99/TheArtificer.git
synced 2026-06-04 09:03:50 -04:00
move getModifiers into artigen, simplify its error handling a lot
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +0,0 @@
|
||||
import { getModifiers } from 'commands/roll/getModifiers.ts';
|
||||
|
||||
export default {
|
||||
getModifiers,
|
||||
};
|
||||
@@ -1,134 +0,0 @@
|
||||
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:';
|
||||
const modifiers: RollModifiers = {
|
||||
noDetails: false,
|
||||
superNoDetails: false,
|
||||
spoiler: '',
|
||||
maxRoll: false,
|
||||
minRoll: false,
|
||||
nominalRoll: false,
|
||||
gmRoll: false,
|
||||
gms: [],
|
||||
order: '',
|
||||
count: false,
|
||||
commaTotals: false,
|
||||
valid: false,
|
||||
apiWarn: '',
|
||||
};
|
||||
|
||||
// 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}`);
|
||||
let defaultCase = false;
|
||||
switch (args[i].toLowerCase()) {
|
||||
case '-c':
|
||||
modifiers.count = true;
|
||||
break;
|
||||
case '-nd':
|
||||
modifiers.noDetails = true;
|
||||
break;
|
||||
case '-snd':
|
||||
modifiers.superNoDetails = true;
|
||||
break;
|
||||
case '-s':
|
||||
modifiers.spoiler = '||';
|
||||
break;
|
||||
case '-max':
|
||||
case '-m':
|
||||
modifiers.maxRoll = true;
|
||||
break;
|
||||
case '-min':
|
||||
modifiers.minRoll = true;
|
||||
break;
|
||||
case '-n':
|
||||
modifiers.nominalRoll = true;
|
||||
break;
|
||||
case '-gm':
|
||||
modifiers.gmRoll = true;
|
||||
|
||||
// -gm is a little more complex, as we must get all of the GMs that need to be DMd
|
||||
while (i + 1 < args.length && args[i + 1].startsWith('<@')) {
|
||||
log(LT.LOG, `Finding all GMs, checking args ${JSON.stringify(args)}`);
|
||||
// Keep looping thru the rest of the args until one does not start with the discord mention code
|
||||
modifiers.gms.push(args[i + 1].replace(/!/g, ''));
|
||||
args.splice(i + 1, 1);
|
||||
}
|
||||
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));
|
||||
}
|
||||
return modifiers;
|
||||
}
|
||||
break;
|
||||
case '-o':
|
||||
// Shift the -o out of the array so the next item is the direction
|
||||
args.splice(i, 1);
|
||||
|
||||
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));
|
||||
}
|
||||
return modifiers;
|
||||
}
|
||||
|
||||
modifiers.order = args[i].toLowerCase()[0];
|
||||
break;
|
||||
case '-ct':
|
||||
modifiers.commaTotals = true;
|
||||
break;
|
||||
default:
|
||||
// Default case should not mess with the array
|
||||
defaultCase = true;
|
||||
break;
|
||||
}
|
||||
|
||||
if (!defaultCase) {
|
||||
args.splice(i, 1);
|
||||
i--;
|
||||
}
|
||||
}
|
||||
|
||||
// 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));
|
||||
}
|
||||
return modifiers;
|
||||
}
|
||||
|
||||
modifiers.valid = true;
|
||||
return modifiers;
|
||||
};
|
||||
Reference in New Issue
Block a user