4.3 KiB
Error Log
ENOENT: no such file or directory, open 'F:\Projects\Github\ECS-Discordweb\discord-bot\events...ackenddb.json'
Context: This error occurs in the guildMemberAdd and guildMemberRemove event handlers in the Discord bot when trying to read the db.json file.
Initial Fix Attempts:
path.joinwith relative path: Initially, the path was constructed usingpath.join(__dirname, '..\..\backend\db.json'). This was incorrect as it didn't go up enough directories.path.joinwith corrected relative path: The path was corrected topath.join(__dirname, '..\..\..\backend\db.json'). This seemed logically correct, but still resulted in the same error, with a strange......in the error path.path.resolve: The path construction was changed to usepath.resolve(__dirname, '..\..\..\backend\db.json')to ensure an absolute path was resolved. This also failed with the same error.
Root Cause Analysis:
The exact cause of the path resolution failure is unclear, but it seems to be related to how __dirname and relative paths are handled when the bot module is required by the backend server. The inconsistent working directory might be the source of the problem.
Solution:
To provide a more robust and centralized solution, the database path will be managed within the backend/db.js file.
- The
dbPathwill be exported frombackend/db.js. - The
guildMemberAdd.jsandguildMemberRemove.jsevent handlers will import thedbPathdirectly frombackend/db.js, eliminating the need for path resolution in the event handlers themselves.
SyntaxError: Unexpected token 'U', "U2FsdGVkX1"... is not valid JSON
Context: This error occurs in the guildMemberAdd and guildMemberRemove event handlers when trying to parse the content of db.json.
Root Cause Analysis:
The db.json file is encrypted, and the event handlers were reading the file content directly and trying to parse it as JSON. The encrypted string starts with "U2FsdGVkX1", which is not a valid JSON token, causing the JSON.parse to fail.
Solution:
The backend/db.js module already provides a readDb function that handles reading the file and decrypting its content. The event handlers must be updated to use this function instead of reading the file directly. This ensures that the encrypted data is correctly decrypted before being parsed as JSON.
Outdated Discord.js Practices
Context: The discord-bot is using a slightly outdated version of discord.js (^14.15.3 instead of the latest ^14.22.1). Additionally, the package.json includes @discordjs/rest and discord-api-types as explicit dependencies, which are now bundled with discord.js v14 and can cause conflicts.
Solution:
- Update the
discord.jsdependency to the latest stable version (^14.22.1). - Remove the
@discordjs/restanddiscord-api-typesdependencies frompackage.json. - Run
npm installto apply the changes and ensure all packages are up-to-date.
Error: Cannot find module 'discord-api-types/v9'
Context: After removing the discord-api-types package, the application fails to start due to a missing module error in discord-bot/deploy-commands.js.
Root Cause Analysis:
The deploy-commands.js file was still referencing discord-api-types/v9 for API-related enums or types. Since this package is no longer an explicit dependency (as it is bundled with discord.js v14), the import fails.
Solution:
The deploy-commands.js file needs to be updated to import the necessary modules, such as Routes, directly from discord.js instead of the now-removed discord-api-types package.
DiscordAPIError[10004]: Unknown Guild
Context: The backend throws an "Unknown Guild" error when the frontend tries to fetch the channels for a server that the bot is not a member of.
Root Cause Analysis:
The /api/servers/:guildId/channels endpoint was attempting to fetch guild information regardless of whether the bot was actually in that guild. This caused a Discord API error when the guild was not found.
Solution:
The endpoint will be updated to first check if the guild exists in the bot's cache using bot.client.guilds.cache.get(guildId). If the guild is not found, it will return an empty array, preventing the API error.