From 04a797dfda79b8c3039b6392d91b8a151941bf0a Mon Sep 17 00:00:00 2001 From: jmiln Date: Mon, 12 Aug 2024 13:00:13 -0700 Subject: [PATCH] chore: Add better command checking and some extra logs --- events/interactionCreate.js | 52 +++++++++++++++++++++---------------- events/ready.js | 12 ++++++--- 2 files changed, 38 insertions(+), 26 deletions(-) diff --git a/events/interactionCreate.js b/events/interactionCreate.js index 663e72e1..031d5427 100644 --- a/events/interactionCreate.js +++ b/events/interactionCreate.js @@ -7,6 +7,7 @@ const ignoreArr = [ "HTTPError [AbortError]: The user aborted a request.", "HTTPError: Service Unavailable", "Internal Server Error", // Something on Discord's end + "Invalid Webhook Token", // ????? "The user aborted a request", // Pretty sure this is also on Discord's end "Cannot send messages to this user", // A user probably has the bot blocked or doesn't allow DMs (No way to check for that) "Unknown interaction", // Not sure, but seems to happen when someone deletes an interaction that the bot is trying to reply to? @@ -63,7 +64,7 @@ module.exports = async (Bot, client, interaction) => { ); } - if (ignoreArr.some((str) => err.toString().includes(str))) { + if (ignoreArr.some((str) => err.toString().toLowerCase().includes(str.toLowerCase()))) { // Don't bother spitting out the whole mess. // Log which command broke, and the first line of the error logErr( @@ -120,26 +121,26 @@ module.exports = async (Bot, client, interaction) => { } else { const aliasList = aliases?.map((al) => ({ ...al, isAlias: true })) || []; - if (["unit", "character", "ship"].includes(focusedOption.name)) { - let unitList = []; - if (focusedOption.name === "unit") { - unitList = [...aliasList, ...Bot.CharacterNames, ...Bot.ShipNames]; - } else if (focusedOption.name === "character") { - unitList = [ - ...aliasList.filter((al) => Bot.CharacterNames.find((cn) => cn.defId === al.defId)), - ...Bot.CharacterNames, - ]; - } else if (focusedOption.name === "ship") { - unitList = [...aliasList.filter((al) => Bot.ShipNames.find((sn) => sn.defId === al.defId)), ...Bot.ShipNames]; - } - filtered = filterAutocomplete(unitList, focusedOption.value?.toLowerCase()); - filtered = filtered - .sort((a, b) => (a.name.toLowerCase() > b.name.toLowerCase() ? 1 : -1)) - .map((unit) => { - if (unit.isAlias) return { name: `${unit.name} (${unit.alias})`, value: unit.defId }; - return { name: unit.name, value: unit.defId }; - }); + if (!["unit", "character", "ship"].includes(focusedOption.name)) return; + + let unitList = []; + if (focusedOption.name === "unit") { + unitList = [...aliasList, ...Bot.CharacterNames, ...Bot.ShipNames]; + } else if (focusedOption.name === "character") { + unitList = [ + ...aliasList.filter((al) => Bot.CharacterNames.find((cn) => cn.defId === al.defId)), + ...Bot.CharacterNames, + ]; + } else if (focusedOption.name === "ship") { + unitList = [...aliasList.filter((al) => Bot.ShipNames.find((sn) => sn.defId === al.defId)), ...Bot.ShipNames]; } + filtered = filterAutocomplete(unitList, focusedOption.value?.toLowerCase()); + filtered = filtered + .sort((a, b) => (a.name.toLowerCase() > b.name.toLowerCase() ? 1 : -1)) + .map((unit) => { + if (unit.isAlias) return { name: `${unit.name} (${unit.alias})`, value: unit.defId }; + return { name: unit.name, value: unit.defId }; + }); } } catch (err) { logErr(`[interactionCreate, autocomplete, cmd=${interaction.commandName}] Unit name issue.`); @@ -157,13 +158,18 @@ module.exports = async (Bot, client, interaction) => { ); } catch (err) { // If it's one of the common errors, just move on, nothing that I can do about it - const ignoreArr = ["unknown interaction", "bad gateway", "service unavailable"]; + const ignoreArr = ["unknown interaction", "bad gateway", "service unavailable", "connect timeout", "unknown message"]; const errStr = ignoreArr.find((elem) => err.toString().toLowerCase().includes(elem)); - if (errStr) return; + if (errStr) { + if (errStr !== "unknown interaction") { + logErr(`[interactionCreate, autocomplete, cmd=${interaction.commandName}] Ignoring error: ${errStr}`); + } + return; + } // Otherwise, print out what I can about it if (typeof err !== "string") { - logErr(`[${Bot.myTime()}] [interactionCreate, autocomplete, cmd=${interaction.commandName}] Missing error.`); + logErr(`[interactionCreate, autocomplete, cmd=${interaction.commandName}] Missing error.`); console.error(interaction?.options?._hoistedOptions || interaction?.options); console.error(err); } else { diff --git a/events/ready.js b/events/ready.js index 52c9fe3f..c7a0077b 100644 --- a/events/ready.js +++ b/events/ready.js @@ -32,8 +32,13 @@ module.exports = async (Bot, client) => { Bot.socket.on("connect", () => { console.log(` [${client.shard.id}] Connected to EventMgr socket!`); }); - Bot.socket.on("disconnect", () => { - console.log(` [${client.shard.id}] Disconnected from EventMgr socket!`); + + Bot.socket.on('connect_error', err => console.error("[Socket.io connect_error]", err)); + Bot.socket.on('reconnect_error', err => console.error("[Socket.io reconnect_error]", err)); + Bot.socket.on('connect_failed', err => console.error("[Socket.io connect_failed]", err)); + Bot.socket.on("disconnect", (reason) => { + // The reason of the disconnection, for example "Ping Timeout" + console.log(` [${client.shard.id}] Socket.io disconnected from EventMgr socket! (${reason})`); }); // Start up the client.ws watcher @@ -60,7 +65,7 @@ module.exports = async (Bot, client) => { // Check all the ranks for shards (To send to channels) await Bot.shardRanks(); - // Only run these every 5min (on :5, :10, :15, etc) + // Only run these every 5min (on :05, :10, :15, etc) const min = new Date().getMinutes(); if (min % 5 === 0) { // Update the shard payout monitors @@ -80,6 +85,7 @@ module.exports = async (Bot, client) => { if (client.shard?.count > 0) { client.shard .broadcastEval((client) => client.reloadDataFiles()) + // .then(() => Bot.logger.log("[Ready/ReloadData data] Reloading all data files")) .catch((err) => console.log(`[Ready/ReloadData data]\n${err}`)); } else { client.reloadDataFiles();