Skip to content

Commit

Permalink
chore: Add better command checking and some extra logs
Browse files Browse the repository at this point in the history
  • Loading branch information
jmiln committed Aug 12, 2024
1 parent a48629b commit 04a797d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 26 deletions.
52 changes: 29 additions & 23 deletions events/interactionCreate.js
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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.`);
Expand All @@ -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 {
Expand Down
12 changes: 9 additions & 3 deletions events/ready.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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();
Expand Down

0 comments on commit 04a797d

Please sign in to comment.