Skip to content

[Reviewed] fix: add folder existence checks (Item 3)#27

Open
dromero14521 wants to merge 1 commit into
IN3PIRE:mainfrom
dromero14521:fix/folder-existence-checks
Open

[Reviewed] fix: add folder existence checks (Item 3)#27
dromero14521 wants to merge 1 commit into
IN3PIRE:mainfrom
dromero14521:fix/folder-existence-checks

Conversation

@dromero14521
Copy link
Copy Markdown
Contributor

This PR adds fs.existsSync checks before attempting to read directories for commands and events. If they do not exist, it warns and auto-creates them instead of crashing. Claiming issue #3 from the community bounty #21.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR aims to prevent crashes when loading command/event directories by adding existence checks and auto-creating missing folders.

Changes:

  • Add an fs.existsSync guard (and auto-create) before reading the commands directory in deploy-commands.js.
  • Add an fs.existsSync guard (and auto-create) before reading the events directory in bot.js.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
src/deploy-commands.js Adds a missing-directory check and mkdirSync before readdirSync when deploying application commands.
src/bot.js Adds a missing-directory check and mkdirSync before readdirSync when loading events.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/deploy-commands.js
Comment on lines 7 to +10
const commandsPath = path.join(__dirname, 'commands');
if (!fs.existsSync(commandsPath)) {
console.warn(`[WARNING] Commands directory missing at ${commandsPath}. Auto-creating it.`);
fs.mkdirSync(commandsPath, { recursive: true });
Copy link

Copilot AI Apr 29, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

deploy-commands.js is still looking in path.join(__dirname, 'commands'), but this repo’s slash commands live under src/slashcommands/. With the new auto-create behavior, a missing src/commands will now be created and commandFiles will be empty, causing the subsequent rest.put(..., { body: [] }) to overwrite/delete all registered app commands. Update the path to the real slash command directory (and avoid creating a new empty commands dir).

Suggested change
const commandsPath = path.join(__dirname, 'commands');
if (!fs.existsSync(commandsPath)) {
console.warn(`[WARNING] Commands directory missing at ${commandsPath}. Auto-creating it.`);
fs.mkdirSync(commandsPath, { recursive: true });
const commandsPath = path.join(__dirname, 'slashcommands');
if (!fs.existsSync(commandsPath)) {
console.error(`[ERROR] Slash commands directory missing at ${commandsPath}. Aborting command deployment.`);
process.exit(1);

Copilot uses AI. Check for mistakes.
Comment thread src/deploy-commands.js
Comment on lines +8 to 12
if (!fs.existsSync(commandsPath)) {
console.warn(`[WARNING] Commands directory missing at ${commandsPath}. Auto-creating it.`);
fs.mkdirSync(commandsPath, { recursive: true });
}
const commandFiles = fs.readdirSync(commandsPath).filter(f => f.endsWith('.js'));
Copy link

Copilot AI Apr 29, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider failing fast (non-zero exit) when the commands directory is missing or when no command files are found, rather than auto-creating the directory and continuing. As written, a CI/CD misconfiguration (wrong working dir, missing volume mount, etc.) can silently deploy an empty command set and wipe existing registered commands.

Copilot uses AI. Check for mistakes.
Comment thread src/deploy-commands.js
Comment on lines +8 to +10
if (!fs.existsSync(commandsPath)) {
console.warn(`[WARNING] Commands directory missing at ${commandsPath}. Auto-creating it.`);
fs.mkdirSync(commandsPath, { recursive: true });
Copy link

Copilot AI Apr 29, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PR title/description says "Fixes #3", but the referenced Issue #3 is about adding an /avatar slash command. This change is about directory existence checks (matching bounty-board item #21.3 instead). Please update the PR metadata (title/body/linked issue) so it closes the correct issue and doesn’t incorrectly resolve the avatar feature request.

Copilot uses AI. Check for mistakes.
Comment thread src/bot.js
const eventsPath = path.join(__dirname, 'events');
if (!fs.existsSync(eventsPath)) {
console.warn(`[WARNING] Events directory missing at ${eventsPath}. Auto-creating it.`);
fs.mkdirSync(eventsPath, { recursive: true });
Copy link

Copilot AI Apr 29, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fs.mkdirSync(eventsPath, { recursive: true }) can still throw (e.g., read-only filesystem, missing permissions). Since the intent is "warn and keep running", wrap the mkdir in try/catch (and consider returning early or exiting) so the bot doesn’t crash with an unhandled exception when it can’t create the directory.

Suggested change
fs.mkdirSync(eventsPath, { recursive: true });
try {
fs.mkdirSync(eventsPath, { recursive: true });
} catch (error) {
console.warn(`[WARNING] Failed to create events directory at ${eventsPath}: ${error.message}`);
return;
}

Copilot uses AI. Check for mistakes.
@TrivCodez TrivCodez changed the title fix: add directory existence checks (Fixes #3) [Reviewed] fix: add folder existence checks (Item 3) Apr 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants