1
1
mirror of https://github.com/Burn-E99/TheArtificer.git synced 2026-06-04 09:03:50 -04:00

Roll command now utilizes embeds, count decorator (-c) complete, added [[api h shorthand

This commit is contained in:
Ean Milligan (Bastion)
2022-05-27 20:59:09 -04:00
parent bbba797dc3
commit b887b93bb2
7 changed files with 254 additions and 155 deletions

View File

@@ -31,7 +31,7 @@ export const api = async (message: DiscordenoMessage, args: string[]) => {
if (await hasGuildPermissions(message.authorId, message.guildId, ['ADMINISTRATOR'])) {
// [[api help
// Shows API help details
if (apiArg === 'help') {
if (apiArg === 'help' || apiArg === 'h') {
apiCommands.help(message);
} // [[api allow/block
// Lets a guild admin allow or ban API rolls from happening in said guild

View File

@@ -10,7 +10,8 @@ import {
sendDirectMessage,
} from '../../deps.ts';
import solver from '../solver/_index.ts';
import { constantCmds, generateDMFailed } from '../constantCmds.ts';
import { SolvedRoll } from '../solver/solver.d.ts';
import { constantCmds, generateCountDetailsEmbed, generateDMFailed, generateRollEmbed } from '../constantCmds.ts';
import rollFuncs from './roll/_index.ts';
export const roll = async (message: DiscordenoMessage, args: string[], command: string) => {
@@ -31,11 +32,15 @@ export const roll = async (message: DiscordenoMessage, args: string[], command:
try {
const originalCommand = `${config.prefix}${command} ${args.join(' ')}`;
const m = await message.send(constantCmds.rolling);
const m = await message.reply(constantCmds.rolling);
// Get modifiers from command
const modifiers = rollFuncs.getModifiers(m, args, command, originalCommand);
// gmModifiers used to create gmEmbed (basically just turn off the gmRoll)
const gmModifiers = JSON.parse(JSON.stringify(modifiers));
gmModifiers.gmRoll = false;
// Return early if the modifiers were invalid
if (!modifiers.valid) {
return;
@@ -43,111 +48,58 @@ export const roll = async (message: DiscordenoMessage, args: string[], command:
// Rejoin all of the args and send it into the solver, if solver returns a falsy item, an error object will be substituded in
const rollCmd = `${command} ${args.join(' ')}`;
const returnmsg = solver.parseRoll(rollCmd, modifiers) || { error: true, errorCode: 'EmptyMessage', errorMsg: 'Error: Empty message', line1: '', line2: '', line3: '' };
const returnmsg = solver.parseRoll(rollCmd, modifiers) || <SolvedRoll> { error: true, errorCode: 'EmptyMessage', errorMsg: 'Error: Empty message' };
let returnText = '';
const pubEmbedDetails = await generateRollEmbed(message.authorId, returnmsg, modifiers);
const gmEmbedDetails = await generateRollEmbed(message.authorId, returnmsg, gmModifiers);
const countEmbed = generateCountDetailsEmbed(returnmsg.counts);
// If there was an error, report it to the user in hopes that they can determine what they did wrong
if (returnmsg.error) {
returnText = returnmsg.errorMsg;
m.edit(returnText);
m.edit({embeds: [pubEmbedDetails.embed]});
if (DEVMODE && config.logRolls) {
// If enabled, log rolls so we can verify the bots math
// If enabled, log rolls so we can see what went wrong
dbClient.execute(queries.insertRollLogCmd(0, 1), [originalCommand, returnmsg.errorCode, m.id]).catch((e) => {
log(LT.ERROR, `Failed to insert into DB: ${JSON.stringify(e)}`);
});
}
return;
} else {
// Else format the output using details from the solver
returnText = `<@${message.authorId}>${returnmsg.line1}\n${returnmsg.line2}`;
// Determine if we are to send a GM roll or a normal roll
if (modifiers.gmRoll) {
// Send the public embed to correct channel
m.edit({embeds: [pubEmbedDetails.embed]});
if (!modifiers.superNoDetails) {
if (modifiers.noDetails) {
returnText += '\nDetails suppressed by -nd flag.';
} else {
returnText += `\nDetails:\n${modifiers.spoiler}${returnmsg.line3}${modifiers.spoiler}`;
}
}
}
// If the roll was a GM roll, send DMs to all the GMs
if (modifiers.gmRoll) {
// Make a new return line to be sent to the roller
const normalText = `<@${message.authorId}>${returnmsg.line1}\nResults have been messaged to the following GMs: ${modifiers.gms.join(' ')}`;
// And message the full details to each of the GMs, alerting roller of every GM that could not be messaged
modifiers.gms.forEach(async (e) => {
log(LT.LOG, `Messaging GM ${e}`);
// If its too big, collapse it into a .txt file and send that instead.
const b = await new Blob([returnText as BlobPart], { 'type': 'text' });
if (b.size > 8388290) {
// Update return text
// todo: embedify
returnText =
`<@${message.authorId}>${returnmsg.line1}\n${returnmsg.line2}\nFull details could not be attached to this messaged as a \`.txt\` file as the file would be too large for Discord to handle. If you would like to see the details of rolls, please send the rolls in multiple messages instead of bundled into one.`;
// Attempt to DM the GMs and send a warning if it could not DM a GM
await sendDirectMessage(BigInt(e.substring(2, e.length - 1)), returnText).catch(() => {
message.send(generateDMFailed(e));
// And message the full details to each of the GMs, alerting roller of every GM that could not be messaged
modifiers.gms.forEach(async (gm) => {
log(LT.LOG, `Messaging GM ${gm}`);
// Attempt to DM the GM and send a warning if it could not DM a GM
await sendDirectMessage(BigInt(gm.substring(2, gm.length - 1)), {
embeds: modifiers.count ? [gmEmbedDetails.embed, countEmbed] : [gmEmbedDetails.embed],
}).then(async () => {
// Check if we need to attach a file and send it after the initial details sent
if (gmEmbedDetails.hasAttachment) {
await sendDirectMessage(BigInt(gm.substring(2, gm.length - 1)), {
file: gmEmbedDetails.attachment,
}).catch(() => {
message.reply(generateDMFailed(gm));
});
}
}).catch(() => {
message.reply(generateDMFailed(gm));
});
} else {
// Update return
// todo: embedify
returnText = `<@${message.authorId}>${returnmsg.line1}\n${returnmsg.line2}\nFull details have been attached to this messaged as a \`.txt\` file for verification purposes.`;
// Attempt to DM the GMs and send a warning if it could not DM a GM
await sendDirectMessage(BigInt(e.substring(2, e.length - 1)), { 'content': returnText, 'file': { 'blob': b, 'name': 'rollDetails.txt' } }).catch(() => {
message.send(generateDMFailed(e));
});
}
});
// Finally send the text
m.edit(normalText);
if (DEVMODE && config.logRolls) {
// If enabled, log rolls so we can verify the bots math
dbClient.execute(queries.insertRollLogCmd(0, 0), [originalCommand, returnText, m.id]).catch((e) => {
log(LT.ERROR, `Failed to insert into DB: ${JSON.stringify(e)}`);
});
}
} else {
// When not a GM roll, make sure the message is not too big
if (returnText.length > 2000) {
// If its too big, collapse it into a .txt file and send that instead.
const b = await new Blob([returnText as BlobPart], { 'type': 'text' });
if (b.size > 8388290) {
// Update return text
returnText =
`<@${message.authorId}>${returnmsg.line1}\n${returnmsg.line2}\nDetails have been ommitted from this message for being over 2000 characters. Full details could not be attached to this messaged as a \`.txt\` file as the file would be too large for Discord to handle. If you would like to see the details of rolls, please send the rolls in multiple messages instead of bundled into one.`;
// Send the results
m.edit(returnText);
} else {
// Update return text
returnText =
`<@${message.authorId}>${returnmsg.line1}\n${returnmsg.line2}\nDetails have been ommitted from this message for being over 2000 characters. Full details have been attached to this messaged as a \`.txt\` file for verification purposes.`;
// Remove the original message to send new one with attachment
m.delete();
// todo: embedify
await message.send({ 'content': returnText, 'file': { 'blob': b, 'name': 'rollDetails.txt' } });
}
} else {
// Finally send the text
m.edit(returnText);
}
if (DEVMODE && config.logRolls) {
// If enabled, log rolls so we can verify the bots math
dbClient.execute(queries.insertRollLogCmd(0, 0), [originalCommand, returnText, m.id]).catch((e) => {
log(LT.ERROR, `Failed to insert into DB: ${JSON.stringify(e)}`);
// Not a gm roll, so just send normal embed to correct channel
await m.edit({
embeds: modifiers.count ? [pubEmbedDetails.embed, countEmbed] : [pubEmbedDetails.embed],
});
if (pubEmbedDetails.hasAttachment) {
// Attachment requires you to send a new message
message.send({
file: pubEmbedDetails.attachment,
});
}
}
}
} catch (e) {