From e33988d103cc60aa53baaf55c140a6847e523f98 Mon Sep 17 00:00:00 2001 From: Parker2991 Date: Thu, 16 Oct 2025 10:43:12 -0500 Subject: [PATCH] reverted back to how i handled errored before the custom errors and moved tellraw check to chat.js where the bot.tellraw function is called --- src/commandUtil/CommandArguments.js | 2 +- src/commandUtil/{errors => }/CommandError.js | 0 src/commandUtil/errors/UnknownCommand.js | 17 ---- src/commands/admin/kick.js | 2 +- src/commands/console/console.js | 2 +- src/commands/public/info.js | 2 +- src/commands/trusted/cloop.js | 2 +- src/data/translations.json | 9 +-- src/modules/chat.js | 14 +++- src/modules/commandHandler.js | 10 +-- src/modules/commandManager.js | 38 ++++++--- src/modules/errors.js | 84 -------------------- src/util/json.js | 34 -------- 13 files changed, 49 insertions(+), 167 deletions(-) rename src/commandUtil/{errors => }/CommandError.js (100%) delete mode 100644 src/commandUtil/errors/UnknownCommand.js delete mode 100644 src/modules/errors.js delete mode 100644 src/util/json.js diff --git a/src/commandUtil/CommandArguments.js b/src/commandUtil/CommandArguments.js index 9602c36..ea3e76c 100644 --- a/src/commandUtil/CommandArguments.js +++ b/src/commandUtil/CommandArguments.js @@ -1,4 +1,4 @@ -const CommandError = require('./errors/CommandError'); +const CommandError = require('./CommandError'); const translations = require('../data/translations.json'); class CommandArguments { diff --git a/src/commandUtil/errors/CommandError.js b/src/commandUtil/CommandError.js similarity index 100% rename from src/commandUtil/errors/CommandError.js rename to src/commandUtil/CommandError.js diff --git a/src/commandUtil/errors/UnknownCommand.js b/src/commandUtil/errors/UnknownCommand.js deleted file mode 100644 index c361e4b..0000000 --- a/src/commandUtil/errors/UnknownCommand.js +++ /dev/null @@ -1,17 +0,0 @@ -class UnknownCommand extends Error { - constructor(commandName) { - super(commandName); - this.name = "UnknownCommand"; - this._message = { - translate: "%s%s%s %s", - color: "red", - with: [ - { translate: "command.unknown.command", color: "red" }, - { text: "\n"}, - { text: `${commandName}`, color: "gray" }, - { translate: "command.context.here", color: "red" } - ] - } - } -} -module.exports = UnknownCommand; diff --git a/src/commands/admin/kick.js b/src/commands/admin/kick.js index 82a9a2d..a3860e5 100644 --- a/src/commands/admin/kick.js +++ b/src/commands/admin/kick.js @@ -1,4 +1,4 @@ -const CommandError = require("../../commandUtil/errors/CommandError"); +const CommandError = require("../../commandUtil/CommandError"); const CommandContext = require("../../commandUtil/CommandContext"); const trustLevel = require("../../commandUtil/CommandTrustLevel"); diff --git a/src/commands/console/console.js b/src/commands/console/console.js index 61b91a0..d7344f2 100644 --- a/src/commands/console/console.js +++ b/src/commands/console/console.js @@ -1,4 +1,4 @@ -const CommandError = require("../../commandUtil/errors/CommandError"); +const CommandError = require("../../commandUtil/CommandError"); const CommandContext = require("../../commandUtil/CommandContext"); const trustLevel = require("../../commandUtil/CommandTrustLevel"); diff --git a/src/commands/public/info.js b/src/commands/public/info.js index 2041dbb..81e3729 100644 --- a/src/commands/public/info.js +++ b/src/commands/public/info.js @@ -1,5 +1,5 @@ const os = require("os"); -const CommandError = require("../../commandUtil/errors/CommandError"); +const CommandError = require("../../commandUtil/CommandError"); const botInfo = require("../../data/info.json"); const { execSync } = require("child_process"); const CommandContext = require("../../commandUtil/CommandContext"); diff --git a/src/commands/trusted/cloop.js b/src/commands/trusted/cloop.js index f67446e..2889693 100644 --- a/src/commands/trusted/cloop.js +++ b/src/commands/trusted/cloop.js @@ -1,4 +1,4 @@ -const CommandError = require("../../commandUtil/errors/CommandError"); +const CommandError = require("../../commandUtil/CommandError"); const CommandContext = require("../../commandUtil/CommandContext"); const trustLevel = require("../../commandUtil/CommandTrustLevel"); diff --git a/src/data/translations.json b/src/data/translations.json index 28c35d6..ffa957e 100644 --- a/src/data/translations.json +++ b/src/data/translations.json @@ -111,19 +111,12 @@ "fnfboyfriendbot.command_manager.validation.invalid_role": "Invalid Role", "fnfboyfriendbot.command_manager.trust_level.console_only": "This command can only be ran via console!", - "fnfboyfriendbot.error.assertion": "An Assertion Error has occurred, hover over this for more info", - "fnfboyfriendbot.error.error": "An Error has occured, hover over this for more info", - "fnfboyfriendbot.error.range": "An Range Error has occurred, hover over this for more info", - "fnfboyfriendbot.error.reference": "An Reference Error has occured, hover over this for more info", - "fnfboyfriendbot.error.syntax": "An Syntax Error has occurred, hover over this for more info", - "fnfboyfriendbot.error.type_error": "An Type Error has occurred, hover over this for more info", - "fnfboyfriendbot.https_requesting.status.forbidden": "", "fnfboyfriendbot.https_requesting.": "", "fnfboyfriendbot.json.parse_invalid": "invalid json component to parse", "fnfboyfriendbot.json.stringify.invalid": "invalid json component to stringify", - "fnfboyfriendbot.json.stringify.too_long": "stringified json exceeds the max command block character limit", + "fnfboyfriendbot.json.too_long": "Stringified JSON exceeds the max command block character limit", "fnfboyfriendbot.reconnect.stop_logging": "stopped logging disconnect messages for now" } \ No newline at end of file diff --git a/src/modules/chat.js b/src/modules/chat.js index ea76115..bb36181 100644 --- a/src/modules/chat.js +++ b/src/modules/chat.js @@ -5,7 +5,6 @@ const creayun = require("../chatParsers/creayun"); const chipmunkmod = require("../chatParsers/chipmunkmod"); const vanilla = require('../chatParsers/vanilla/vanilla'); const me = require('../chatParsers/vanilla/me') -const { stringify } = require('../util/json'); function parseNbt (chatTypes, type, sender, target, message) { const chatType = chatTypes[type] @@ -183,7 +182,18 @@ class chat { }; bot.tellraw = (selector, message) => { - bot.core.run(`minecraft:tellraw ${selector} ` + stringify(message)); + const command = "minecraft:tellraw"; + const commandLength = 32767 - selector.length; + const messageLength = JSON.stringify(message).length + const finalLength = commandLength - messageLength + // here we check to see if the output surpasses the max command block character limit which is 32767 + if (messageLength > finalLength) + bot.core.run(`${command} ${selector} ` + JSON.stringify({ + translate: "fnfboyfriendbot.json.too_long", + fallback: bot.translations["fnfboyfriendbot.json.too_long"], + color: "red" + })) + else bot.core.run(`${command} ${selector} ` + JSON.stringify(message)); }; } } diff --git a/src/modules/commandHandler.js b/src/modules/commandHandler.js index 3907a67..d0df395 100644 --- a/src/modules/commandHandler.js +++ b/src/modules/commandHandler.js @@ -8,7 +8,7 @@ class commandHandler { const options = context.options; bot.on("parsed_message", (data) => { - //try { + try { if (data.type !== "minecraft:chat" && data.type !== "extras:command_spy") return; const prefixes = config.prefixes; prefixes.map((prefix) => { @@ -30,7 +30,7 @@ class commandHandler { source.sendFeedback = (message) => { if (options.mode === "savageFriends") bot.tellraw(`@a`, [ message - ])//[I; + ]) else bot.tellraw(`@p[nbt={UUID:[I;${source.player.entityUuid}]}]`, message) } ratelimit++; @@ -44,9 +44,9 @@ class commandHandler { bot.commandManager.executeString(source, command); } }); - //} catch (e) { - //console.log(e.stack); - //} + } catch (e) { + console.log(e.stack); + } }); } } diff --git a/src/modules/commandManager.js b/src/modules/commandManager.js index f2a6b6b..bf4c60a 100644 --- a/src/modules/commandManager.js +++ b/src/modules/commandManager.js @@ -1,7 +1,6 @@ const fs = require("fs"); const path = require("path"); -const CommandError = require("../commandUtil/errors/CommandError"); -const UnknownCommand = require('../commandUtil/errors/UnknownCommand') +const CommandError = require("../commandUtil/CommandError"); const CommandSource = require("../commandUtil/CommandSource"); const CommandArguments = require('../commandUtil/CommandArguments') const fixansi = require("../util/ansi"); @@ -24,17 +23,18 @@ class commandManager { try { const authFind = bot.auth.list.find((e) => e.player === source.player.uuid) if (!command) { - throw new UnknownCommand(commandName) + throw new CommandError({ + translate: "%s%s%s %s", + color: "red", + with: [ + { translate: "command.unknown.command", color: "red" }, + { text: "\n" }, + { text: `${commandName}`, color: "gray" }, + { translate: "command.context.here", color: "red" } + ] + }) } - process.on('uncaughtException', (error) => { - process.emit('error', source, error) - }) - - process.on('unhandledRejection', (error) => { - process.emit('error', source, error) - }) // unhandledRejection and uncaughtException emit here so the emitted error can use source.sendFeedback for async - if (authFind) { source.player.validateBypass = true if (authFind.trustLevel === "trusted") source.player.trustLevel = 1; @@ -100,7 +100,21 @@ class commandManager { discordClient, }); } catch (error) { - process.emit('error', source, error) + if (error instanceof CommandError) { + source.sendFeedback(error._message); + } else { + bot.console.error(error.stack) + source.sendFeedback( + { + translate: "command.failed", + color: "red", + hoverEvent: { + action: "show_text", + contents: String(error.stack), + }, + } + ) + } } }, diff --git a/src/modules/errors.js b/src/modules/errors.js deleted file mode 100644 index 775a4b5..0000000 --- a/src/modules/errors.js +++ /dev/null @@ -1,84 +0,0 @@ -class errors { - constructor (context) { - const bot = context.bot; - process.on('error', (source, error) => { - const errorName = error.name; - switch (errorName) { - case "CommandError": - source.sendFeedback(error._message) - break; - case "UnknownCommand": - source.sendFeedback(error._message) - break - case "Error": - source.sendFeedback({ - translate: "fnfboyfriendbot.error.error", - fallback: bot.translations["fnfboyfriendbot.error.error"], - color: "red", - hover_event: { - action: "show_text", - value: String(error.stack) - } - }) - break; - case "TypeError": - source.sendFeedback({ - translate: "fnfboyfriendbot.error.type_error", - fallback: bot.translations["fnfboyfriendbot.error.type_error"], - color: "red", - hover_event: { - action: "show_text", - value: String(error.stack) - } - }) - break; - case "RangeError": - source.sendFeedback({ - translate: "fnfboyfriendbot.error.range", - fallback: bot.translations["fnfboyfriendbot.error.range"], - color: "red", - hover_event: { - action: "show_text", - value: String(error.stack) - } - }) - break; - case "ReferenceError": - source.sendFeedback({ - translate: "fnfboyfriendbot.error.reference", - fallback: bot.translations["fnfboyfriendbot.error.reference"], - color: "red", - hover_event: { - action: "show_text", - value: String(error.stack) - } - }) - break; - case "AssertionError": - source.sendFeedback({ - translate: "fnfboyfriendbot.error.assertion", - fallback: bot.translations["fnfboyfriendbot.error.assertion"], - color: "red", - hover_event: { - action: "show_text", - value: String(error.stack) - } - }) - break; - case "SyntaxError": - source.sendFeedback({ - translate: "fnfboyfriendbot.error.syntax", - fallback: bot.translations["fnfboyfriendbot.error.syntax"], - color: "red", - hover_event: { - action: "show_text", - value: String(error.stack) - } - }) - break; - } - }); - } -} - -module.exports = errors; \ No newline at end of file diff --git a/src/util/json.js b/src/util/json.js deleted file mode 100644 index b1cb9af..0000000 --- a/src/util/json.js +++ /dev/null @@ -1,34 +0,0 @@ -const translations = require('../data/translations.json'); - -class json { - static parse (json) { - try { - return JSON.parse(json) - } catch { - return { - translate: "fnfboyfriendbot.json.parse_invalid", - fallback: translations["fnfboyfriendbot.json.parse_invalid"], - color: "red" - } - } - } - - static stringify (json) { - try { - const stringify = JSON.stringify(json); - if (stringify.length > 32681) return JSON.stringify({ - translate: "fnfboyfriendbot.json.stringify.too_long", - fallback: translations["fnfboyfriendbot.json.stringify.too_long"], - color: "red", - }) - else return stringify; - } catch { - return JSON.stringify({ - translate: "fnfboyfriendbot.json.stringify.invalid", - fallback: translations["fnfboyfriendbot.json.stringify.invalid"], - color: "red" - }) - } - } -} -module.exports = json \ No newline at end of file