mirror of
https://github.com/Burn-E99/TheArtificer.git
synced 2026-01-08 21:37:54 -05:00
Initial work on V2.0.0
Updated dependencies and corrected syntax errors caused by the newer deps.
This commit is contained in:
1352
src/api.ts
1352
src/api.ts
File diff suppressed because it is too large
Load Diff
@ -6,16 +6,16 @@
|
||||
|
||||
import {
|
||||
// Discordeno deps
|
||||
CacheData
|
||||
cache, cacheHandlers
|
||||
} from "../deps.ts";
|
||||
import { LogTypes as LT } from "./utils.enums.ts";
|
||||
import utils from "./utils.ts";
|
||||
|
||||
import config from "../config.ts";
|
||||
|
||||
// getRandomStatus(bot cache) returns status as string
|
||||
// getRandomStatus() returns status as string
|
||||
// Gets a new random status for the bot
|
||||
const getRandomStatus = (cache: CacheData): string => {
|
||||
const getRandomStatus = async (): Promise<string> => {
|
||||
let status = "";
|
||||
switch (Math.floor((Math.random() * 4) + 1)) {
|
||||
case 1:
|
||||
@ -27,9 +27,11 @@ const getRandomStatus = (cache: CacheData): string => {
|
||||
case 3:
|
||||
status = `${config.prefix}info to learn more`;
|
||||
break;
|
||||
default:
|
||||
status = `Rolling dice for ${cache.guilds.size} servers`;
|
||||
default: {
|
||||
const cachedCount = await cacheHandlers.size("guilds")
|
||||
status = `Rolling dice for ${cachedCount + cache.dispatchedGuildIds.size} servers`;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return status;
|
||||
@ -37,7 +39,7 @@ const getRandomStatus = (cache: CacheData): string => {
|
||||
|
||||
// updateListStatistics(bot ID, current guild count) returns nothing
|
||||
// Sends the current server count to all bot list sites we are listed on
|
||||
const updateListStatistics = (botID: string, serverCount: number): void => {
|
||||
const updateListStatistics = (botID: bigint, serverCount: number): void => {
|
||||
config.botLists.forEach(async e => {
|
||||
utils.log(LT.LOG, `Updating statistics for ${JSON.stringify(e)}`)
|
||||
if (e.enabled) {
|
||||
@ -45,7 +47,7 @@ const updateListStatistics = (botID: string, serverCount: number): void => {
|
||||
tempHeaders.append(e.headers[0].header, e.headers[0].value);
|
||||
tempHeaders.append("Content-Type", "application/json");
|
||||
// ?{} is a template used in config, just need to replace it with the real value
|
||||
const response = await fetch(e.apiUrl.replace("?{bot_id}", botID), {
|
||||
const response = await fetch(e.apiUrl.replace("?{bot_id}", botID.toString()), {
|
||||
"method": 'POST',
|
||||
"headers": tempHeaders,
|
||||
"body": JSON.stringify(e.body).replace('"?{server_count}"', serverCount.toString()) // ?{server_count} needs the "" removed from around it aswell to make sure its sent as a number
|
||||
|
||||
@ -64,7 +64,7 @@ const escapeCharacters = (str: string, esc: string): string => {
|
||||
utils.log(LT.LOG, `Escaping character ${esc[i]} | ${str}, ${esc}`);
|
||||
// Create a new regex to look for that char that needs replaced and escape it
|
||||
const temprgx = new RegExp(`[${esc[i]}]`, "g");
|
||||
str = str.replace(temprgx, ("\\" + esc[i]));
|
||||
str = str.replace(temprgx, `\\${esc[i]}`);
|
||||
}
|
||||
return str;
|
||||
};
|
||||
@ -158,7 +158,7 @@ const roll = (rollStr: string, maximiseRoll: boolean, nominalRoll: boolean): Rol
|
||||
if (remains.length > 0) {
|
||||
// Determine if the first item is a drop, and if it is, add the d back in
|
||||
if (remains.search(/\D/) !== 0 || remains.indexOf("l") === 0 || remains.indexOf("h") === 0) {
|
||||
remains = "d" + remains;
|
||||
remains = `d${remains}`;
|
||||
}
|
||||
|
||||
// Loop until all remaining args are parsed
|
||||
@ -260,7 +260,7 @@ const roll = (rollStr: string, maximiseRoll: boolean, nominalRoll: boolean): Rol
|
||||
break;
|
||||
default:
|
||||
// Throw error immediately if unknown op is encountered
|
||||
throw new Error("UnknownOperation_" + tSep);
|
||||
throw new Error(`UnknownOperation_${tSep}`);
|
||||
}
|
||||
// Finally slice off everything else parsed this loop
|
||||
remains = remains.slice(afterNumIdx);
|
||||
@ -538,22 +538,22 @@ const formatRoll = (rollConf: string, maximiseRoll: boolean, nominalRoll: boolea
|
||||
// If the roll was a crit hit or fail, or dropped/rerolled, add the formatting needed
|
||||
if (e.critHit) {
|
||||
// Bold for crit success
|
||||
preFormat = "**" + preFormat;
|
||||
postFormat = postFormat + "**";
|
||||
preFormat = `**${preFormat}`;
|
||||
postFormat = `${postFormat}**`;
|
||||
}
|
||||
if (e.critFail) {
|
||||
// Underline for crit fail
|
||||
preFormat = "__" + preFormat;
|
||||
postFormat = postFormat + "__";
|
||||
preFormat = `__${preFormat}`;
|
||||
postFormat = `${postFormat}__`;
|
||||
}
|
||||
if (e.dropped || e.rerolled) {
|
||||
// Strikethrough for dropped/rerolled rolls
|
||||
preFormat = "~~" + preFormat;
|
||||
postFormat = postFormat + "~~";
|
||||
preFormat = `~~${preFormat}`;
|
||||
postFormat = `${postFormat}~~`;
|
||||
}
|
||||
|
||||
// Finally add this to the roll's details
|
||||
tempDetails += preFormat + e.roll + postFormat + " + ";
|
||||
tempDetails += `${preFormat}${e.roll}${postFormat} + `;
|
||||
});
|
||||
// After the looping is done, remove the extra " + " from the details and cap it with the closing ]
|
||||
tempDetails = tempDetails.substr(0, (tempDetails.length - 3));
|
||||
@ -660,13 +660,13 @@ const fullSolver = (conf: (string | number | SolvedStep)[], wrapDetails: boolean
|
||||
// If operand1 is a SolvedStep, populate our subStepSolve with its details and crit/fail flags
|
||||
if (typeof operand1 === "object") {
|
||||
oper1 = operand1.total;
|
||||
subStepSolve.details = operand1.details + "\\" + conf[i];
|
||||
subStepSolve.details = `${operand1.details}\\${conf[i]}`;
|
||||
subStepSolve.containsCrit = operand1.containsCrit;
|
||||
subStepSolve.containsFail = operand1.containsFail;
|
||||
} else {
|
||||
// else parse it as a number and add it to the subStep details
|
||||
oper1 = parseFloat(operand1.toString());
|
||||
subStepSolve.details = oper1.toString() + "\\" + conf[i];
|
||||
subStepSolve.details = `${oper1.toString()}\\${conf[i]}`;
|
||||
}
|
||||
|
||||
// If operand2 is a SolvedStep, populate our subStepSolve with its details without overriding what operand1 filled in
|
||||
@ -740,7 +740,7 @@ const fullSolver = (conf: (string | number | SolvedStep)[], wrapDetails: boolean
|
||||
|
||||
// If this was a nested call, add on parens around the details to show what math we've done
|
||||
if (wrapDetails) {
|
||||
stepSolve.details = "(" + stepSolve.details + ")";
|
||||
stepSolve.details = `(${stepSolve.details})`;
|
||||
}
|
||||
|
||||
// If our total has reached undefined for some reason, error out now
|
||||
@ -899,25 +899,25 @@ const parseRoll = (fullCmd: string, localPrefix: string, localPostfix: string, m
|
||||
|
||||
// If the roll containted a crit success or fail, set the formatting around it
|
||||
if (e.containsCrit) {
|
||||
preFormat = "**" + preFormat;
|
||||
postFormat = postFormat + "**";
|
||||
preFormat = `**${preFormat}`;
|
||||
postFormat = `${postFormat}**`;
|
||||
}
|
||||
if (e.containsFail) {
|
||||
preFormat = "__" + preFormat;
|
||||
postFormat = postFormat + "__";
|
||||
preFormat = `__${preFormat}`;
|
||||
postFormat = `${postFormat}__`;
|
||||
}
|
||||
|
||||
// Populate line2 (the results) and line3 (the details) with their data
|
||||
if (order === "") {
|
||||
line2 += preFormat + e.rollTotal + postFormat + escapeCharacters(e.rollPostFormat, "|*_~`");
|
||||
line2 += `${preFormat}${e.rollTotal}${postFormat}${escapeCharacters(e.rollPostFormat, "|*_~`")}`;
|
||||
} else {
|
||||
// If order is on, turn rolls into csv without formatting
|
||||
line2 += preFormat + e.rollTotal + postFormat + ", ";
|
||||
line2 += `${preFormat}${e.rollTotal}${postFormat}, `;
|
||||
}
|
||||
|
||||
line2 = line2.replace(/\*\*\*\*/g, "** **").replace(/____/g, "__ __").replace(/~~~~/g, "~~ ~~");
|
||||
|
||||
line3 += "`" + e.initConfig + "` = " + e.rollDetails + " = " + preFormat + e.rollTotal + postFormat + "\n";
|
||||
line3 += `\`${e.initConfig}\` = ${e.rollDetails} = ${preFormat}${e.rollTotal}${postFormat}\n`;
|
||||
});
|
||||
|
||||
// If order is on, remove trailing ", "
|
||||
@ -950,11 +950,11 @@ const parseRoll = (fullCmd: string, localPrefix: string, localPostfix: string, m
|
||||
errorMsg = "Formatting Error: CritScore range specified without a maximum, remove - or add maximum to correct";
|
||||
break;
|
||||
case "UnknownOperation":
|
||||
errorMsg = "Error: Unknown Operation " + errorDetails;
|
||||
errorMsg = `Error: Unknown Operation ${errorDetails}`;
|
||||
if (errorDetails === "-") {
|
||||
errorMsg += "\nNote: Negative numbers are not supported";
|
||||
} else if (errorDetails === " ") {
|
||||
errorMsg += "\nNote: Every roll must be closed by " + localPostfix;
|
||||
errorMsg += `\nNote: Every roll must be closed by ${localPostfix}`;
|
||||
}
|
||||
break;
|
||||
case "NoZerosAllowed":
|
||||
@ -982,7 +982,7 @@ const parseRoll = (fullCmd: string, localPrefix: string, localPostfix: string, m
|
||||
errorMsg += "Crit Score (cs)";
|
||||
break;
|
||||
default:
|
||||
errorMsg += "Unhandled - " + errorDetails;
|
||||
errorMsg += `Unhandled - ${errorDetails}`;
|
||||
break;
|
||||
}
|
||||
errorMsg += " cannot be zero";
|
||||
@ -1013,7 +1013,7 @@ const parseRoll = (fullCmd: string, localPrefix: string, localPostfix: string, m
|
||||
break;
|
||||
default:
|
||||
utils.log(LT.ERROR, `Undangled Error: ${errorName}, ${errorDetails}`);
|
||||
errorMsg = "Unhandled Error: " + solverError.message + "\nCheck input and try again, if issue persists, please use `[[report` to alert the devs of the issue";
|
||||
errorMsg = `Unhandled Error: ${solverError.message}\nCheck input and try again, if issue persists, please use \`[[report\` to alert the devs of the issue`;
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
24
src/utils.ts
24
src/utils.ts
@ -6,7 +6,7 @@
|
||||
|
||||
import {
|
||||
// Discordeno deps
|
||||
Message, MessageContent,
|
||||
sendMessage,
|
||||
|
||||
// nanoid deps
|
||||
nanoid
|
||||
@ -35,9 +35,9 @@ const ask = async (question: string, stdin = Deno.stdin, stdout = Deno.stdout):
|
||||
return answer.trim();
|
||||
};
|
||||
|
||||
// cmdPrompt(logChannel, botName, sendMessage) returns nothing
|
||||
// cmdPrompt(logChannel, botName) returns nothing
|
||||
// cmdPrompt creates an interactive CLI for the bot, commands can vary
|
||||
const cmdPrompt = async (logChannel: string, botName: string, sendMessage: (c: string, m: string) => Promise<Message>): Promise<void> => {
|
||||
const cmdPrompt = async (logChannel: bigint, botName: string): Promise<void> => {
|
||||
let done = false;
|
||||
|
||||
while (!done) {
|
||||
@ -69,10 +69,10 @@ const cmdPrompt = async (logChannel: string, botName: string, sendMessage: (c: s
|
||||
// Sends [message] to specified [channel]
|
||||
else if (command === "m") {
|
||||
try {
|
||||
const channelID = args.shift() || "";
|
||||
const channelId = args.shift() || "";
|
||||
const message = args.join(" ");
|
||||
|
||||
sendMessage(channelID, message).catch(reason => {
|
||||
sendMessage(BigInt(channelId), message).catch(reason => {
|
||||
console.error(reason);
|
||||
});
|
||||
}
|
||||
@ -104,18 +104,6 @@ const cmdPrompt = async (logChannel: string, botName: string, sendMessage: (c: s
|
||||
}
|
||||
};
|
||||
|
||||
// sendIndirectMessage(originalMessage, messageContent, sendMessage, sendDirectMessage) returns Message
|
||||
// sendIndirectMessage determines if the message needs to be sent as a direct message or as a normal message
|
||||
const sendIndirectMessage = async (originalMessage: Message, messageContent: (string | MessageContent), sendMessage: (c: string, m: (string | MessageContent)) => Promise<Message>, sendDirectMessage: (c: string, m: (string | MessageContent)) => Promise<Message>): Promise<Message> => {
|
||||
if (originalMessage.guildID === "") {
|
||||
// guildID was empty, meaning the original message was sent as a DM
|
||||
return await sendDirectMessage(originalMessage.author.id, messageContent);
|
||||
} else {
|
||||
// guildID was not empty, meaning the original message was sent in a server
|
||||
return await sendMessage(originalMessage.channelID, messageContent);
|
||||
}
|
||||
};
|
||||
|
||||
// initLog() returns nothing
|
||||
// Handles ensuring the required directory structure is created
|
||||
const initLog = (name: string): void => {
|
||||
@ -161,4 +149,4 @@ const log = async (level: LogTypes, message: string, error = new Error()): Promi
|
||||
}
|
||||
};
|
||||
|
||||
export default { cmdPrompt, sendIndirectMessage, initLog, log };
|
||||
export default { cmdPrompt, initLog, log };
|
||||
|
||||
Reference in New Issue
Block a user