mirror of
https://github.com/Burn-E99/TheArtificer.git
synced 2026-06-04 00:53:50 -04:00
utils.log implemented
Log files will now be created locally for easy debugging api.ts, mod.ts, intervals.ts, solver.ts - replaced console.log with utils.log mod.d.ts - made object syntax consistent utils.enums.ts - Created LogTypes enum for utils.log utils.ts - Created initLog and log for logging to files
This commit is contained in:
74
src/api.ts
74
src/api.ts
@@ -20,6 +20,8 @@ import {
|
||||
} from "../deps.ts";
|
||||
|
||||
import solver from "./solver.ts";
|
||||
import { LogTypes as LT } from "./utils.enums.ts";
|
||||
import utils from "./utils.ts";
|
||||
|
||||
import config from "../config.ts";
|
||||
|
||||
@@ -27,7 +29,7 @@ import config from "../config.ts";
|
||||
// start initializes and runs the entire API for the bot
|
||||
const start = async (dbClient: Client, cache: CacheData, sendMessage: (c: string, m: (string | MessageContent)) => Promise<Message>, sendDirectMessage: (c: string, m: (string | MessageContent)) => Promise<Message>): Promise<void> => {
|
||||
const server = serve({ hostname: "0.0.0.0", port: config.api.port });
|
||||
console.log(`HTTP api running at: http://localhost:${config.api.port}/`);
|
||||
utils.log(LT.LOG, `HTTP api running at: http://localhost:${config.api.port}/`);
|
||||
|
||||
// rateLimitTime holds all users with the last time they started a rate limit timer
|
||||
const rateLimitTime = new Map<string, number>();
|
||||
@@ -108,8 +110,8 @@ const start = async (dbClient: Client, cache: CacheData, sendMessage: (c: string
|
||||
let erroredOut = false;
|
||||
|
||||
// Insert new key/user pair into the db
|
||||
await dbClient.execute("INSERT INTO all_keys(userid,apiKey) values(?,?)", [apiUserid, newKey]).catch(() => {
|
||||
console.log("Failed to insert into database 20");
|
||||
await dbClient.execute("INSERT INTO all_keys(userid,apiKey) values(?,?)", [apiUserid, newKey]).catch(e => {
|
||||
utils.log(LT.ERROR, `Failed to insert into database: ${JSON.stringify(e)}`);
|
||||
request.respond({ status: Status.InternalServerError, body: STATUS_TEXT.get(Status.InternalServerError) });
|
||||
erroredOut = true;
|
||||
});
|
||||
@@ -139,8 +141,8 @@ const start = async (dbClient: Client, cache: CacheData, sendMessage: (c: string
|
||||
let erroredOut = false;
|
||||
|
||||
// Get all channels userid has authorized
|
||||
const dbAllowedChannelQuery = await dbClient.query("SELECT * FROM allowed_channels WHERE userid = ?", [apiUserid]).catch(() => {
|
||||
console.log("Failed to query database 22");
|
||||
const dbAllowedChannelQuery = await dbClient.query("SELECT * FROM allowed_channels WHERE userid = ?", [apiUserid]).catch(e => {
|
||||
utils.log(LT.ERROR, `Failed to insert into database: ${JSON.stringify(e)}`);
|
||||
request.respond({ status: Status.InternalServerError, body: STATUS_TEXT.get(Status.InternalServerError) });
|
||||
erroredOut = true;
|
||||
});
|
||||
@@ -206,8 +208,8 @@ const start = async (dbClient: Client, cache: CacheData, sendMessage: (c: string
|
||||
request.respond({ status: Status.BadRequest, body: STATUS_TEXT.get(Status.BadRequest) });
|
||||
|
||||
// Always log API rolls for abuse detection
|
||||
dbClient.execute("INSERT INTO roll_log(input,result,resultid,api,error) values(?,?,?,1,1)", [originalCommand, "EmptyInput", null]).catch(() => {
|
||||
console.log("Failed to insert into database 10");
|
||||
dbClient.execute("INSERT INTO roll_log(input,result,resultid,api,error) values(?,?,?,1,1)", [originalCommand, "EmptyInput", null]).catch(e => {
|
||||
utils.log(LT.ERROR, `Failed to insert into database: ${JSON.stringify(e)}`);
|
||||
});
|
||||
break;
|
||||
}
|
||||
@@ -217,8 +219,8 @@ const start = async (dbClient: Client, cache: CacheData, sendMessage: (c: string
|
||||
request.respond({ status: Status.BadRequest, body: STATUS_TEXT.get(Status.BadRequest) });
|
||||
|
||||
// Always log API rolls for abuse detection
|
||||
dbClient.execute("INSERT INTO roll_log(input,result,resultid,api,error) values(?,?,?,1,1)", [originalCommand, "BadOrder", null]).catch(() => {
|
||||
console.log("Failed to insert into database 10");
|
||||
dbClient.execute("INSERT INTO roll_log(input,result,resultid,api,error) values(?,?,?,1,1)", [originalCommand, "BadOrder", null]).catch(e => {
|
||||
utils.log(LT.ERROR, `Failed to insert into database: ${JSON.stringify(e)}`);
|
||||
});
|
||||
break;
|
||||
}
|
||||
@@ -238,8 +240,8 @@ const start = async (dbClient: Client, cache: CacheData, sendMessage: (c: string
|
||||
request.respond({ status: Status.InternalServerError, body: returnmsg.errorMsg });
|
||||
|
||||
// Always log API rolls for abuse detection
|
||||
dbClient.execute("INSERT INTO roll_log(input,result,resultid,api,error) values(?,?,?,1,1)", [originalCommand, returnmsg.errorCode, null]).catch(() => {
|
||||
console.log("Failed to insert into database 11");
|
||||
dbClient.execute("INSERT INTO roll_log(input,result,resultid,api,error) values(?,?,?,1,1)", [originalCommand, returnmsg.errorCode, null]).catch(e => {
|
||||
utils.log(LT.ERROR, `Failed to insert into database: ${JSON.stringify(e)}`);
|
||||
});
|
||||
break;
|
||||
} else {
|
||||
@@ -268,8 +270,8 @@ const start = async (dbClient: Client, cache: CacheData, sendMessage: (c: string
|
||||
request.respond({ status: Status.BadRequest, body: STATUS_TEXT.get(Status.BadRequest) });
|
||||
|
||||
// Always log API rolls for abuse detection
|
||||
dbClient.execute("INSERT INTO roll_log(input,result,resultid,api,error) values(?,?,?,1,1)", [originalCommand, "NoGMsSent", null]).catch(() => {
|
||||
console.log("Failed to insert into database 12");
|
||||
dbClient.execute("INSERT INTO roll_log(input,result,resultid,api,error) values(?,?,?,1,1)", [originalCommand, "NoGMsSent", null]).catch(e => {
|
||||
utils.log(LT.ERROR, `Failed to insert into database: ${JSON.stringify(e)}`);
|
||||
});
|
||||
break;
|
||||
}
|
||||
@@ -307,12 +309,12 @@ const start = async (dbClient: Client, cache: CacheData, sendMessage: (c: string
|
||||
// Send the return message as a DM or normal message depening on if the channel is set
|
||||
if ((query.get("channel") || "").length > 0) {
|
||||
m = await sendMessage(query.get("channel") || "", failedSend).catch(() => {
|
||||
request.respond({ status: Status.InternalServerError, body: "Message 10 failed to send." });
|
||||
request.respond({ status: Status.InternalServerError, body: "Message failed to send." });
|
||||
errorOut = true;
|
||||
});
|
||||
} else {
|
||||
m = await sendDirectMessage(query.get("user") || "", failedSend).catch(() => {
|
||||
request.respond({ status: Status.InternalServerError, body: "Message 11 failed to send." });
|
||||
request.respond({ status: Status.InternalServerError, body: "Message failed to send." });
|
||||
errorOut = true;
|
||||
});
|
||||
}
|
||||
@@ -320,8 +322,8 @@ const start = async (dbClient: Client, cache: CacheData, sendMessage: (c: string
|
||||
});
|
||||
|
||||
// Always log API rolls for abuse detection
|
||||
dbClient.execute("INSERT INTO roll_log(input,result,resultid,api,error) values(?,?,?,1,0)", [originalCommand, returnText, ((typeof m === "object") ? m.id : null)]).catch(() => {
|
||||
console.log("Failed to insert into database 13");
|
||||
dbClient.execute("INSERT INTO roll_log(input,result,resultid,api,error) values(?,?,?,1,0)", [originalCommand, returnText, ((typeof m === "object") ? m.id : null)]).catch(e => {
|
||||
utils.log(LT.ERROR, `Failed to insert into database: ${JSON.stringify(e)}`);
|
||||
});
|
||||
|
||||
// Handle closing the request out
|
||||
@@ -362,8 +364,8 @@ const start = async (dbClient: Client, cache: CacheData, sendMessage: (c: string
|
||||
}
|
||||
|
||||
// If enabled, log rolls so we can verify the bots math
|
||||
dbClient.execute("INSERT INTO roll_log(input,result,resultid,api,error) values(?,?,?,1,0)", [originalCommand, returnText, ((typeof m === "object") ? m.id : null)]).catch(() => {
|
||||
console.log("Failed to insert into database 14");
|
||||
dbClient.execute("INSERT INTO roll_log(input,result,resultid,api,error) values(?,?,?,1,0)", [originalCommand, returnText, ((typeof m === "object") ? m.id : null)]).catch(e => {
|
||||
utils.log(LT.ERROR, `Failed to insert into database: ${JSON.stringify(e)}`);
|
||||
});
|
||||
|
||||
// Handle closing the request out
|
||||
@@ -376,7 +378,7 @@ const start = async (dbClient: Client, cache: CacheData, sendMessage: (c: string
|
||||
}
|
||||
} catch (err) {
|
||||
// Handle any errors we missed
|
||||
console.log(err)
|
||||
utils.log(LT.ERROR, `Unhandled Error: ${JSON.stringify(err)}`);
|
||||
request.respond({ status: Status.InternalServerError, body: STATUS_TEXT.get(Status.InternalServerError) });
|
||||
}
|
||||
} else {
|
||||
@@ -404,8 +406,8 @@ const start = async (dbClient: Client, cache: CacheData, sendMessage: (c: string
|
||||
let erroredOut = false;
|
||||
|
||||
// Insert new user/channel pair into the db
|
||||
await dbClient.execute("INSERT INTO allowed_channels(userid,channelid) values(?,?)", [apiUserid, BigInt(query.get("channel"))]).catch(() => {
|
||||
console.log("Failed to insert into database 21");
|
||||
await dbClient.execute("INSERT INTO allowed_channels(userid,channelid) values(?,?)", [apiUserid, BigInt(query.get("channel"))]).catch(e => {
|
||||
utils.log(LT.ERROR, `Failed to insert into database: ${JSON.stringify(e)}`);
|
||||
request.respond({ status: Status.InternalServerError, body: STATUS_TEXT.get(Status.InternalServerError) });
|
||||
erroredOut = true;
|
||||
});
|
||||
@@ -463,8 +465,8 @@ const start = async (dbClient: Client, cache: CacheData, sendMessage: (c: string
|
||||
}
|
||||
|
||||
// Execute the DB modification
|
||||
await dbClient.execute("UPDATE all_keys SET ?? = ? WHERE userid = ?", [key, value, apiUserid]).catch(() => {
|
||||
console.log("Failed to update database 28");
|
||||
await dbClient.execute("UPDATE all_keys SET ?? = ? WHERE userid = ?", [key, value, apiUserid]).catch(e => {
|
||||
utils.log(LT.ERROR, `Failed to insert into database: ${JSON.stringify(e)}`);
|
||||
request.respond({ status: Status.InternalServerError, body: STATUS_TEXT.get(Status.InternalServerError) });
|
||||
erroredOut = true;
|
||||
});
|
||||
@@ -503,8 +505,8 @@ const start = async (dbClient: Client, cache: CacheData, sendMessage: (c: string
|
||||
}
|
||||
|
||||
// Execute the DB modification
|
||||
await dbClient.execute("UPDATE allowed_channels SET banned = ? WHERE userid = ? AND channelid = ?", [value, apiUserid, BigInt(query.get("channel"))]).catch(() => {
|
||||
console.log("Failed to update database 24");
|
||||
await dbClient.execute("UPDATE allowed_channels SET banned = ? WHERE userid = ? AND channelid = ?", [value, apiUserid, BigInt(query.get("channel"))]).catch(e => {
|
||||
utils.log(LT.ERROR, `Failed to insert into database: ${JSON.stringify(e)}`);
|
||||
request.respond({ status: Status.InternalServerError, body: STATUS_TEXT.get(Status.InternalServerError) });
|
||||
erroredOut = true;
|
||||
});
|
||||
@@ -543,8 +545,8 @@ const start = async (dbClient: Client, cache: CacheData, sendMessage: (c: string
|
||||
}
|
||||
|
||||
// Update the requested entry
|
||||
await dbClient.execute("UPDATE allowed_channels SET active = ? WHERE userid = ? AND channelid = ?", [value, apiUserid, BigInt(query.get("channel"))]).catch(() => {
|
||||
console.log("Failed to update database 26");
|
||||
await dbClient.execute("UPDATE allowed_channels SET active = ? WHERE userid = ? AND channelid = ?", [value, apiUserid, BigInt(query.get("channel"))]).catch(e => {
|
||||
utils.log(LT.ERROR, `Failed to insert into database: ${JSON.stringify(e)}`);
|
||||
request.respond({ status: Status.InternalServerError, body: STATUS_TEXT.get(Status.InternalServerError) });
|
||||
erroredOut = true;
|
||||
});
|
||||
@@ -583,8 +585,8 @@ const start = async (dbClient: Client, cache: CacheData, sendMessage: (c: string
|
||||
// User has recieved their delete code and we need to delete the account now
|
||||
let erroredOut = false;
|
||||
|
||||
await dbClient.execute("DELETE FROM allowed_channels WHERE userid = ?", [apiUserid]).catch(() => {
|
||||
console.log("Failed to delete from database 2A");
|
||||
await dbClient.execute("DELETE FROM allowed_channels WHERE userid = ?", [apiUserid]).catch(e => {
|
||||
utils.log(LT.ERROR, `Failed to insert into database: ${JSON.stringify(e)}`);
|
||||
request.respond({ status: Status.InternalServerError, body: STATUS_TEXT.get(Status.InternalServerError) });
|
||||
erroredOut = true;
|
||||
});
|
||||
@@ -592,8 +594,8 @@ const start = async (dbClient: Client, cache: CacheData, sendMessage: (c: string
|
||||
break;
|
||||
}
|
||||
|
||||
await dbClient.execute("DELETE FROM all_keys WHERE userid = ?", [apiUserid]).catch(() => {
|
||||
console.log("Failed to delete from database 2B");
|
||||
await dbClient.execute("DELETE FROM all_keys WHERE userid = ?", [apiUserid]).catch(e => {
|
||||
utils.log(LT.ERROR, `Failed to insert into database: ${JSON.stringify(e)}`);
|
||||
request.respond({ status: Status.InternalServerError, body: STATUS_TEXT.get(Status.InternalServerError) });
|
||||
erroredOut = true;
|
||||
});
|
||||
@@ -615,8 +617,8 @@ const start = async (dbClient: Client, cache: CacheData, sendMessage: (c: string
|
||||
let erroredOut = false;
|
||||
|
||||
// Execute the DB modification
|
||||
await dbClient.execute("UPDATE all_keys SET deleteCode = ? WHERE userid = ?", [deleteCode, apiUserid]).catch(() => {
|
||||
console.log("Failed to update database 29");
|
||||
await dbClient.execute("UPDATE all_keys SET deleteCode = ? WHERE userid = ?", [deleteCode, apiUserid]).catch(e => {
|
||||
utils.log(LT.ERROR, `Failed to insert into database: ${JSON.stringify(e)}`);
|
||||
request.respond({ status: Status.InternalServerError, body: STATUS_TEXT.get(Status.InternalServerError) });
|
||||
erroredOut = true;
|
||||
});
|
||||
@@ -689,8 +691,8 @@ const start = async (dbClient: Client, cache: CacheData, sendMessage: (c: string
|
||||
let erroredOut = false;
|
||||
|
||||
// Insert new key/user pair into the db
|
||||
await dbClient.execute("INSERT INTO all_keys(userid,apiKey,email) values(?,?,?)", [BigInt(query.get("user")), newKey, (query.get("email") || "").toLowerCase()]).catch(() => {
|
||||
console.log("Failed to insert into database 20");
|
||||
await dbClient.execute("INSERT INTO all_keys(userid,apiKey,email) values(?,?,?)", [BigInt(query.get("user")), newKey, (query.get("email") || "").toLowerCase()]).catch(e => {
|
||||
utils.log(LT.ERROR, `Failed to insert into database: ${JSON.stringify(e)}`);
|
||||
request.respond({ status: Status.InternalServerError, body: STATUS_TEXT.get(Status.InternalServerError) });
|
||||
erroredOut = true;
|
||||
});
|
||||
|
||||
@@ -8,6 +8,8 @@ import {
|
||||
// Discordeno deps
|
||||
CacheData
|
||||
} from "../deps.ts";
|
||||
import { LogTypes as LT } from "./utils.enums.ts";
|
||||
import utils from "./utils.ts";
|
||||
|
||||
import config from "../config.ts";
|
||||
|
||||
@@ -47,7 +49,7 @@ const updateListStatistics = (botID: string, serverCount: number): void => {
|
||||
"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
|
||||
});
|
||||
console.log(response);
|
||||
utils.log(LT.LOG, `${JSON.stringify(response)}`);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
10
src/mod.d.ts
vendored
10
src/mod.d.ts
vendored
@@ -2,9 +2,9 @@
|
||||
|
||||
// EmojiConf is used as a structure for the emojis stored in config.ts
|
||||
export type EmojiConf = {
|
||||
"name": string,
|
||||
"aliases": Array<string>,
|
||||
"id": string,
|
||||
"animated": boolean,
|
||||
"deleteSender": boolean
|
||||
name: string,
|
||||
aliases: Array<string>,
|
||||
id: string,
|
||||
animated: boolean,
|
||||
deleteSender: boolean
|
||||
};
|
||||
|
||||
@@ -5,6 +5,8 @@
|
||||
*/
|
||||
|
||||
import { RollSet, SolvedStep, SolvedRoll, ReturnData } from "./solver.d.ts";
|
||||
import { LogTypes as LT } from "./utils.enums.ts";
|
||||
import utils from "./utils.ts";
|
||||
|
||||
// MAXLOOPS determines how long the bot will attempt a roll
|
||||
// Default is 5000000 (5 million), which results in at most a 10 second delay before the bot calls the roll infinite or too complex
|
||||
@@ -987,7 +989,7 @@ const parseRoll = (fullCmd: string, localPrefix: string, localPostfix: string, m
|
||||
errorMsg = "Error: Roll became undefined, one or more operands are not a roll or a number, check input";
|
||||
break;
|
||||
default:
|
||||
console.error(errorName, errorDetails);
|
||||
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";
|
||||
break;
|
||||
}
|
||||
|
||||
13
src/utils.enums.ts
Normal file
13
src/utils.enums.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
/* The Artificer was built in memory of Babka
|
||||
* With love, Ean
|
||||
*
|
||||
* December 21, 2020
|
||||
*/
|
||||
|
||||
// enum for all possible console.log types
|
||||
export enum LogTypes {
|
||||
LOG = "log",
|
||||
INFO = "info",
|
||||
WARN = "warn",
|
||||
ERROR = "error"
|
||||
}
|
||||
56
src/utils.ts
56
src/utils.ts
@@ -6,9 +6,19 @@
|
||||
|
||||
import {
|
||||
// Discordeno deps
|
||||
Message, MessageContent
|
||||
Message, MessageContent,
|
||||
|
||||
// nanoid deps
|
||||
nanoid
|
||||
} from "../deps.ts";
|
||||
|
||||
import { LogTypes } from "./utils.enums.ts";
|
||||
|
||||
// Constant initialized at runtime for consistent file names
|
||||
let startDate: string;
|
||||
let logFolder: string;
|
||||
let initialized = false;
|
||||
|
||||
// split2k(longMessage) returns shortMessage[]
|
||||
// split2k takes a long string in and cuts it into shorter strings to be sent in Discord
|
||||
const split2k = (chunk: string): string[] => {
|
||||
@@ -143,6 +153,46 @@ const sendIndirectMessage = async (originalMessage: Message, messageContent: (st
|
||||
}
|
||||
};
|
||||
|
||||
// Write logging function with trace and whatnot for errors and necessary messages to log, log bot state in server to determine if user is at fault or if I am at fault (maybe message user if its their fault?)
|
||||
// initLog() returns nothing
|
||||
// Handles ensuring the required directory structure is created
|
||||
const initLog = (name: string): void => {
|
||||
// Initialize the file name
|
||||
startDate = new Date().toISOString().split("T")[0];
|
||||
logFolder = name;
|
||||
const startupMessage = `
|
||||
---------------------------------------------------------------------------------------------------
|
||||
---------------------------------------- LOGGING STARTED -----------------------------------------
|
||||
------------------------------------ ${new Date().toISOString()} -------------------------------------
|
||||
---------------------------------------------------------------------------------------------------`;
|
||||
|
||||
export default { split2k, cmdPrompt, sendIndirectMessage };
|
||||
// Make all required folders if they are missing
|
||||
const folders = ["combined", "traces"];
|
||||
Object.values(LogTypes).forEach(level => {
|
||||
folders.push(level)
|
||||
});
|
||||
|
||||
// Make each folder if its missing and insert the startup message
|
||||
folders.forEach(level => {
|
||||
Deno.mkdirSync(`./${logFolder}/${level}`, { recursive: true });
|
||||
Deno.writeTextFileSync(`./${logFolder}/${level}/${startDate}.log`, `${startupMessage}\n`, {append: true});
|
||||
});
|
||||
initialized = true;
|
||||
};
|
||||
|
||||
// log(level, message) returns nothing
|
||||
// Handles sending messages to console.log and sending a copy of the log to a file for review on crashes
|
||||
const log = async (level: LogTypes, message: string, error = new Error()): Promise<void> => {
|
||||
const msgId = await nanoid(10);
|
||||
const formattedMsg = `${new Date().toISOString()} | ${msgId} | ${level} | ${message}`;
|
||||
const traceMsg = `${error.stack}`
|
||||
// Default functionality of logging to console
|
||||
console[level](formattedMsg);
|
||||
// Logging to files for permanent info
|
||||
if (initialized) {
|
||||
await Deno.writeTextFile(`./${logFolder}/${level}/${startDate}.log`, `${formattedMsg}\n`, {append: true});
|
||||
await Deno.writeTextFile(`./${logFolder}/combined/${startDate}.log`, `${formattedMsg}\n`, {append: true});
|
||||
await Deno.writeTextFile(`./${logFolder}/traces/${startDate}.log`, `${formattedMsg}\n${traceMsg}\n\n`, {append: true});
|
||||
}
|
||||
};
|
||||
|
||||
export default { split2k, cmdPrompt, sendIndirectMessage, initLog, log };
|
||||
|
||||
Reference in New Issue
Block a user