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:
Ean Milligan (Bastion) 2021-03-14 23:27:53 -04:00
parent 0b5f44d18c
commit d449d1d85d
7 changed files with 233 additions and 160 deletions

232
mod.ts
View File

@ -23,6 +23,7 @@ import utils from "./src/utils.ts";
import solver from "./src/solver.ts"; import solver from "./src/solver.ts";
import { EmojiConf } from "./src/mod.d.ts"; import { EmojiConf } from "./src/mod.d.ts";
import { LogTypes as LT } from "./src/utils.enums.ts";
import { DEVMODE, DEBUG, LOCALMODE } from "./flags.ts"; import { DEVMODE, DEBUG, LOCALMODE } from "./flags.ts";
import config from "./config.ts"; import config from "./config.ts";
@ -37,13 +38,16 @@ const dbClient = await new Client().connect({
password: config.db.password password: config.db.password
}); });
// Initialize logging client with folder to use for logs, needs --allow-write set on Deno startup
utils.initLog("logs");
// Start up the Discord Bot // Start up the Discord Bot
startBot({ startBot({
token: LOCALMODE ? config.localtoken : config.token, token: LOCALMODE ? config.localtoken : config.token,
intents: [Intents.GUILD_MESSAGES, Intents.DIRECT_MESSAGES, Intents.GUILDS], intents: [Intents.GUILD_MESSAGES, Intents.DIRECT_MESSAGES, Intents.GUILDS],
eventHandlers: { eventHandlers: {
ready: () => { ready: () => {
console.log(`${config.name} Logged in!`); utils.log(LT.INFO, `${config.name} Logged in!`);
editBotsStatus(StatusTypes.Online, "Booting up . . .", ActivityType.Game); editBotsStatus(StatusTypes.Online, "Booting up . . .", ActivityType.Game);
// Interval to rotate the status text every 30 seconds to show off more commands // Interval to rotate the status text every 30 seconds to show off more commands
@ -51,34 +55,34 @@ startBot({
try { try {
// Wrapped in try-catch due to hard crash possible // Wrapped in try-catch due to hard crash possible
editBotsStatus(StatusTypes.Online, intervals.getRandomStatus(cache), ActivityType.Game); editBotsStatus(StatusTypes.Online, intervals.getRandomStatus(cache), ActivityType.Game);
} catch (err) { } catch (e) {
console.error("Failed to update status 00", err); utils.log(LT.ERROR, `Failed to update status: ${JSON.stringify(e)}`);
} }
}, 30000); }, 30000);
// Interval to update bot list stats every 24 hours // Interval to update bot list stats every 24 hours
LOCALMODE ? console.log("updateListStatistics not running") : setInterval(() => intervals.updateListStatistics(botID, cache.guilds.size), 86400000); LOCALMODE ? utils.log(LT.INFO, "updateListStatistics not running") : setInterval(() => intervals.updateListStatistics(botID, cache.guilds.size), 86400000);
// setTimeout added to make sure the startup message does not error out // setTimeout added to make sure the startup message does not error out
setTimeout(() => { setTimeout(() => {
LOCALMODE ? console.log("updateListStatistics not running") : intervals.updateListStatistics(botID, cache.guilds.size); LOCALMODE ? utils.log(LT.INFO, "updateListStatistics not running") : intervals.updateListStatistics(botID, cache.guilds.size);
editBotsStatus(StatusTypes.Online, `Boot Complete`, ActivityType.Game); editBotsStatus(StatusTypes.Online, `Boot Complete`, ActivityType.Game);
sendMessage(config.logChannel, `${config.name} has started, running version ${config.version}.`).catch(() => { sendMessage(config.logChannel, `${config.name} has started, running version ${config.version}.`).catch(e => {
console.error("Failed to send message 00"); utils.log(LT.ERROR, `Failed to send message: ${JSON.stringify(e)}`);
}); });
}, 1000); }, 1000);
}, },
guildCreate: (guild: Guild) => { guildCreate: (guild: Guild) => {
sendMessage(config.logChannel, `New guild joined: ${guild.name} (id: ${guild.id}). This guild has ${guild.memberCount} members!`).catch(() => { sendMessage(config.logChannel, `New guild joined: ${guild.name} (id: ${guild.id}). This guild has ${guild.memberCount} members!`).catch(e => {
console.error("Failed to send message 01"); utils.log(LT.ERROR, `Failed to send message: ${JSON.stringify(e)}`);
}); });
}, },
guildDelete: (guild: Guild) => { guildDelete: (guild: Guild) => {
sendMessage(config.logChannel, `I have been removed from: ${guild.name} (id: ${guild.id}).`).catch(() => { sendMessage(config.logChannel, `I have been removed from: ${guild.name} (id: ${guild.id}).`).catch(e => {
console.error("Failed to send message 02"); utils.log(LT.ERROR, `Failed to send message: ${JSON.stringify(e)}`);
}); });
}, },
debug: (DEVMODE ? console.error : () => { }), debug: dmsg => utils.log(LT.LOG, `${JSON.stringify(dmsg)}`),
messageCreate: async (message: Message) => { messageCreate: async (message: Message) => {
// Ignore all other bots // Ignore all other bots
if (message.author.bot) return; if (message.author.bot) return;
@ -96,16 +100,16 @@ startBot({
// Its a ping test, what else do you want. // Its a ping test, what else do you want.
if (command === "ping") { if (command === "ping") {
// Light telemetry to see how many times a command is being run // Light telemetry to see how many times a command is being run
dbClient.execute(`CALL INC_CNT("ping");`).catch(err => { dbClient.execute(`CALL INC_CNT("ping");`).catch(e => {
console.error("Failed to call procedure 00", err); utils.log(LT.ERROR, `Failed to call stored procedure INC_CNT: ${JSON.stringify(e)}`);
}); });
// Calculates ping between sending a message and editing it, giving a nice round-trip latency. // Calculates ping between sending a message and editing it, giving a nice round-trip latency.
try { try {
const m = await utils.sendIndirectMessage(message, "Ping?", sendMessage, sendDirectMessage); const m = await utils.sendIndirectMessage(message, "Ping?", sendMessage, sendDirectMessage);
m.edit(`Pong! Latency is ${m.timestamp - message.timestamp}ms.`); m.edit(`Pong! Latency is ${m.timestamp - message.timestamp}ms.`);
} catch (err) { } catch (e) {
console.error("Failed to send message 10", message, err); utils.log(LT.ERROR, `Failed to send message: ${JSON.stringify(message)} | ${JSON.stringify(e)}`);
} }
} }
@ -113,12 +117,12 @@ startBot({
// Displays a short message I wanted to include // Displays a short message I wanted to include
else if (command === "rip" || command === "memory") { else if (command === "rip" || command === "memory") {
// Light telemetry to see how many times a command is being run // Light telemetry to see how many times a command is being run
dbClient.execute(`CALL INC_CNT("rip");`).catch(err => { dbClient.execute(`CALL INC_CNT("rip");`).catch(e => {
console.error("Failed to call procedure 01", err); utils.log(LT.ERROR, `Failed to call stored procedure INC_CNT: ${JSON.stringify(e)}`);
}); });
utils.sendIndirectMessage(message, "The Artificer was built in memory of my Grandmother, Babka\nWith much love, Ean\n\nDecember 21, 2020", sendMessage, sendDirectMessage).catch(err => { utils.sendIndirectMessage(message, "The Artificer was built in memory of my Grandmother, Babka\nWith much love, Ean\n\nDecember 21, 2020", sendMessage, sendDirectMessage).catch(e => {
console.error("Failed to send message 11", message, err); utils.log(LT.ERROR, `Failed to send message: ${JSON.stringify(message)} | ${JSON.stringify(e)}`);
}); });
} }
@ -126,12 +130,12 @@ startBot({
// Help command specifically for the roll command // Help command specifically for the roll command
else if (command === "rollhelp" || command === "rh" || command === "hr" || command === "??" || command?.startsWith("xdy")) { else if (command === "rollhelp" || command === "rh" || command === "hr" || command === "??" || command?.startsWith("xdy")) {
// Light telemetry to see how many times a command is being run // Light telemetry to see how many times a command is being run
dbClient.execute(`CALL INC_CNT("rollhelp");`).catch(err => { dbClient.execute(`CALL INC_CNT("rollhelp");`).catch(e => {
console.error("Failed to call procedure 02", err); utils.log(LT.ERROR, `Failed to call stored procedure INC_CNT: ${JSON.stringify(e)}`);
}); });
utils.sendIndirectMessage(message, longStrs.rollhelp.join("\n"), sendMessage, sendDirectMessage).catch(err => { utils.sendIndirectMessage(message, longStrs.rollhelp.join("\n"), sendMessage, sendDirectMessage).catch(e => {
console.error("Failed to send message 21", message, err); utils.log(LT.ERROR, `Failed to send message: ${JSON.stringify(message)} | ${JSON.stringify(e)}`);
}); });
} }
@ -139,12 +143,12 @@ startBot({
// Help command, prints from help file // Help command, prints from help file
else if (command === "help" || command === "h" || command === "?") { else if (command === "help" || command === "h" || command === "?") {
// Light telemetry to see how many times a command is being run // Light telemetry to see how many times a command is being run
dbClient.execute(`CALL INC_CNT("help");`).catch(err => { dbClient.execute(`CALL INC_CNT("help");`).catch(e => {
console.error("Failed to call procedure 03", err); utils.log(LT.ERROR, `Failed to call stored procedure INC_CNT: ${JSON.stringify(e)}`);
}); });
utils.sendIndirectMessage(message, longStrs.help.join("\n"), sendMessage, sendDirectMessage).catch(err => { utils.sendIndirectMessage(message, longStrs.help.join("\n"), sendMessage, sendDirectMessage).catch(e => {
console.error("Failed to send message 20", message, err); utils.log(LT.ERROR, `Failed to send message: ${JSON.stringify(message)} | ${JSON.stringify(e)}`);
}); });
} }
@ -152,12 +156,12 @@ startBot({
// Info command, prints short desc on bot and some links // Info command, prints short desc on bot and some links
else if (command === "info" || command === "i") { else if (command === "info" || command === "i") {
// Light telemetry to see how many times a command is being run // Light telemetry to see how many times a command is being run
dbClient.execute(`CALL INC_CNT("info");`).catch(err => { dbClient.execute(`CALL INC_CNT("info");`).catch(e => {
console.error("Failed to call procedure 04", err); utils.log(LT.ERROR, `Failed to call stored procedure INC_CNT: ${JSON.stringify(e)}`);
}); });
utils.sendIndirectMessage(message, longStrs.info.join("\n"), sendMessage, sendDirectMessage).catch(err => { utils.sendIndirectMessage(message, longStrs.info.join("\n"), sendMessage, sendDirectMessage).catch(e => {
console.error("Failed to send message 22", message, err); utils.log(LT.ERROR, `Failed to send message: ${JSON.stringify(message)} | ${JSON.stringify(e)}`);
}); });
} }
@ -165,12 +169,12 @@ startBot({
// Privacy command, prints short desc on bot's privacy policy // Privacy command, prints short desc on bot's privacy policy
else if (command === "privacy") { else if (command === "privacy") {
// Light telemetry to see how many times a command is being run // Light telemetry to see how many times a command is being run
dbClient.execute(`CALL INC_CNT("privacy");`).catch(err => { dbClient.execute(`CALL INC_CNT("privacy");`).catch(e => {
console.error("Failed to call procedure 04", err); utils.log(LT.ERROR, `Failed to call stored procedure INC_CNT: ${JSON.stringify(e)}`);
}); });
utils.sendIndirectMessage(message, longStrs.privacy.join("\n"), sendMessage, sendDirectMessage).catch(err => { utils.sendIndirectMessage(message, longStrs.privacy.join("\n"), sendMessage, sendDirectMessage).catch(e => {
console.error("Failed to send message 2E", message, err); utils.log(LT.ERROR, `Failed to send message: ${JSON.stringify(message)} | ${JSON.stringify(e)}`);
}); });
} }
@ -178,12 +182,12 @@ startBot({
// Returns version of the bot // Returns version of the bot
else if (command === "version" || command === "v") { else if (command === "version" || command === "v") {
// Light telemetry to see how many times a command is being run // Light telemetry to see how many times a command is being run
dbClient.execute(`CALL INC_CNT("version");`).catch(err => { dbClient.execute(`CALL INC_CNT("version");`).catch(e => {
console.error("Failed to call procedure 05", err); utils.log(LT.ERROR, `Failed to call stored procedure INC_CNT: ${JSON.stringify(e)}`);
}); });
utils.sendIndirectMessage(message, `My current version is ${config.version}.`, sendMessage, sendDirectMessage).catch(err => { utils.sendIndirectMessage(message, `My current version is ${config.version}.`, sendMessage, sendDirectMessage).catch(e => {
console.error("Failed to send message 30", message, err); utils.log(LT.ERROR, `Failed to send message: ${JSON.stringify(message)} | ${JSON.stringify(e)}`);
}); });
} }
@ -191,15 +195,15 @@ startBot({
// Manually report a failed roll // Manually report a failed roll
else if (command === "report" || command === "r") { else if (command === "report" || command === "r") {
// Light telemetry to see how many times a command is being run // Light telemetry to see how many times a command is being run
dbClient.execute(`CALL INC_CNT("report");`).catch(err => { dbClient.execute(`CALL INC_CNT("report");`).catch(e => {
console.error("Failed to call procedure 06", err); utils.log(LT.ERROR, `Failed to call stored procedure INC_CNT: ${JSON.stringify(e)}`);
}); });
sendMessage(config.reportChannel, ("USER REPORT:\n" + args.join(" "))).catch(err => { sendMessage(config.reportChannel, ("USER REPORT:\n" + args.join(" "))).catch(e => {
console.error("Failed to send message 50", message, err); utils.log(LT.ERROR, `Failed to send message: ${JSON.stringify(message)} | ${JSON.stringify(e)}`);
}); });
utils.sendIndirectMessage(message, "Failed command has been reported to my developer.\n\nFor more in depth support, and information about planned maintenance, please join the support server here: https://discord.gg/peHASXMZYv", sendMessage, sendDirectMessage).catch(err => { utils.sendIndirectMessage(message, "Failed command has been reported to my developer.\n\nFor more in depth support, and information about planned maintenance, please join the support server here: https://discord.gg/peHASXMZYv", sendMessage, sendDirectMessage).catch(e => {
console.error("Failed to send message 51", message, err); utils.log(LT.ERROR, `Failed to send message: ${JSON.stringify(message)} | ${JSON.stringify(e)}`);
}); });
} }
@ -207,22 +211,22 @@ startBot({
// Displays stats on the bot // Displays stats on the bot
else if (command === "stats" || command === "s") { else if (command === "stats" || command === "s") {
// Light telemetry to see how many times a command is being run // Light telemetry to see how many times a command is being run
dbClient.execute(`CALL INC_CNT("stats");`).catch(err => { dbClient.execute(`CALL INC_CNT("stats");`).catch(e => {
console.error("Failed to call procedure 07", err); utils.log(LT.ERROR, `Failed to call stored procedure INC_CNT: ${JSON.stringify(e)}`);
}); });
// Calculate how many times commands have been run // Calculate how many times commands have been run
const rollQuery = await dbClient.query(`SELECT count FROM command_cnt WHERE command = "roll";`).catch(err => { const rollQuery = await dbClient.query(`SELECT count FROM command_cnt WHERE command = "roll";`).catch(e => {
console.error("Failed to query 17", err); utils.log(LT.ERROR, `Failed to query DB: ${JSON.stringify(e)}`);
}); });
const totalQuery = await dbClient.query(`SELECT SUM(count) as count FROM command_cnt;`).catch(err => { const totalQuery = await dbClient.query(`SELECT SUM(count) as count FROM command_cnt;`).catch(e => {
console.error("Failed to query 27", err); utils.log(LT.ERROR, `Failed to query DB: ${JSON.stringify(e)}`);
}); });
const rolls = BigInt(rollQuery[0].count); const rolls = BigInt(rollQuery[0].count);
const total = BigInt(totalQuery[0].count); const total = BigInt(totalQuery[0].count);
utils.sendIndirectMessage(message, `${config.name} is rolling dice for ${cache.members.size} active users, in ${cache.channels.size} channels of ${cache.guilds.size} servers.\n\nSo far, ${rolls} dice have been rolled and ${total - rolls} utility commands have been run.`, sendMessage, sendDirectMessage).catch(err => { utils.sendIndirectMessage(message, `${config.name} is rolling dice for ${cache.members.size} active users, in ${cache.channels.size} channels of ${cache.guilds.size} servers.\n\nSo far, ${rolls} dice have been rolled and ${total - rolls} utility commands have been run.`, sendMessage, sendDirectMessage).catch(e => {
console.error("Failed to send message 60", message, err); utils.log(LT.ERROR, `Failed to send message: ${JSON.stringify(message)} | ${JSON.stringify(e)}`);
}); });
} }
@ -230,8 +234,8 @@ startBot({
// API sub commands // API sub commands
else if (command === "api" && args.length > 0) { else if (command === "api" && args.length > 0) {
// Light telemetry to see how many times a command is being run // Light telemetry to see how many times a command is being run
dbClient.execute(`CALL INC_CNT("api");`).catch(err => { dbClient.execute(`CALL INC_CNT("api");`).catch(e => {
console.error("Failed to call procedure 0A", err); utils.log(LT.ERROR, `Failed to call stored procedure INC_CNT: ${JSON.stringify(e)}`);
}); });
// Local apiArg in lowercase // Local apiArg in lowercase
@ -239,8 +243,8 @@ startBot({
// Alert users who DM the bot that this command is for guilds only // Alert users who DM the bot that this command is for guilds only
if (message.guildID === "") { if (message.guildID === "") {
utils.sendIndirectMessage(message, `API commands are only available in guilds.`, sendMessage, sendDirectMessage).catch(err => { utils.sendIndirectMessage(message, `API commands are only available in guilds.`, sendMessage, sendDirectMessage).catch(e => {
console.error("Failed to send message 24", message, err); utils.log(LT.ERROR, `Failed to send message: ${JSON.stringify(message)} | ${JSON.stringify(e)}`);
}); });
return; return;
} }
@ -250,61 +254,61 @@ startBot({
// [[api help // [[api help
// Shows API help details // Shows API help details
if (apiArg === "help") { if (apiArg === "help") {
utils.sendIndirectMessage(message, longStrs.apihelp.join("\n"), sendMessage, sendDirectMessage).catch(err => { utils.sendIndirectMessage(message, longStrs.apihelp.join("\n"), sendMessage, sendDirectMessage).catch(e => {
console.error("Failed to send message 23", message, err); utils.log(LT.ERROR, `Failed to send message: ${JSON.stringify(message)} | ${JSON.stringify(e)}`);
}); });
} }
// [[api allow/block // [[api allow/block
// Lets a guild admin allow or ban API rolls from happening in said guild // Lets a guild admin allow or ban API rolls from happening in said guild
else if (apiArg === "allow" || apiArg === "block" || apiArg === "enable" || apiArg === "disable") { else if (apiArg === "allow" || apiArg === "block" || apiArg === "enable" || apiArg === "disable") {
const guildQuery = await dbClient.query(`SELECT guildid FROM allowed_guilds WHERE guildid = ?`, [message.guildID]).catch(err => { const guildQuery = await dbClient.query(`SELECT guildid FROM allowed_guilds WHERE guildid = ?`, [message.guildID]).catch(e0 => {
console.error("Failed to query 1A", err); utils.log(LT.ERROR, `Failed to query DB: ${JSON.stringify(e0)}`);
utils.sendIndirectMessage(message, `Failed to ${apiArg} API rolls for this guild. If this issue persists, please report this to the developers.`, sendMessage, sendDirectMessage).catch(err => { utils.sendIndirectMessage(message, `Failed to ${apiArg} API rolls for this guild. If this issue persists, please report this to the developers.`, sendMessage, sendDirectMessage).catch(e1 => {
console.error("Failed to send message 29", message, err); utils.log(LT.ERROR, `Failed to send message: ${JSON.stringify(message)} | ${JSON.stringify(e1)}`);
}); });
return; return;
}); });
if (guildQuery.length === 0) { if (guildQuery.length === 0) {
// Since guild is not in our DB, add it in // Since guild is not in our DB, add it in
await dbClient.execute(`INSERT INTO allowed_guilds(guildid,active) values(?,?)`, [BigInt(message.guildID), ((apiArg === "allow" || apiArg === "enable") ? 1 : 0)]).catch(err => { await dbClient.execute(`INSERT INTO allowed_guilds(guildid,active) values(?,?)`, [BigInt(message.guildID), ((apiArg === "allow" || apiArg === "enable") ? 1 : 0)]).catch(e0 => {
console.error("Failed to inersert 2A", err); utils.log(LT.ERROR, `Failed to insert into DB: ${JSON.stringify(e0)}`);
utils.sendIndirectMessage(message, `Failed to ${apiArg} API rolls for this guild. If this issue persists, please report this to the developers.`, sendMessage, sendDirectMessage).catch(err => { utils.sendIndirectMessage(message, `Failed to ${apiArg} API rolls for this guild. If this issue persists, please report this to the developers.`, sendMessage, sendDirectMessage).catch(e1 => {
console.error("Failed to send message 26", message, err); utils.log(LT.ERROR, `Failed to send message: ${JSON.stringify(message)} | ${JSON.stringify(e1)}`);
}); });
return; return;
}); });
} else { } else {
// Since guild is in our DB, update it // Since guild is in our DB, update it
await dbClient.execute(`UPDATE allowed_guilds SET active = ? WHERE guildid = ?`, [((apiArg === "allow" || apiArg === "enable") ? 1 : 0), BigInt(message.guildID)]).catch(err => { await dbClient.execute(`UPDATE allowed_guilds SET active = ? WHERE guildid = ?`, [((apiArg === "allow" || apiArg === "enable") ? 1 : 0), BigInt(message.guildID)]).catch(e0 => {
console.error("Failed to inersert 3A", err); utils.log(LT.ERROR, `Failed to update DB: ${JSON.stringify(e0)}`);
utils.sendIndirectMessage(message, `Failed to ${apiArg} API rolls for this guild. If this issue persists, please report this to the developers.`, sendMessage, sendDirectMessage).catch(err => { utils.sendIndirectMessage(message, `Failed to ${apiArg} API rolls for this guild. If this issue persists, please report this to the developers.`, sendMessage, sendDirectMessage).catch(e1 => {
console.error("Failed to send message 28", message, err); utils.log(LT.ERROR, `Failed to send message: ${JSON.stringify(message)} | ${JSON.stringify(e1)}`);
}); });
return; return;
}); });
} }
// We won't get here if there's any errors, so we know it has bee successful, so report as such // We won't get here if there's any errors, so we know it has bee successful, so report as such
utils.sendIndirectMessage(message, `API rolls have successfully been ${apiArg}ed for this guild.`, sendMessage, sendDirectMessage).catch(err => { utils.sendIndirectMessage(message, `API rolls have successfully been ${apiArg}ed for this guild.`, sendMessage, sendDirectMessage).catch(e => {
console.error("Failed to send message 27", message, err); utils.log(LT.ERROR, `Failed to send message: ${JSON.stringify(message)} | ${JSON.stringify(e)}`);
}); });
} }
// [[api delete // [[api delete
// Lets a guild admin delete their server from the database // Lets a guild admin delete their server from the database
else if (apiArg === "delete") { else if (apiArg === "delete") {
await dbClient.execute(`DELETE FROM allowed_guilds WHERE guildid = ?`, [message.guildID]).catch(err => { await dbClient.execute(`DELETE FROM allowed_guilds WHERE guildid = ?`, [message.guildID]).catch(e0 => {
console.error("Failed to query 1B", err); utils.log(LT.ERROR, `Failed to query DB: ${JSON.stringify(e0)}`);
utils.sendIndirectMessage(message, `Failed to delete this guild from the database. If this issue persists, please report this to the developers.`, sendMessage, sendDirectMessage).catch(err => { utils.sendIndirectMessage(message, `Failed to delete this guild from the database. If this issue persists, please report this to the developers.`, sendMessage, sendDirectMessage).catch(e1 => {
console.error("Failed to send message 2F", message, err); utils.log(LT.ERROR, `Failed to send message: ${JSON.stringify(message)} | ${JSON.stringify(e1)}`);
}); });
return; return;
}); });
// We won't get here if there's any errors, so we know it has bee successful, so report as such // We won't get here if there's any errors, so we know it has bee successful, so report as such
utils.sendIndirectMessage(message, `This guild's API setting has been removed from The Artifier's Database.`, sendMessage, sendDirectMessage).catch(err => { utils.sendIndirectMessage(message, `This guild's API setting has been removed from The Artifier's Database.`, sendMessage, sendDirectMessage).catch(e => {
console.error("Failed to send message 2G", message, err); utils.log(LT.ERROR, `Failed to send message: ${JSON.stringify(message)} | ${JSON.stringify(e)}`);
}); });
} }
@ -312,10 +316,10 @@ startBot({
// Lets a guild admin check the status of API rolling in said guild // Lets a guild admin check the status of API rolling in said guild
else if (apiArg === "status") { else if (apiArg === "status") {
// Get status of guild from the db // Get status of guild from the db
const guildQuery = await dbClient.query(`SELECT active, banned FROM allowed_guilds WHERE guildid = ?`, [message.guildID]).catch(err => { const guildQuery = await dbClient.query(`SELECT active, banned FROM allowed_guilds WHERE guildid = ?`, [message.guildID]).catch(e0 => {
console.error("Failed to query 1A", err); utils.log(LT.ERROR, `Failed to query DB: ${JSON.stringify(e0)}`);
utils.sendIndirectMessage(message, `Failed to check API rolls status for this guild. If this issue persists, please report this to the developers.`, sendMessage, sendDirectMessage).catch(err => { utils.sendIndirectMessage(message, `Failed to check API rolls status for this guild. If this issue persists, please report this to the developers.`, sendMessage, sendDirectMessage).catch(e1 => {
console.error("Failed to send message 2A", message, err); utils.log(LT.ERROR, `Failed to send message: ${JSON.stringify(message)} | ${JSON.stringify(e1)}`);
}); });
return; return;
}); });
@ -324,24 +328,24 @@ startBot({
if (guildQuery.length > 0) { if (guildQuery.length > 0) {
// Check if guild is banned from using API and return appropriate message // Check if guild is banned from using API and return appropriate message
if (guildQuery[0].banned) { if (guildQuery[0].banned) {
utils.sendIndirectMessage(message, `The Artificer's API is ${config.api.enable ? "currently enabled" : "currently disabled"}.\n\nAPI rolls are banned from being used in this guild. This will not be reversed.`, sendMessage, sendDirectMessage).catch(err => { utils.sendIndirectMessage(message, `The Artificer's API is ${config.api.enable ? "currently enabled" : "currently disabled"}.\n\nAPI rolls are banned from being used in this guild. This will not be reversed.`, sendMessage, sendDirectMessage).catch(e => {
console.error("Failed to send message 2B", message, err); utils.log(LT.ERROR, `Failed to send message: ${JSON.stringify(message)} | ${JSON.stringify(e)}`);
}); });
} else { } else {
utils.sendIndirectMessage(message, `The Artificer's API is ${config.api.enable ? "currently enabled" : "currently disabled"}.\n\nAPI rolls are ${guildQuery[0].active ? "allowed" : "blocked from being used"} in this guild.`, sendMessage, sendDirectMessage).catch(err => { utils.sendIndirectMessage(message, `The Artificer's API is ${config.api.enable ? "currently enabled" : "currently disabled"}.\n\nAPI rolls are ${guildQuery[0].active ? "allowed" : "blocked from being used"} in this guild.`, sendMessage, sendDirectMessage).catch(e => {
console.error("Failed to send message 2C", message, err); utils.log(LT.ERROR, `Failed to send message: ${JSON.stringify(message)} | ${JSON.stringify(e)}`);
}); });
} }
} else { } else {
// Guild is not in DB, therefore they are blocked // Guild is not in DB, therefore they are blocked
utils.sendIndirectMessage(message, `The Artificer's API is ${config.api.enable ? "currently enabled" : "currently disabled"}.\n\nAPI rolls are blocked from being used in this guild.`, sendMessage, sendDirectMessage).catch(err => { utils.sendIndirectMessage(message, `The Artificer's API is ${config.api.enable ? "currently enabled" : "currently disabled"}.\n\nAPI rolls are blocked from being used in this guild.`, sendMessage, sendDirectMessage).catch(e => {
console.error("Failed to send message 2D", message, err); utils.log(LT.ERROR, `Failed to send message: ${JSON.stringify(message)} | ${JSON.stringify(e)}`);
}); });
} }
} }
} else { } else {
utils.sendIndirectMessage(message, `API commands are powerful and can only be used by guild Owners and Admins.\n\nFor information on how to use the API, please check the GitHub README for more information: <https://github.com/Burn-E99/TheArtificer>`, sendMessage, sendDirectMessage).catch(err => { utils.sendIndirectMessage(message, `API commands are powerful and can only be used by guild Owners and Admins.\n\nFor information on how to use the API, please check the GitHub README for more information: <https://github.com/Burn-E99/TheArtificer>`, sendMessage, sendDirectMessage).catch(e => {
console.error("Failed to send message 25", message, err); utils.log(LT.ERROR, `Failed to send message: ${JSON.stringify(message)} | ${JSON.stringify(e)}`);
}); });
} }
} }
@ -350,14 +354,14 @@ startBot({
// Dice rolling commence! // Dice rolling commence!
else if ((command + args.join("")).indexOf(config.postfix) > -1) { else if ((command + args.join("")).indexOf(config.postfix) > -1) {
// Light telemetry to see how many times a command is being run // Light telemetry to see how many times a command is being run
dbClient.execute(`CALL INC_CNT("roll");`).catch(err => { dbClient.execute(`CALL INC_CNT("roll");`).catch(e => {
console.error("Failed to call procedure 08", err); utils.log(LT.ERROR, `Failed to call stored procedure INC_CNT: ${JSON.stringify(e)}`);
}); });
// If DEVMODE is on, only allow this command to be used in the devServer // If DEVMODE is on, only allow this command to be used in the devServer
if (DEVMODE && message.guildID !== config.devServer) { if (DEVMODE && message.guildID !== config.devServer) {
utils.sendIndirectMessage(message, "Command is in development, please try again later.", sendMessage, sendDirectMessage).catch(err => { utils.sendIndirectMessage(message, "Command is in development, please try again later.", sendMessage, sendDirectMessage).catch(e => {
console.error("Failed to send message 70", message, err); utils.log(LT.ERROR, `Failed to send message: ${JSON.stringify(message)} | ${JSON.stringify(e)}`);
}); });
return; return;
} }
@ -421,7 +425,7 @@ startBot({
if (DEVMODE && config.logRolls) { if (DEVMODE && config.logRolls) {
// If enabled, log rolls so we can verify the bots math // If enabled, log rolls so we can verify the bots math
dbClient.execute("INSERT INTO roll_log(input,result,resultid,api,error) values(?,?,?,0,1)", [originalCommand, "NoGMsFound", m.id]).catch(e => { dbClient.execute("INSERT INTO roll_log(input,result,resultid,api,error) values(?,?,?,0,1)", [originalCommand, "NoGMsFound", m.id]).catch(e => {
console.log("Failed to insert into database 00", e); utils.log(LT.ERROR, `Failed to insert into DB: ${JSON.stringify(e)}`);
}); });
} }
return; return;
@ -440,7 +444,7 @@ startBot({
if (DEVMODE && config.logRolls) { if (DEVMODE && config.logRolls) {
// If enabled, log rolls so we can verify the bots math // If enabled, log rolls so we can verify the bots math
dbClient.execute("INSERT INTO roll_log(input,result,resultid,api,error) values(?,?,?,0,1)", [originalCommand, "NoOrderFound", m.id]).catch(e => { dbClient.execute("INSERT INTO roll_log(input,result,resultid,api,error) values(?,?,?,0,1)", [originalCommand, "NoOrderFound", m.id]).catch(e => {
console.log("Failed to insert into database 05", e); utils.log(LT.ERROR, `Failed to insert into DB: ${JSON.stringify(e)}`);
}); });
} }
return; return;
@ -463,7 +467,7 @@ startBot({
if (DEVMODE && config.logRolls) { if (DEVMODE && config.logRolls) {
// If enabled, log rolls so we can verify the bots math // If enabled, log rolls so we can verify the bots math
dbClient.execute("INSERT INTO roll_log(input,result,resultid,api,error) values(?,?,?,0,1)", [originalCommand, "MaxAndNominal", m.id]).catch(e => { dbClient.execute("INSERT INTO roll_log(input,result,resultid,api,error) values(?,?,?,0,1)", [originalCommand, "MaxAndNominal", m.id]).catch(e => {
console.log("Failed to insert into database 01", e); utils.log(LT.ERROR, `Failed to insert into DB: ${JSON.stringify(e)}`);
}); });
} }
return; return;
@ -483,7 +487,7 @@ startBot({
if (DEVMODE && config.logRolls) { if (DEVMODE && config.logRolls) {
// If enabled, log rolls so we can verify the bots math // If enabled, log rolls so we can verify the bots math
dbClient.execute("INSERT INTO roll_log(input,result,resultid,api,error) values(?,?,?,0,1)", [originalCommand, returnmsg.errorCode, m.id]).catch(e => { dbClient.execute("INSERT INTO roll_log(input,result,resultid,api,error) values(?,?,?,0,1)", [originalCommand, returnmsg.errorCode, m.id]).catch(e => {
console.log("Failed to insert into database 02", e); utils.log(LT.ERROR, `Failed to insert into DB: ${JSON.stringify(e)}`);
}); });
} }
return; return;
@ -523,7 +527,7 @@ startBot({
if (DEVMODE && config.logRolls) { if (DEVMODE && config.logRolls) {
// If enabled, log rolls so we can verify the bots math // If enabled, log rolls so we can verify the bots math
dbClient.execute("INSERT INTO roll_log(input,result,resultid,api,error) values(?,?,?,0,0)", [originalCommand, returnText, m.id]).catch(e => { dbClient.execute("INSERT INTO roll_log(input,result,resultid,api,error) values(?,?,?,0,0)", [originalCommand, returnText, m.id]).catch(e => {
console.log("Failed to insert into database 03", e); utils.log(LT.ERROR, `Failed to insert into DB: ${JSON.stringify(e)}`);
}); });
} }
} else { } else {
@ -547,12 +551,12 @@ startBot({
if (DEVMODE && config.logRolls) { if (DEVMODE && config.logRolls) {
// If enabled, log rolls so we can verify the bots math // If enabled, log rolls so we can verify the bots math
dbClient.execute("INSERT INTO roll_log(input,result,resultid,api,error) values(?,?,?,0,0)", [originalCommand, returnText, m.id]).catch(e => { dbClient.execute("INSERT INTO roll_log(input,result,resultid,api,error) values(?,?,?,0,0)", [originalCommand, returnText, m.id]).catch(e => {
console.log("Failed to insert into database 04", e); utils.log(LT.ERROR, `Failed to insert into DB: ${JSON.stringify(e)}`);
}); });
} }
} }
} catch (err) { } catch (e) {
console.error("Something failed 71"); utils.log(LT.ERROR, `Undandled Error: ${JSON.stringify(e)}`);
} }
} }
@ -560,22 +564,22 @@ startBot({
// Check if the unhandled command is an emoji request // Check if the unhandled command is an emoji request
else { else {
// Start looping thru the possible emojis // Start looping thru the possible emojis
config.emojis.some((e: EmojiConf) => { config.emojis.some((emoji: EmojiConf) => {
// If a match gets found // If a match gets found
if (e.aliases.indexOf(command || "") > -1) { if (emoji.aliases.indexOf(command || "") > -1) {
// Light telemetry to see how many times a command is being run // Light telemetry to see how many times a command is being run
dbClient.execute(`CALL INC_CNT("emoji");`).catch(err => { dbClient.execute(`CALL INC_CNT("emoji");`).catch(e => {
console.error("Failed to call procedure 09", err); utils.log(LT.ERROR, `Failed to call stored procedure INC_CNT: ${JSON.stringify(e)}`);
}); });
// Send the needed emoji // Send the needed emoji1
utils.sendIndirectMessage(message, `<${e.animated ? "a" : ""}:${e.name}:${e.id}>`, sendMessage, sendDirectMessage).catch(err => { utils.sendIndirectMessage(message, `<${emoji.animated ? "a" : ""}:${emoji.name}:${emoji.id}>`, sendMessage, sendDirectMessage).catch(e => {
console.error("Failed to send message 40", message, err); utils.log(LT.ERROR, `Failed to send message: ${JSON.stringify(message)} | ${JSON.stringify(e)}`);
}); });
// And attempt to delete if needed // And attempt to delete if needed
if (e.deleteSender) { if (emoji.deleteSender) {
message.delete().catch(err => { message.delete().catch(e => {
console.error("Failed to delete message 41", message, err); utils.log(LT.WARN, `Failed to delete message: ${JSON.stringify(message)} | ${JSON.stringify(e)}`);
}); });
} }
return true; return true;

View File

@ -20,6 +20,8 @@ import {
} from "../deps.ts"; } from "../deps.ts";
import solver from "./solver.ts"; import solver from "./solver.ts";
import { LogTypes as LT } from "./utils.enums.ts";
import utils from "./utils.ts";
import config from "../config.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 // 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 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 }); 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 // rateLimitTime holds all users with the last time they started a rate limit timer
const rateLimitTime = new Map<string, number>(); const rateLimitTime = new Map<string, number>();
@ -108,8 +110,8 @@ const start = async (dbClient: Client, cache: CacheData, sendMessage: (c: string
let erroredOut = false; let erroredOut = false;
// Insert new key/user pair into the db // Insert new key/user pair into the db
await dbClient.execute("INSERT INTO all_keys(userid,apiKey) values(?,?)", [apiUserid, newKey]).catch(() => { await dbClient.execute("INSERT INTO all_keys(userid,apiKey) values(?,?)", [apiUserid, newKey]).catch(e => {
console.log("Failed to insert into database 20"); utils.log(LT.ERROR, `Failed to insert into database: ${JSON.stringify(e)}`);
request.respond({ status: Status.InternalServerError, body: STATUS_TEXT.get(Status.InternalServerError) }); request.respond({ status: Status.InternalServerError, body: STATUS_TEXT.get(Status.InternalServerError) });
erroredOut = true; erroredOut = true;
}); });
@ -139,8 +141,8 @@ const start = async (dbClient: Client, cache: CacheData, sendMessage: (c: string
let erroredOut = false; let erroredOut = false;
// Get all channels userid has authorized // Get all channels userid has authorized
const dbAllowedChannelQuery = await dbClient.query("SELECT * FROM allowed_channels WHERE userid = ?", [apiUserid]).catch(() => { const dbAllowedChannelQuery = await dbClient.query("SELECT * FROM allowed_channels WHERE userid = ?", [apiUserid]).catch(e => {
console.log("Failed to query database 22"); utils.log(LT.ERROR, `Failed to insert into database: ${JSON.stringify(e)}`);
request.respond({ status: Status.InternalServerError, body: STATUS_TEXT.get(Status.InternalServerError) }); request.respond({ status: Status.InternalServerError, body: STATUS_TEXT.get(Status.InternalServerError) });
erroredOut = true; 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) }); request.respond({ status: Status.BadRequest, body: STATUS_TEXT.get(Status.BadRequest) });
// Always log API rolls for abuse detection // 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(() => { dbClient.execute("INSERT INTO roll_log(input,result,resultid,api,error) values(?,?,?,1,1)", [originalCommand, "EmptyInput", null]).catch(e => {
console.log("Failed to insert into database 10"); utils.log(LT.ERROR, `Failed to insert into database: ${JSON.stringify(e)}`);
}); });
break; 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) }); request.respond({ status: Status.BadRequest, body: STATUS_TEXT.get(Status.BadRequest) });
// Always log API rolls for abuse detection // 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(() => { dbClient.execute("INSERT INTO roll_log(input,result,resultid,api,error) values(?,?,?,1,1)", [originalCommand, "BadOrder", null]).catch(e => {
console.log("Failed to insert into database 10"); utils.log(LT.ERROR, `Failed to insert into database: ${JSON.stringify(e)}`);
}); });
break; break;
} }
@ -238,8 +240,8 @@ const start = async (dbClient: Client, cache: CacheData, sendMessage: (c: string
request.respond({ status: Status.InternalServerError, body: returnmsg.errorMsg }); request.respond({ status: Status.InternalServerError, body: returnmsg.errorMsg });
// Always log API rolls for abuse detection // 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(() => { dbClient.execute("INSERT INTO roll_log(input,result,resultid,api,error) values(?,?,?,1,1)", [originalCommand, returnmsg.errorCode, null]).catch(e => {
console.log("Failed to insert into database 11"); utils.log(LT.ERROR, `Failed to insert into database: ${JSON.stringify(e)}`);
}); });
break; break;
} else { } 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) }); request.respond({ status: Status.BadRequest, body: STATUS_TEXT.get(Status.BadRequest) });
// Always log API rolls for abuse detection // 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(() => { dbClient.execute("INSERT INTO roll_log(input,result,resultid,api,error) values(?,?,?,1,1)", [originalCommand, "NoGMsSent", null]).catch(e => {
console.log("Failed to insert into database 12"); utils.log(LT.ERROR, `Failed to insert into database: ${JSON.stringify(e)}`);
}); });
break; 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 // Send the return message as a DM or normal message depening on if the channel is set
if ((query.get("channel") || "").length > 0) { if ((query.get("channel") || "").length > 0) {
m = await sendMessage(query.get("channel") || "", failedSend).catch(() => { 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; errorOut = true;
}); });
} else { } else {
m = await sendDirectMessage(query.get("user") || "", failedSend).catch(() => { 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; errorOut = true;
}); });
} }
@ -320,8 +322,8 @@ const start = async (dbClient: Client, cache: CacheData, sendMessage: (c: string
}); });
// Always log API rolls for abuse detection // 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(() => { dbClient.execute("INSERT INTO roll_log(input,result,resultid,api,error) values(?,?,?,1,0)", [originalCommand, returnText, ((typeof m === "object") ? m.id : null)]).catch(e => {
console.log("Failed to insert into database 13"); utils.log(LT.ERROR, `Failed to insert into database: ${JSON.stringify(e)}`);
}); });
// Handle closing the request out // 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 // 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(() => { dbClient.execute("INSERT INTO roll_log(input,result,resultid,api,error) values(?,?,?,1,0)", [originalCommand, returnText, ((typeof m === "object") ? m.id : null)]).catch(e => {
console.log("Failed to insert into database 14"); utils.log(LT.ERROR, `Failed to insert into database: ${JSON.stringify(e)}`);
}); });
// Handle closing the request out // Handle closing the request out
@ -376,7 +378,7 @@ const start = async (dbClient: Client, cache: CacheData, sendMessage: (c: string
} }
} catch (err) { } catch (err) {
// Handle any errors we missed // 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) }); request.respond({ status: Status.InternalServerError, body: STATUS_TEXT.get(Status.InternalServerError) });
} }
} else { } else {
@ -404,8 +406,8 @@ const start = async (dbClient: Client, cache: CacheData, sendMessage: (c: string
let erroredOut = false; let erroredOut = false;
// Insert new user/channel pair into the db // Insert new user/channel pair into the db
await dbClient.execute("INSERT INTO allowed_channels(userid,channelid) values(?,?)", [apiUserid, BigInt(query.get("channel"))]).catch(() => { await dbClient.execute("INSERT INTO allowed_channels(userid,channelid) values(?,?)", [apiUserid, BigInt(query.get("channel"))]).catch(e => {
console.log("Failed to insert into database 21"); utils.log(LT.ERROR, `Failed to insert into database: ${JSON.stringify(e)}`);
request.respond({ status: Status.InternalServerError, body: STATUS_TEXT.get(Status.InternalServerError) }); request.respond({ status: Status.InternalServerError, body: STATUS_TEXT.get(Status.InternalServerError) });
erroredOut = true; erroredOut = true;
}); });
@ -463,8 +465,8 @@ const start = async (dbClient: Client, cache: CacheData, sendMessage: (c: string
} }
// Execute the DB modification // Execute the DB modification
await dbClient.execute("UPDATE all_keys SET ?? = ? WHERE userid = ?", [key, value, apiUserid]).catch(() => { await dbClient.execute("UPDATE all_keys SET ?? = ? WHERE userid = ?", [key, value, apiUserid]).catch(e => {
console.log("Failed to update database 28"); utils.log(LT.ERROR, `Failed to insert into database: ${JSON.stringify(e)}`);
request.respond({ status: Status.InternalServerError, body: STATUS_TEXT.get(Status.InternalServerError) }); request.respond({ status: Status.InternalServerError, body: STATUS_TEXT.get(Status.InternalServerError) });
erroredOut = true; erroredOut = true;
}); });
@ -503,8 +505,8 @@ const start = async (dbClient: Client, cache: CacheData, sendMessage: (c: string
} }
// Execute the DB modification // Execute the DB modification
await dbClient.execute("UPDATE allowed_channels SET banned = ? WHERE userid = ? AND channelid = ?", [value, apiUserid, BigInt(query.get("channel"))]).catch(() => { await dbClient.execute("UPDATE allowed_channels SET banned = ? WHERE userid = ? AND channelid = ?", [value, apiUserid, BigInt(query.get("channel"))]).catch(e => {
console.log("Failed to update database 24"); utils.log(LT.ERROR, `Failed to insert into database: ${JSON.stringify(e)}`);
request.respond({ status: Status.InternalServerError, body: STATUS_TEXT.get(Status.InternalServerError) }); request.respond({ status: Status.InternalServerError, body: STATUS_TEXT.get(Status.InternalServerError) });
erroredOut = true; erroredOut = true;
}); });
@ -543,8 +545,8 @@ const start = async (dbClient: Client, cache: CacheData, sendMessage: (c: string
} }
// Update the requested entry // Update the requested entry
await dbClient.execute("UPDATE allowed_channels SET active = ? WHERE userid = ? AND channelid = ?", [value, apiUserid, BigInt(query.get("channel"))]).catch(() => { await dbClient.execute("UPDATE allowed_channels SET active = ? WHERE userid = ? AND channelid = ?", [value, apiUserid, BigInt(query.get("channel"))]).catch(e => {
console.log("Failed to update database 26"); utils.log(LT.ERROR, `Failed to insert into database: ${JSON.stringify(e)}`);
request.respond({ status: Status.InternalServerError, body: STATUS_TEXT.get(Status.InternalServerError) }); request.respond({ status: Status.InternalServerError, body: STATUS_TEXT.get(Status.InternalServerError) });
erroredOut = true; 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 // User has recieved their delete code and we need to delete the account now
let erroredOut = false; let erroredOut = false;
await dbClient.execute("DELETE FROM allowed_channels WHERE userid = ?", [apiUserid]).catch(() => { await dbClient.execute("DELETE FROM allowed_channels WHERE userid = ?", [apiUserid]).catch(e => {
console.log("Failed to delete from database 2A"); utils.log(LT.ERROR, `Failed to insert into database: ${JSON.stringify(e)}`);
request.respond({ status: Status.InternalServerError, body: STATUS_TEXT.get(Status.InternalServerError) }); request.respond({ status: Status.InternalServerError, body: STATUS_TEXT.get(Status.InternalServerError) });
erroredOut = true; erroredOut = true;
}); });
@ -592,8 +594,8 @@ const start = async (dbClient: Client, cache: CacheData, sendMessage: (c: string
break; break;
} }
await dbClient.execute("DELETE FROM all_keys WHERE userid = ?", [apiUserid]).catch(() => { await dbClient.execute("DELETE FROM all_keys WHERE userid = ?", [apiUserid]).catch(e => {
console.log("Failed to delete from database 2B"); utils.log(LT.ERROR, `Failed to insert into database: ${JSON.stringify(e)}`);
request.respond({ status: Status.InternalServerError, body: STATUS_TEXT.get(Status.InternalServerError) }); request.respond({ status: Status.InternalServerError, body: STATUS_TEXT.get(Status.InternalServerError) });
erroredOut = true; erroredOut = true;
}); });
@ -615,8 +617,8 @@ const start = async (dbClient: Client, cache: CacheData, sendMessage: (c: string
let erroredOut = false; let erroredOut = false;
// Execute the DB modification // Execute the DB modification
await dbClient.execute("UPDATE all_keys SET deleteCode = ? WHERE userid = ?", [deleteCode, apiUserid]).catch(() => { await dbClient.execute("UPDATE all_keys SET deleteCode = ? WHERE userid = ?", [deleteCode, apiUserid]).catch(e => {
console.log("Failed to update database 29"); utils.log(LT.ERROR, `Failed to insert into database: ${JSON.stringify(e)}`);
request.respond({ status: Status.InternalServerError, body: STATUS_TEXT.get(Status.InternalServerError) }); request.respond({ status: Status.InternalServerError, body: STATUS_TEXT.get(Status.InternalServerError) });
erroredOut = true; erroredOut = true;
}); });
@ -689,8 +691,8 @@ const start = async (dbClient: Client, cache: CacheData, sendMessage: (c: string
let erroredOut = false; let erroredOut = false;
// Insert new key/user pair into the db // 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(() => { await dbClient.execute("INSERT INTO all_keys(userid,apiKey,email) values(?,?,?)", [BigInt(query.get("user")), newKey, (query.get("email") || "").toLowerCase()]).catch(e => {
console.log("Failed to insert into database 20"); utils.log(LT.ERROR, `Failed to insert into database: ${JSON.stringify(e)}`);
request.respond({ status: Status.InternalServerError, body: STATUS_TEXT.get(Status.InternalServerError) }); request.respond({ status: Status.InternalServerError, body: STATUS_TEXT.get(Status.InternalServerError) });
erroredOut = true; erroredOut = true;
}); });

View File

@ -8,6 +8,8 @@ import {
// Discordeno deps // Discordeno deps
CacheData CacheData
} from "../deps.ts"; } from "../deps.ts";
import { LogTypes as LT } from "./utils.enums.ts";
import utils from "./utils.ts";
import config from "../config.ts"; import config from "../config.ts";
@ -47,7 +49,7 @@ const updateListStatistics = (botID: string, serverCount: number): void => {
"headers": tempHeaders, "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 "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
View File

@ -2,9 +2,9 @@
// EmojiConf is used as a structure for the emojis stored in config.ts // EmojiConf is used as a structure for the emojis stored in config.ts
export type EmojiConf = { export type EmojiConf = {
"name": string, name: string,
"aliases": Array<string>, aliases: Array<string>,
"id": string, id: string,
"animated": boolean, animated: boolean,
"deleteSender": boolean deleteSender: boolean
}; };

View File

@ -5,6 +5,8 @@
*/ */
import { RollSet, SolvedStep, SolvedRoll, ReturnData } from "./solver.d.ts"; 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 // 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 // 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"; errorMsg = "Error: Roll became undefined, one or more operands are not a roll or a number, check input";
break; break;
default: 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"; errorMsg = "Unhandled Error: " + solverError.message + "\nCheck input and try again, if issue persists, please use `[[report` to alert the devs of the issue";
break; break;
} }

13
src/utils.enums.ts Normal file
View 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"
}

View File

@ -6,9 +6,19 @@
import { import {
// Discordeno deps // Discordeno deps
Message, MessageContent Message, MessageContent,
// nanoid deps
nanoid
} from "../deps.ts"; } 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(longMessage) returns shortMessage[]
// split2k takes a long string in and cuts it into shorter strings to be sent in Discord // split2k takes a long string in and cuts it into shorter strings to be sent in Discord
const split2k = (chunk: string): string[] => { 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 };